为什么 GetProcAddress 不起作用?
首先,我创建一个名为 SimpleDll.dll
的简单 dll,其头文件:
// SimpleDll.h
#ifdef MYLIBAPI
#else
#define MYLIBAPI __declspec(dllimport)
#endif
MYLIBAPI int Add(int a. int b);
其源代码:
// SimpleDll.c
#include <windows.h>
#define MYLIBAPI __declspec(dllexport)
#include "SimpleDll.h"
int Add(int a, int b)
{
return a + b;
}
然后我在另一个项目中调用它,它工作正常:
// TestSimpleDll.c
#include "stdafx.h"
#include <windows.h>
#include "SimpleDll.h"
#pragma comment(lib, "SimpleDll.lib")
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d", Add(10, 30)); // Give the expected result 40
return 0;
}
但是,当我调用 GetProcAddress< /code> 来获取它的地址,它不起作用!
// TestSimpleDll2.c
#include "stdafx.h"
#include <windows.h>
#include "SimpleDll.h"
#pragma comment(lib, "SimpleDll.lib")
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d", Add(10, 30)); // Give the expected result 40
HMODULE hModule = GetModuleHandleA("SimpleDll.dll"); // hModule is found
PROC add_proc = GetProcAddress(hModule, "Add"); // but Add is not found !
// add_proc is NULL!
return 0;
}
感谢您的帮助。 (PS:我在Windows7上使用VS2010)
更新:
这是 SimpleDll.dll
文件的依赖遍历器显示的内容:
First, I create a simple dll called SimpleDll.dll
, its head file:
// SimpleDll.h
#ifdef MYLIBAPI
#else
#define MYLIBAPI __declspec(dllimport)
#endif
MYLIBAPI int Add(int a. int b);
its source code:
// SimpleDll.c
#include <windows.h>
#define MYLIBAPI __declspec(dllexport)
#include "SimpleDll.h"
int Add(int a, int b)
{
return a + b;
}
Then I call it in another project, and it works fine:
// TestSimpleDll.c
#include "stdafx.h"
#include <windows.h>
#include "SimpleDll.h"
#pragma comment(lib, "SimpleDll.lib")
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d", Add(10, 30)); // Give the expected result 40
return 0;
}
However, when I call GetProcAddress
to get it's address, it doesn't work!
// TestSimpleDll2.c
#include "stdafx.h"
#include <windows.h>
#include "SimpleDll.h"
#pragma comment(lib, "SimpleDll.lib")
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d", Add(10, 30)); // Give the expected result 40
HMODULE hModule = GetModuleHandleA("SimpleDll.dll"); // hModule is found
PROC add_proc = GetProcAddress(hModule, "Add"); // but Add is not found !
// add_proc is NULL!
return 0;
}
Thanks for your help. (PS: I use VS2010 on Windows7)
Update:
This is what the depedency walker show for the SimpleDll.dll
file:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要导出 GetProcAddress 的名称,则应使用 .def 文件。否则,您将不得不处理 c++ 名称 重整 和符号装饰。
您可以通过将函数声明为
extern "C"
来避免损坏,但避免修饰的唯一方法是使用 .DEF 文件。另一件事 - 在 Dependency walker 中 - 使用 F10 在修饰名称和未修饰名称之间切换。
You should use a .def file if you want to export the name for GetProcAddress. Otherwise you will have to deal with c++ name mangling and with symbol decorations.
You can avoid mangling by declaring your function as
extern "C"
, but the only way to avoid decorations is to use a .DEF file.One more thing - in Dependency walker - use F10 to toggle between decorated and undecorated names.
Dependency Walker 是解决此类 DLL 问题的出色工具。
我假设您正在将 DLL 编译为 C 代码。否则,C++ 执行名称修改会导致问题。
为了避免名称修改,只需将导出定义包装在 extern“C”中。
Dependency Walker is an excellent tool for troubleshooting DLL issues like this.
I'm assuming you are compiling the DLL as C code. Otherwise, C++ performs name mangling that would cause problems.
To avoid name mangling simply wrap the export definition in extern "C".