VC6.0的DLL问题

来源:百度知道 编辑:UC知道 时间:2024/07/04 09:14:53
程序是这样的:
DLL

.cpp
int _stdcall getAge(void)
{
return 318;
}
int _stdcall add(int a, int b)
{
int c=a+b;
return c;
}

.h
int getAge(void);
int add(int a, int b);

.def
EXPORTS
getAge
add
/////////////////
测试程序
void CTestDLLDlg::OnButton1()
{
typedef int(*pAdd)(int a, int b);
HINSTANCE hDLL;
pAdd add;
hDLL=LoadLibrary("Win32DLL.dll");
add=(pAdd)GetProcAddress(hDLL,"add");

int a=1,b=2;
int c;
c=add(a, b);
CString str;
str.Format("%d",c);
MessageBox(str);

FreeLibrary(hDLL);
}

运行到c=add(a, b);会崩溃,为什么

DLL中导出的函数名应该不是add,你用Depends看看,应该用 extern "C" add

add=(pAdd)GetProcAddress(hDLL,"add");

这一步不一定会成功的,另外你函数导出时貌似也有问题,可以参考一下一楼的做法

去掉_stdcall

.cpp
int getAge(void)
{
return 318;
}
int add(int a, int b)
{
int c=a+b;
return c;
}

完整工程:
http://www.namipan.com/d/baidu_ask_70870757.zip/30aef0b549b09473acb0c410e1e9fd270a087f1629c50000