静态库 动态库

来源:互联网 发布:mysql 删除某一字段 编辑:程序博客网 时间:2024/04/29 11:40

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);

 

调用结束。

原创粉丝点击