void CCardDlg::OnButton1()
{
int fMin;
HINSTANCE hIn;
// 创建一个新的函数指针数据类型
typedef int(MINTYPE)(char*,char*);
MINTYPE*pMin=0;
// 载入FindMin.dll
VERIFY(hIn=::LoadLibrary("mydll.dll"));
// 取得FindMin()函数的地址
??? VERIFY(pMin=(MINTYPE*)::GetProcAddress((HMODULE)hIn, "mydll"));
// 测试动态链接库的FindMin()函数
/* char a[10];
char b[1024]; strcpy(a,"aaaaaaa");
strcpy(b,"this is start test"); fMin=(*pMin)(a,b);
// 显示测试结果
CString str;
str.Format("The minimum of %s and %s is %d", a,b,fMin);
AfxMessageBox(str);
// 卸载DLL
*/
FreeLibrary(hIn);
}在打???号的那句出错,出错信息为:
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!Program: C:\Card\Debug\Card.exe
File: C:\Card\CardDlg.cpp
Line: 183For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.(Press Retry to debug the application)
---------------------------
终止(A) 重试(R) 忽略(I)
---------------------------到底为什么呢?
{
int fMin;
HINSTANCE hIn;
// 创建一个新的函数指针数据类型
typedef int(MINTYPE)(char*,char*);
MINTYPE*pMin=0;
// 载入FindMin.dll
VERIFY(hIn=::LoadLibrary("mydll.dll"));
// 取得FindMin()函数的地址
??? VERIFY(pMin=(MINTYPE*)::GetProcAddress((HMODULE)hIn, "mydll"));
// 测试动态链接库的FindMin()函数
/* char a[10];
char b[1024]; strcpy(a,"aaaaaaa");
strcpy(b,"this is start test"); fMin=(*pMin)(a,b);
// 显示测试结果
CString str;
str.Format("The minimum of %s and %s is %d", a,b,fMin);
AfxMessageBox(str);
// 卸载DLL
*/
FreeLibrary(hIn);
}在打???号的那句出错,出错信息为:
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!Program: C:\Card\Debug\Card.exe
File: C:\Card\CardDlg.cpp
Line: 183For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.(Press Retry to debug the application)
---------------------------
终止(A) 重试(R) 忽略(I)
---------------------------到底为什么呢?
typedef int (* MINTYPE)(char*,char*);
MINTYPE pMin
???那句
VERIFY(pMin=(MINTYPE)GetProcAddress(hIn, "mydll"));
所以C++的DLL一般都是静态调用的;
pMin=(MINTYPE)GetProcAddress(hIn, "mydll")这是动态调用方法只能调C风格的DLL.
当然,你如果有本事把DLL中_mydll@@48576@3479这种怪名字找出来,动态调用同样可以哦
make a def file:
LIBRARY MyDllEXPORTS
mydll
////////////