如何在C中导入DLL函数?
我得到了一个我正在尝试使用的 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 返回 NULL,最后一个错误值为 127。(未找到过程)
我做错了什么?
I was given a DLL that I'm trying to use. The DLL contains the function "send".
this is what I did:
#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)
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
代码看起来或多或少都不错,所以 *.dll 可能有问题。请下载Dependency Walker应用程序并检查该库导出了哪些类型的函数。
Code look more or less good, so probably something is wrong with *.dll. Please download Dependency Walker application and check what kind of functions are exported by this library.
如果您运行 64 位环境并且“sendsms.dll”被编译为 32 位,则 loadlibrary 不起作用。您需要将项目编译为 32 位才能加载 dll。
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.
DLL 可能不导出这样的函数。
这通常是由编译器添加到函数名称中的“修饰”引起的。例如,“发送”实际上可能被视为:
_send
_send@4
?send@@ABRACADABRA
要解决此问题,您应该执行以下操作:
Probably the DLL doesn't export such a function.
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
To resolve this that's what you should do:
我注意到您在 LoadLibrary 上使用 TEXT,但不在 GetProcAddress 上使用 TEXT。如果 GetProcAddress 误解了您的字符串,则它可能正在寻找错误的函数。
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.