MFC APP中使用MFC DLL(用C++)
先寫DLL檔案:
選擇MFC擴充套件DLL。
在.def檔案中加入:
; mfcDBstore_1.def : Declares the module parameters for the DLL.
LIBRARY "mfcDBstore_1"
DESCRIPTION 'mfcDBstore_1 Windows Dynamic Link Library'
EXPORTS
; Explicit exports can go here
mfcDBstore; //這個是需要匯出的名字
然後再CPP檔案中加入:
void APIENTRY mfcDBstore(); //extern "C" void APIENTRY Msg(); //去掉extern,以C++的方式編譯
然後:
class A //寫自己需要的類
{
public:
A()
{
AfxMessageBox("A()");
}
~A()
{
AfxMessageBox("~A()");
}
};
void APIENTRY mfcDBstore() //建立物件
{
AfxMessageBox("click mouse mfcDBstore");
A a;
}
至此,DLL編寫完。開始編寫MFC 單文件程式:
選擇類檢視,新增訊息處理函式,選擇滑鼠左鍵按下。
新增程式碼:
void CMfcDBstore_2View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
FARPROC lpfn; //定義函式地址
HINSTANCE hinst; //定義控制代碼
hinst=LoadLibrary("C:/Program Files/Microsoft Visual Studio/MyProjects/DLLspace2/mfcDBstore_1/Debug/mfcDBstore_1.dll");//載入DLL
if(hinst==NULL) //載入失敗
{
AfxMessageBox("載入DLL失敗!");
return;//返回
}
lpfn=GetProcAddress(hinst,"mfcDBstore");//取得DLL匯出函式的地址
if (lpfn==NULL)
{
AfxMessageBox("讀取函式地址失敗!");
return;//返回
}
lpfn();//執行DLL函式
FreeLibrary(hinst);//釋放DLLShareMFCDill.dll
CView::OnLButtonDown(nFlags, point);
}
嗯。按下滑鼠就可以看到 預設的構造和析構被呼叫。