DLL dynamically load/zh CN

From Free Pascal wiki
Revision as of 03:52, 19 October 2021 by Robsean (talk | contribs)
Jump to navigationJump to search
Windows logo - 2012.svg

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;

   ...