本示例介绍如何在 Delphi 中调用外部 DLL 文件及其内存释放方法,帮助开发者掌握 Delphi 与动态链接库之间的交互技巧。
在Delphi中调用DLL并通过主程序释放的例子如下:
首先,在Delphi的主程序代码中需要导入并声明所要使用的DLL中的函数。例如假设有一个名为`MyDll.dll`的库,并且我们需要使用其中的一个函数,该函数原型为:
```delphi
function MyFunction(param1: Integer; param2: String): Integer; stdcall;
```
我们首先在Delphi程序中导入这个函数:
```delphi
var
hModule: THandle;
procedure InitDll;
begin
hModule := LoadLibrary(MyDll.dll);
if hModule <> 0 then begin
// 成功加载DLL,接下来可以调用其中的函数了。
MyFunction := GetProcAddress(hModule, MyFunction);
// 调用该函数:
MyFunction(12345, Hello World!);
end;
end;
procedure FreeDll;
begin
if hModule <> 0 then begin
FreeLibrary(hModule);
end;
end;
```
这里`InitDll`过程用于加载DLL并获取函数地址,而`FreeDll`则用来释放该库。在程序的适当位置调用这两个过程即可完成对DLL的操作。
以上就是Delphi中使用和释放DLL的一个基本例子。