关于delphi:免费Pascal找不到dll的入口点

Free Pascal can't find entry point for dll

我是Pascal的新手。
我想在免费的pascal中调用.dll文件中的函数,并且在运行项目时出现以下错误:

过程入口点GetProcAddress不在动态链接库HNLib.dll中。

代码如下:

1
2
3
4
5
6
7
8
9
Program Test;
function GetProcAddress : Integer; cdecl; external 'HNLib.dll';
function GetProcAddress : Single; cdecl; external 'HNLib.dll';
procedure GetProcAddress( X : Single); cdecl; external 'HNLib.dll';
procedure GetProcAddress; cdecl; external 'HNLib.dll';
begin
  GetProcAddress( 5.5 );
  readln;
end.

.pas文件和dll位于一个目录中。

请帮助我!


GetProcAddress并非您所想的;它的目的是在DLL中找到命名的过程或函数,并返回该函数的地址,以便可以从您的代码中调用它。您必须首先使用LoadLibrary将动态链接库(DLL)加载到内存中,然后将句柄作为GetProcAddress的第一个参数传递给该DLL,并将函数的名称作为第二个参数传递给它。如果可以在DLL中找到该函数,则将返回其地址,您可以使用该地址来调用该函数。

(此外,GetProcAddress是特定于Windows的,WinAPI中的大多数功能是stdcall而不是cdecl。除非您有文档说这些功能正在使用cdecl调用约定,否则,您可能应该使用stdcall。)

您还需要在using子句中至少使用Windows单元,因为在此声明了GetProcAddressLoadLibrary

有关更多信息,请参见LoadLibrary和GetProcAddress上的WinAPI文档。

对于刚开始的程序员,您可能会发现使用函数的静态链接而不是动态链接(使用GetProcAddress获得)更容易。静态链接的示例为(未经测试!!!! –仅是一个快速的代码示例,因为我没有\\'HNLib.DLL \\'可以链接到):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Your Dll import unit
unit MyDllProcs;

interface

  function GetIntCalcResult(const IntVal: Integer);

implementation

  function GetIntCalcResult(const IntVal: Integer); stdcall; external 'HNLib.dll';

end.

// Your own app's code
program Test;

interface

  uses MyDllProcs;

implementation

function DoSomethingWithDll(const ValueToCalc: Integer): Integer;        
begin
  Result := GetIntCalcResult(ValueToCalc);
end;

begin
  WriteLn('DoSomethingWithDll returned ', DoSomethingWithDll(10));
  ReadLn;
end.

请注意,当像这样静态链接DLL函数时,您的DLL必须在应用启动时可用,并且该函数必须包含在该DLL中;如果不是,则您的应用程序将无法加载。

此外,请注意,DLL中通常不能具有多个具有相同名称的功能,因为没有信息可用于确定加载完成时要加载哪个功能。每个名称应使用单独的唯一名称,否则加载可能会失败。