首先,我创建了一个名为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来获取它的地址时,它不起作用!
// 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 )
更新:
这是depedency walker为SimpleDll.dll文件显示的内容:

发布于 2011-04-16 14:16:04
如果要导出GetProcAddress的名称,则应使用.def文件。否则,您将不得不处理c++名称、mangling和符号修饰。
您可以通过将函数声明为extern "C"来避免损坏,但避免修饰的唯一方法是使用.DEF文件。
还有一件事-在依赖遍历中-使用F10在修饰和未修饰名称之间切换。
发布于 2011-04-16 13:51:50
对于解决这样的DLL问题,Dependency Walker是一个很好的工具。
我假设您正在将DLL编译为C代码。否则,C++将执行名称损坏,这将导致问题。
要避免名称损坏,只需将导出定义包装在外部"C“中。
extern "C" {
MYLIBAPI int Add(int a. int b);
}https://stackoverflow.com/questions/5684737
复制相似问题