VC dll的调用技巧
Dll2.cpp:
int add(int a,int b)
{
return a+b;
}
int substract(int a,int b)
{
return a-b;
}
Dll2.def:
LIBRARY D112
EXPORTS
add
substract
正确调用:
HMODULE hMod=LoadLibrary("Dll2.dll");
typedef int (/*_stdcall*/ *MSGBOX)(int a,int b);
MSGBOX he=(MSGBOX)GetProcAddress(hMod,/*MAKEINTRESOURCE(1)*/"add");
if (!he)
{
// handle the error
FreeLibrary(hMod);
int i=GetLastError();
CString str_err;
str_err.Format("错误:%d",i);
AfxMessageBox(str_err);
}
CString str;
//int i=he(2,3);
str.Format("5+3=%d",he(5,3));
MessageBox(str);
错误调用:
HMODULE hMod=LoadLibrary("Dll2.dll");
typedef int (_stdcall *MSGBOX)(int a,int b);
MSGBOX he=(MSGBOX)GetProcAddress(hMod,/*MAKEINTRESOURCE(1)*/"add");
if (!he)
{
// handle the error
FreeLibrary(hMod);
int i=GetLastError();
CString str_err;
str_err.Format("错误:%d",i);
AfxMessageBox(str_err);
}
CString str;
//int i=he(2,3);
str.Format("5+3=%d",he(5,3));
MessageBox(str);