Difference between revisions of "DLL dynamically load/zh CN"
From Free Pascal wiki
Jump to navigationJump to search (Created page with "{{DLL_dynamically_load |DLL_动态地_加载}} 这篇教程向你展示如何动态地加载一个DLL(动态链接库)。 The DLL (DLLTest.dll) referred to in the example be...") |
|||
Line 3: | Line 3: | ||
这篇教程向你展示如何动态地加载一个DLL(动态链接库)。 | 这篇教程向你展示如何动态地加载一个DLL(动态链接库)。 | ||
− | + | 在下面的示例中将引用的DLL(在DLLTest.dll中的源文件): | |
<syntaxhighlight lang=pascal> | <syntaxhighlight lang=pascal> | ||
Line 15: | Line 15: | ||
{$R *.res} | {$R *.res} | ||
− | // | + | // DLL中的子程序 |
function funStringBack(strIn : string) : PChar; | function funStringBack(strIn : string) : PChar; | ||
begin | begin | ||
Line 22: | Line 22: | ||
− | // | + | // 导出子程序 |
exports | exports | ||
funStringBack; | funStringBack; | ||
Line 37: | Line 37: | ||
** Memory must be reserved for a variable (data field) to which the above data type is assigned. | ** Memory must be reserved for a variable (data field) to which the above data type is assigned. | ||
** Memory must be reserved for a handle, to which the DLL handle will be assigned later. | ** Memory must be reserved for a handle, to which the DLL handle will be assigned later. | ||
− | * | + | * 分配DLL和外部的子例程并接受数据 |
** Call the DLL and assign the handle of the DLL to the handle. | ** Call the DLL and assign the handle of the DLL to the handle. | ||
** The pointer for the variables must be changed to the memory of the external subroutine. | ** The pointer for the variables must be changed to the memory of the external subroutine. | ||
** The result of the external subroutine is to be accepted. | ** The result of the external subroutine is to be accepted. | ||
− | * | + | * 释放所有的存储器 |
− | ** | + | ** 变量的指针必须再次指向一个无效的存储器区域(:= nil)以释放外部的子程序。 |
− | ** | + | ** DLL的存储器必须再次释放。 |
− | + | 在你自己的程序中,集成/融入、使用和释放DLL中的子程序: | |
<syntaxhighlight lang=pascal> | <syntaxhighlight lang=pascal> | ||
Line 55: | Line 55: | ||
Include function funDll : string; | Include function funDll : string; | ||
type | type | ||
− | // | + | // 照着DLL(在源文件中)所使用的定义的方式,定义将要调用的子程序 |
TfunStringBack = function(strIn : string) : PChar; stdcall; | TfunStringBack = function(strIn : string) : PChar; stdcall; | ||
var | var | ||
− | // | + | // 为DLL子程序创建一个合适的变量(数据字段) |
funStringBack : TfunStringBack; | funStringBack : TfunStringBack; | ||
− | // | + | // 为DLL创建一个句柄 |
LibHandle : THandle; | LibHandle : THandle; | ||
begin | begin | ||
− | // | + | // 获取将要使用的库的句柄 |
LibHandle := LoadLibrary(PChar('DLLTest.dll')); | LibHandle := LoadLibrary(PChar('DLLTest.dll')); | ||
− | // | + | // 检查是否成功加载DLL |
if LibHandle <> 0 then | if LibHandle <> 0 then | ||
begin | begin | ||
− | // | + | // 分配子程序调用的存储器地址给变量funStringBack |
− | // 'funStringBack' | + | // 'funStringBack'来自DLL DLLTest.dll |
Pointer(funStringBack) := GetProcAddress(LibHandle, 'funStringBack'); | Pointer(funStringBack) := GetProcAddress(LibHandle, 'funStringBack'); | ||
− | // | + | // 检查是否已经返回一个有限的存储器地址 |
if @funStringBack <> nil then | if @funStringBack <> nil then | ||
Result := funStringBack('hello world'); | Result := funStringBack('hello world'); | ||
end; | end; | ||
− | // | + | // 释放存储器y |
funStringBack := nil; | funStringBack := nil; | ||
FreeLibrary(LibHandle); | FreeLibrary(LibHandle); |
Revision as of 03:52, 19 October 2021
This article applies to Windows only.
See also: Multiplatform Programming Guide
│
Deutsch (de) │
English (en) │
русский (ru) │
中文(中国大陆) (zh_CN) │
这篇教程向你展示如何动态地加载一个DLL(动态链接库)。
在下面的示例中将引用的DLL(在DLLTest.dll中的源文件):
library info;
{$mode objfpc} {$H+}
uses
SysUtils;
{$R *.res}
// DLL中的子程序
function funStringBack(strIn : string) : PChar;
begin
funStringBack := PChar(UpperCase(strIn));
end ;
// 导出子程序
exports
funStringBack;
begin
end.
我应该做什么:
- 定义存储器
- A data type must be created that corresponds exactly to the (external) subroutine that is to be imported from the DLL.
- 保留存储器
- Memory must be reserved for a variable (data field) to which the above data type is assigned.
- Memory must be reserved for a handle, to which the DLL handle will be assigned later.
- 分配DLL和外部的子例程并接受数据
- Call the DLL and assign the handle of the DLL to the handle.
- The pointer for the variables must be changed to the memory of the external subroutine.
- The result of the external subroutine is to be accepted.
- 释放所有的存储器
- 变量的指针必须再次指向一个无效的存储器区域(:= nil)以释放外部的子程序。
- DLL的存储器必须再次释放。
在你自己的程序中,集成/融入、使用和释放DLL中的子程序:
uses
Windows, ...;
...
Include function funDll : string;
type
// 照着DLL(在源文件中)所使用的定义的方式,定义将要调用的子程序
TfunStringBack = function(strIn : string) : PChar; stdcall;
var
// 为DLL子程序创建一个合适的变量(数据字段)
funStringBack : TfunStringBack;
// 为DLL创建一个句柄
LibHandle : THandle;
begin
// 获取将要使用的库的句柄
LibHandle := LoadLibrary(PChar('DLLTest.dll'));
// 检查是否成功加载DLL
if LibHandle <> 0 then
begin
// 分配子程序调用的存储器地址给变量funStringBack
// 'funStringBack'来自DLL DLLTest.dll
Pointer(funStringBack) := GetProcAddress(LibHandle, 'funStringBack');
// 检查是否已经返回一个有限的存储器地址
if @funStringBack <> nil then
Result := funStringBack('hello world');
end;
// 释放存储器y
funStringBack := nil;
FreeLibrary(LibHandle);
end;
...