【发布时间】:2011-08-06 18:49:27
【问题描述】:
首先,我创建一个名为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)
更新:
这是 SimpleDll.dll 文件的依赖行者显示的内容:
【问题讨论】:
-
1) 为什么要明确使用
GetModuleHandle的ANSI 版本? 2)如果采用widechar路线,您应该将字符串文字包装在_T("") -
你的名字被破坏了,使用 .def 文件
标签: c++ windows visual-studio winapi name-decoration