Delphi调用一些EXE,DLL

Delphi调用一些EXE,DLL

1.调用EXE

WINEXEC//调用可执行文件

winexec('command.com /c copy *.* c:\',SW_Normal);

winexec('start abc.txt');

ShellExecute或ShellExecuteEx//启动文件关联程序

uses shellapi

shellexecute(application.Handle ,'open','www.csdn.net,nil,nil,sw_normal);

function executefile(const filename,params,defaultDir:string;showCmd:integer):THandle;

ExecuteFile('C:\abc\a.txt','x.abc','c:\abc\',0);

ExecuteFile('http://tingweb.yeah.net','','',0);

ExecuteFile('mailto:tingweb@wx88.net','','',0);

2.调用DLL

在Delphi中动态调用DLL

动态调用DLL相对复杂很多,但非常灵活。

为了全面的说明该问题,这次我们举一个调用由C++编写的DLL的例子。首先在C++中编译下面的DLL源程序。

#include

extern ”C” _declspec(dllexport)

int WINAPI TestC(int i)

{

return i;

}

编译后生成一个DLL文件,在这里我们称该文件为Cpp.dll,该DLL中只有一个返回整数类型的函数TestC。为了方便说明,我们仍然引用上面的调用程序,只是将原来的Button1Click过程中的语句用下面的代码替换掉了。

procedure TForm1.Button1Click(Sender: TObject);

type

TIntFunc=function(i:integer):integer;stdcall;

var

Th:Thandle;

Tf:TIntFunc;

Tp:TFarProc;

begin

Th:=LoadLibrary(’Cpp.dll’); {装载DLL}

if Th>0 then

try

Tp:=GetProcAddress(Th,PChar(’TestC’));

if Tp<>nil

then begin

Tf:=TIntFunc(Tp);

Edit1.Text:=IntToStr(Tf(1)); {调用TestC函数}

end

else

ShowMessage(’TestC函数没有找到’);

finally

FreeLibrary(Th); {释放DLL}

end

else

ShowMessage(’Cpp.dll没有找到’);

end;