用VC 的 AppWizard生成了一个mfcappwizard(dll),想连入vc的程序中去。我是这么作的:
一、用AppWizard生成了一个dll,(是MFC Appwizard(dll)),命名为MyDll
1、在MyDll.cpp中添加函数://系统是把他作为全局的函数好像
void DllTest(void)
{
AfxMessageBox("this is a dll function");
} 2、在MyDll.def中添加
DllTest @1二、建立基于对话框的一个应用程序UseDll,添加一个按钮, 1、在UserDllDlg.cpp中定义变量如下: HINSTANCE dll_handle=NULL;
typedef void (*DLLTEST)(void);
DLLTEST DllTest;//这两个变量是作为全局变量定义在类的外部 2、在OnButton()中添加代码:想显示链接dll并调用DllTest()函数,添加的代码如下: Dll_handle=LoadLibrary("...\...\...\Mydll.dll");//dll文件的路径
if(Dll_handle==NULL)
{
AfxMessageBox("dll has not be loaded !");
return;
}
DllTest=(DLLTEST)GetProcAddress(Dll_handle,"DllTest");
if(DllTest==NULL)
{
AfxMessageBox("dll function has not be loaded !");
return;
}
AfxMessageBox("begin to use function");
DllTest();
AfxMessageBox("end of use function");
FreeLibrary(Dll_handle);在程序中执行到DllTest()时,系统就提示Debug Assertion Failed!
Program F:\........\Dlluser.exe
File: wincore.cpp
Line: 884不知道是什么原因。我曾经尝试将MyDll的函数定义改为: extern "C" _declspec(dllexport)
void DllTest()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
AfxMessageBox("this is a dll function");
}
或extern "C" void PASCAL EXPORT DllTest(void)
或extern "c" AFX_API_EXPORT void DllTest(void)
但是都没有用请问如何解决?
一、用AppWizard生成了一个dll,(是MFC Appwizard(dll)),命名为MyDll
1、在MyDll.cpp中添加函数://系统是把他作为全局的函数好像
void DllTest(void)
{
AfxMessageBox("this is a dll function");
} 2、在MyDll.def中添加
DllTest @1二、建立基于对话框的一个应用程序UseDll,添加一个按钮, 1、在UserDllDlg.cpp中定义变量如下: HINSTANCE dll_handle=NULL;
typedef void (*DLLTEST)(void);
DLLTEST DllTest;//这两个变量是作为全局变量定义在类的外部 2、在OnButton()中添加代码:想显示链接dll并调用DllTest()函数,添加的代码如下: Dll_handle=LoadLibrary("...\...\...\Mydll.dll");//dll文件的路径
if(Dll_handle==NULL)
{
AfxMessageBox("dll has not be loaded !");
return;
}
DllTest=(DLLTEST)GetProcAddress(Dll_handle,"DllTest");
if(DllTest==NULL)
{
AfxMessageBox("dll function has not be loaded !");
return;
}
AfxMessageBox("begin to use function");
DllTest();
AfxMessageBox("end of use function");
FreeLibrary(Dll_handle);在程序中执行到DllTest()时,系统就提示Debug Assertion Failed!
Program F:\........\Dlluser.exe
File: wincore.cpp
Line: 884不知道是什么原因。我曾经尝试将MyDll的函数定义改为: extern "C" _declspec(dllexport)
void DllTest()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
AfxMessageBox("this is a dll function");
}
或extern "C" void PASCAL EXPORT DllTest(void)
或extern "c" AFX_API_EXPORT void DllTest(void)
但是都没有用请问如何解决?
extern "C" _declspec(dllexport)
void DllTest()
{
}
在这种情况下,你的UserDllDlg.cpp中关于此函数的申明也要改一下:
改为
typedef void (__cdecl *DLLTEST)(void);
LoadLibrary("Mydll.dll");//dll文件的路径你把dll与exe放在同一文件夹内~