說實話,編寫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都寫完了.不過一些基本理論並沒有講,自己查資料嘛!