windows 如何在 C 中导入 DLL 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3594750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import a DLL function in C?
提问by Mikey
I was given a DLL that I'm trying to use. The DLL contains the function "send". this is what I did:
我得到了一个我正在尝试使用的 DLL。DLL 包含函数“发送”。这就是我所做的:
#include <stdio.h>
#include <Windows.h>
int main(int argc, char * argv[])
{
HMODULE libHandle;
if ((libHandle = LoadLibrary(TEXT("SendSMS.dll"))) == NULL)
{
printf("load failed\n");
return 1;
}
if (GetProcAddress(libHandle, "send") == NULL)
{
printf("GetProcAddress failed\n");
printf("%d\n", GetLastError());
return 1;
}
return 0;
}
GetProcAddress returns NULL, and the last error value is 127. (procedure was not found)
GetProcAddress 返回NULL,最后一个错误值为127。(过程没找到)
What am I doing wrong?
我究竟做错了什么?
回答by Zuljin
Code look more or less good, so probably something is wrong with *.dll. Please download Dependency Walkerapplication and check what kind of functions are exported by this library.
代码看起来或多或少不错,所以 *.dll 可能有问题。请下载Dependency Walker应用程序并检查该库导出的函数类型。
回答by ertan
If you running 64bit environment and "sendsms.dll" is compiled as 32bit loadlibrary does not work. You need to compile your project as 32bit to load dlls.
如果您运行 64 位环境并且“sendsms.dll”被编译为 32 位 loadlibrary 不起作用。您需要将项目编译为 32 位才能加载 dll。
回答by valdo
Probably the DLL doesn't export such a function.
DLL 可能不会导出这样的函数。
This is usually caused by the "decorations" the compiler adds to the function name. For instance "send" may actually be seen as:
这通常是由编译器添加到函数名称中的“装饰”引起的。例如“发送”实际上可能被视为:
_send
_send@4
?send@@ABRACADABRA
_send
_send@4
?send@@ABRACADABRA
To resolve this that's what you should do:
要解决这个问题,您应该这样做:
- Use the "depends" utility (depends32.exe, shipped with MSVC) to view what your DLL actuallyexports.
- If you're the author of the DLL - you may force the export name to be what you want, by using "def" file (for linker)
- 使用“depends”实用程序(depends32.exe,随 MSVC 一起提供)查看 DLL实际导出的内容。
- 如果您是 DLL 的作者 - 您可以使用“def”文件(用于链接器)强制导出名称为您想要的名称
回答by Puppy
I noticed that you're using TEXT on LoadLibrary, but not on GetProcAddress. If GetProcAddress is misinterpreting your string, it could be looking for the wrong function.
我注意到您在 LoadLibrary 上使用 TEXT,而不是在 GetProcAddress 上。如果 GetProcAddress 误解了您的字符串,则它可能正在寻找错误的函数。