1、MFC动态库,可以插入对话框资源的动态库
创建:
创建一个MFC AppWizard(dll)工程,选择Regular Dll using shared MFC DLL,创建一般的动态连接的MFC动态库。命名为MyDll
添加一个对话框资源,并添加关联的类,类名命名为 DllDlg
在MyDll.h文件中,添加非MFC函数,extern "C" _declspec (dllexport) void ShowDlg();
在对应的MyDll.cpp中 实现该函数
#include "DllDlg.h"
extern "C" _declspec (dllexport) void ShowDlg()
{
AFX_MANAGE_STATE(::AfxGetStaticModuleState());//这句是关键,用于资源转换,否则无法调用对话框资源或其他资源。
CDllDlg dlg;
int nRet = dlg.DoModal();
}
编译生成MyDll.dll文件。
动态调用:
包含头文件
#include "MyDll.h"
HINSTANCE hInst = ::LoadLibrary(L"storage card//emulatorDbg//zjLCD1.dll");
if(!hInst){
MessageBox(L"加载库失败!");
return;
}
typedef void(*ShowDlgFun)();
ShowDlgFun pShowDlg= (ShowDlgFun)::GetProcAddress(hInst,L"ShowDlg");
if(!Add){
CString str;
str.Format(L"获得函数地址失败!err=%d",::GetLastError());
MessageBox(str);
}
pShowDlg();//执行函数
::FreeLibrary(hInst);
调用结束。