每次都為dll書(shū)寫(xiě)煩惱,書(shū)寫(xiě)模式其實(shí)是固定的,下面就一步一步完全實(shí)現(xiàn)一個(gè)動(dòng)態(tài)庫(kù)。
1、選擇MFC AppWizard(dll),項(xiàng)目名Demo,默認(rèn)完成
2、新建一h文件DemoExp.h,導(dǎo)出模塊所有接口,新建一cpp實(shí)現(xiàn)接口
3、在DemoExp.h中:
// 下面兩行防止重復(fù)包含
#ifndef?_DEMOEXP_H???(注:可以是任意的)
#define?_DEMOEXP_H
// 下面一行預(yù)定義,在編譯的時(shí)候定義_DEMOEXP把函數(shù)導(dǎo)出,調(diào)用的時(shí)候沒(méi)有定義則導(dǎo)出函數(shù)
#ifdef _DEMOEXP???(注:可以是任意的)
#define DEMOEXPAPI __declspec(dllexport)
#else
#define DEMOEXPAPI __declspec(dllimport)
#endif
// 導(dǎo)出函數(shù)接口
DEMOEXPAPI int ShowDlg();
// 調(diào)用的時(shí)候沒(méi)有定義則自動(dòng)連接
#ifndef _DEMOEXP???(注:和上面一樣)
?#pragma comment(lib, "Demo.lib")
#endif
#endif
4、在DemoExp.cpp中先空實(shí)現(xiàn):
#include "StdAfx.h"
#include "DemoExp.h"
DEMOEXPAPI int ShowDlg()
{
?return 0;
}
5、添加一對(duì)話(huà)框,并生產(chǎn)類(lèi)CDemoDlg類(lèi)
6、在DemoExp.cpp中:
#include "DemoDlg.h"
修改函數(shù)如下:
DEMOEXPAPI int ShowDlg()
{
?// 切換資源模塊
?AFX_MANAGE_STATE(AfxGetStaticModuleState());
?CDemoDlg?dlg;
?dlg.DoModal();
?return 0;
}
7、設(shè)置Setting->c/c++->Preprocessor definitions:添加,_DEMOEXP;stdafx.h中添加#include "Resource.h"
8、編譯即生產(chǎn)dll
9、添加一對(duì)話(huà)框程序測(cè)試,具體見(jiàn)示例代碼。
posted on 2006-02-13 23:32
萬(wàn)連文 閱讀(955)
評(píng)論(2) 編輯 收藏 引用 所屬分類(lèi):
MFC