DELPHI可以调用任何语言写的DLL文件,调用的方法例子代码:
varpsapi: cardinal
//下面申明DLL中子程序的原型
EnumProcesses: function (lpidProcess, cb, cbNeeded: DWORD): Integer stdcall
EnumProcessModules: function (hProcess: THandle lphModule: HMODULE cb, lpcbNeeded: DWORD): Integer stdcall
GetModuleBaseNameA: function 桐御(hProcess: THandle HMODULE: HMODULE lpBaseName: PChar nSize: DWORD): Integer stdcall
GetModuleFileNameExA: function (hProcess: THandle HMODULE: HMODULE lpFileName: PChar nSize: DWORD): Integer stdcall
begin
//打开PSAPI.DLL文件
psapi:=LoadLibrary('psapi')
if psapi<>0 缺租then
try
//把函数原型与DLL关联
EnumProcesses:=GetProcAddress(psapi,'EnumProcesses')
伏轮兆 EnumProcessModules:=GetProcAddress(psapi,'EnumProcessModules')
GetModuleBaseNameA:=GetProcAddress(psapi,'GetModuleBaseNameA')
GetModuleFileNameExA:=GetProcAddress(psapi,'GetModuleFileNameExA')
//执行审核的函数
if (@GetModuleBaseNameA<>nil) and (@GetModuleFileNameExA<>nil) and (@EnumProcessModules<>nil) and (@EnumProcesses<>nil) then
//创建DLLlibrary test
uses
dialogs,
SysUtils,
Classes
{$R *.res}
function My_Max(a,b:integer):integerstdcall
begin
if (a >b) then
result := a
else
result := b
end
exports My_Max
begin
end.
//调用开始
unit Unit11
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,stdctrls
type
TForm1 = class(TForm)
Button1: TButton
procedure Button1Click(Sender: TObject)
private
{ Private declarations }
public
{ Public declarations }
end
var
Form1: TForm1
implementation
{$R *.dfm}
function My_Max(a,b:integer):integerstdcall
external 'E:\mypro\1019(3)\test.dll'
procedure TForm1.Button1Click(Sender: TObject)
begin
showmessage(inttostr(my_max(25,10)))
end
end.
调用时注意DLL里的函数是区返陵陪别大小写的,这点和DELPHI里的函数不太一样,如果调用的和DLL里的不一致,汪拆会报一个漏蠢无法定位程序输入点的错误。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)