Free Pascal 找不到 dll 的入口点

发布于 2025-01-08 07:32:24 字数 509 浏览 1 评论 0原文

我对帕斯卡完全陌生。 我想在 free pascal 中调用 .dll 文件中的函数,但运行项目时出现以下错误:

无法在动态链接库 HNLib.dll 中找到过程入口点 GetProcAddress。

这是代码:

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 位于一个目录中。

请帮我!

I am complete new to pascal.
I want to call my function in .dll file in free pascal and I get following error when I run the project:

The procedure entry point GetProcAddress could not be located in the dynamic link library HNLib.dll.

here is the code:

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 file and dll are in one directory.

Please Help ME!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青丝拂面 2025-01-15 07:32:24

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

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

您还至少需要您的 Windows 单元使用子句,因为那是声明了 GetProcAddressLoadLibrary

请参阅 LoadLibrary 上的 WinAPI 文档和 <一个href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms683212%28v=vs.85%29.aspx">GetProcAddress 了解更多信息。

对于初级程序员,您可能会发现使用函数的静态链接而不是动态链接(通过 GetProcAddress 获得)更容易。静态链接的一个例子是(未经测试!!! - 只是一个快速代码示例,因为我没有“HNLib.DLL”来链接):

// 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 中通常不能有多个同名函数,因为在加载完成时没有可用于确定要加载哪个函数的信息。每个都应该有一个单独的、不同的名称,否则加载可能会失败。

GetProcAddress is not what you seem to think it is; it's purpose is to locate named procedures or functions in a DLL and return the address of that function so it can be called from your code. You have to first use LoadLibrary to load the dynamic link library (DLL) into memory, and then pass a handle to that DLL as the first parameter of GetProcAddress and the name of the function whose address you want as the second parameter. If the function can be found in the DLL, it's address is returned, and you can use that address to call the function.

(In addition, GetProcAddress is pretty Windows-specific, and the majority of functions in the WinAPI are stdcall and not cdecl. Unless you have documentation saying that the functions are using the cdecl calling convention, you should probably use stdcall.)

You would also need at least the Windows unit in your uses clause, since that's where GetProcAddress and LoadLibrary are declared.

See the WinAPI documentation on LoadLibrary and GetProcAddress for more information.

For a beginning programmer, you'll probably find it easier to use static linking of the functions instead of dynamic (which you get with GetProcAddress). An example of static linking would be (untested !!!- just a quick code example, since I don't have 'HNLib.DLL' to link against):

// 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.

Note that when statically linking DLL functions like this, your DLL must be available when your app starts, and the function must be contained in that DLL; if not, your application won't load.

Also, note that you can't typically have multiple functions with the same name in the DLL, as there's no information available to use to figure which one to load when the load is being done. Each should have a separate, distinct name or the loading will probably fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文