天狼鼠

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

实话,编写MFC规则库和扩展库和编写其他库没有什么区别.其实都一样.只不过, MFC规则库和扩展库对支持MFC的特性更好,你在写MFC规则库和扩展库的时候基本上可以和你写MFC应用程序一样.

那我们就随便写写吧.

首先建立一个MFC扩展的库吧.在这里我们导出一个函数调用前面的MFC静态苦的导出函数:

extern "C" __declspec(dllexport) void ShowDialog()
{
 typedef void ( *lpShowDialog)( );           //DLL里的函数原型
 
 HINSTANCE hInst = NULL;              //DLL的实例句柄,在WIN32中HINSTANCE和HMODULE可以互换使用
 lpShowDialog ShowDialog;             //函数定义

 hInst = LoadLibrary( "..\\MFCStatic\\Debug\\MFCStatic.dll" );   //导入DLL
 if ( !hInst ) return ;
 ShowDialog = (lpShowDialog)GetProcAddress( hInst, "ShowDialog" );
 if ( ShowDialog )
 {
  ShowDialog( );               //调用DLL里的函数
 }
 FreeLibrary( hInst );             //释放DLL
}

我们再建立一个MFC规则库,在这里我们在调用刚才建立的MFC扩展库的导出函数.

实现文件代码如下:

int CMFCDllApp::ExitInstance()
{
 // TODO: Add your specialized code here and/or call the base class
 //释放资源...
 return CWinApp::ExitInstance();
}

BOOL CMFCDllApp::InitInstance()
{
 // TODO: Add your specialized code here and/or call the base class
 /*---------------------------------*\
 一般我们在这里完成一些必要的初始化工
 作。
 \*---------------------------------*/
 
 //AfxEnableControlContainer(); //允许你使用一MFC写的控件(ActiveX)
 
 /*-----------------------------------------------------*\
 if ( !AfxSocketInit() )
 {
 AfxMessageBox( _T(" 初始化SOCKET环境失败!") );
 return FALSE;
 }
 \*-----------------------------------------------------*/

 /*-----------------------------------------------------*\
 if ( !AfxOleInit() )
 {
  AfxMessageBox( _T(" 初始化组件库失败!") );
  return FALSE;
 }
 \*-----------------------------------------------------*/
 //....
 return CWinApp::InitInstance();
}

extern "C" __declspec(dllexport) void ShowDialog()
{
 typedef void ( *lpShowDialog)( );           //DLL里的函数原型
 
 HINSTANCE hInst = NULL;              //DLL的实例句柄,在WIN32中HINSTANCE和HMODULE可以互换使用
 lpShowDialog ShowDialog;             //函数定义
 
 hInst = LoadLibrary( "..\\MFCEDll\\Debug\\MFCEDll.dll" );    //导入DLL
 if ( !hInst ) return ;
 ShowDialog = (lpShowDialog)GetProcAddress( hInst, "ShowDialog" );
 if ( ShowDialog )
 {
  ShowDialog( );               //调用DLL里的函数
 }
 FreeLibrary( hInst );             //释放DLL
}

最后,我们试着在外部调用他们:

void CDllDlg::OnBtnMfcdll()
{
 // TODO: Add your control notification handler code here
 typedef void ( *lpShowDialog)( );           //DLL里的函数原型
 
 HINSTANCE hInst = NULL;              //DLL的实例句柄,在WIN32中HINSTANCE和HMODULE可以互换使用
 lpShowDialog ShowDialog;             //函数定义
 
 hInst = AfxLoadLibrary( ".\\MFCDll\\Debug\\MFCDll.dll" );     //导入DLL
 if ( !hInst ) return ;
 ShowDialog = (lpShowDialog)GetProcAddress( hInst, "ShowDialog" );
 if ( ShowDialog )
 {
  ShowDialog( );               //调用DLL里的函数
 }
 AfxFreeLibrary( hInst );             //释放DLL
}

void CDllDlg::OnBtnMfcedll()
{
 // TODO: Add your control notification handler code here
 typedef void ( *lpShowDialog)( );           //DLL里的函数原型
 
 HINSTANCE hInst = NULL;              //DLL的实例句柄,在WIN32中HINSTANCE和HMODULE可以互换使用
 lpShowDialog ShowDialog;             //函数定义
 
 hInst = AfxLoadLibrary( "..\\MFCEDll\\Debug\\MFCEDll.dll" );    //导入DLL
 if ( !hInst ) return ;
 ShowDialog = (lpShowDialog)GetProcAddress( hInst, "ShowDialog" );
 if ( ShowDialog )
 {
  ShowDialog( );               //调用DLL里的函数
 }
 AfxFreeLibrary( hInst );             //释放DLL
}


到这里基本上DLL都写完了.不过一些基本理论并没有讲,自己查资料嘛!

posted on 2011-03-20 16:55  情有独钟  阅读(333)  评论(0编辑  收藏  举报