调用动态链接库

1、静态调用

在程序运行时自动加载,加载失败应用程序退出。

   
分步语法 function add(a,b:Integer):Integer; stdcall;
function add; external 'project1.dll' name 'adddigit';
联合语法 function add(a,b:Integer):Integer; stdcall; external 'project1.dll' name 'adddigit';

2、动态调用

用程序代码加载动态链接库中的函数并运行。

步骤 说明 示例
1 加载 DLL 文件 var FLibHandle: THandle;

FLibHandle := LoadLibrary('NETAPI32.DLL');

if FLibHandle = 0 then Exit;

2 定义函数指针 var
   NetShareDelNT: function(servername: PWideChar; netname: PWideChar; reserved: DWORD): LongInt; stdcall;
3 读取函数指针 @NetShareDelNT := GetProcAddress(FLibHandle, 'NetShareDel');

if not Assigned(NetShareDelNT) then
begin
   FreeLibrary(FLibHandle);
   Exit;
end;

4 运行函数 NetShareEnumNT(nil, 2, @ShareNT, DWORD(-1),@entriesread, @totalentries, nil)
5 释放 DLL 文件 FreeLibrary(FLibHandle);