Free Pascal 找不到 dll 的入口点
我对帕斯卡完全陌生。 我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GetProcAddress
并不是你想象的那样;它的目的是在 DLL 中定位命名过程或函数并返回该函数的地址,以便可以从代码中调用它。您必须首先使用LoadLibrary
将动态链接库 (DLL) 加载到内存中,然后将该 DLL 的句柄作为GetProcAddress
的第一个参数以及名称传递给该 DLL。您想要将其地址作为第二个参数的函数。如果可以在 DLL 中找到该函数,则返回它的地址,您可以使用该地址来调用该函数。(此外,
GetProcAddress
是 Windows 特定的,WinAPI 中的大多数函数是stdcall
而不是cdecl
。除非您有文档说这些函数使用cdecl
调用约定,您可能应该使用stdcall
。)您还至少需要您的 Windows 单元使用子句,因为那是声明了
GetProcAddress
和LoadLibrary
。请参阅 LoadLibrary 上的 WinAPI 文档和 <一个href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms683212%28v=vs.85%29.aspx">GetProcAddress 了解更多信息。
对于初级程序员,您可能会发现使用函数的静态链接而不是动态链接(通过
GetProcAddress
获得)更容易。静态链接的一个例子是(未经测试!!! - 只是一个快速代码示例,因为我没有“HNLib.DLL”来链接):请注意,当像这样静态链接 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 useLoadLibrary
to load the dynamic link library (DLL) into memory, and then pass a handle to that DLL as the first parameter ofGetProcAddress
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 arestdcall
and notcdecl
. Unless you have documentation saying that the functions are using thecdecl
calling convention, you should probably usestdcall
.)You would also need at least the
Windows
unit in your uses clause, since that's whereGetProcAddress
andLoadLibrary
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):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.