在vc6里面一般都沒(méi)有這個(gè)頭文件的,在后面的版本(.NET)都有相關(guān)的說(shuō)明,不過(guò)這個(gè)是CString包含的頭文件,我之前將open source的代碼移植到win32 application平臺(tái)下面,對(duì)方的編譯器是vc2003,而我的是用到了vc6,所以就遇到這個(gè)問(wèn)題了,解決辦法有兩種:
1)在工程里面加入MFCsupport,因?yàn)镃String是MFC里面的內(nèi)容。具體做法是project/settings/General/microsoft foundation class處選擇using MFC as a shared dll./ static library.
在msdn上面有具體的說(shuō)明。
可能看起來(lái)比較麻煩,不過(guò)不好意思,就這樣弄過(guò)來(lái)防止msdn上面這個(gè)文章過(guò)舊了,又被刪除了。
給 ATL EXE 項(xiàng)目添加 MFC 支持
1. |
在包括 Atlbase.h 之前,將以下 #include 指令添加到 StdAfx.h:
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation extensions
|
2. |
更改項(xiàng)目設(shè)置以使用 MFC。 在 Project Settings 對(duì)話框中,單擊 General 選項(xiàng)卡,然后將 Microsoft Foundation Classes 列表框中的設(shè)置更改為 MFC。 |
3. |
添加 CWinApp 衍生類(lèi),并聲明一個(gè)該類(lèi)型的全局變量,如下所示:
class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
protected:
BOOL m_bRun;
};
|
4. |
用以下 InitInstance 和 ExitInstance 代碼替換 _tWinMain 函數(shù):
BOOL CMyApp::InitInstance()
{
// Initialize OLE libraries.
if (!AfxOleInit())
{
AfxMessageBox(_T("OLE Initialization Failed!"));
return FALSE;
}
// Initialize CcomModule.
_Module.Init(ObjectMap, m_hInstance);
_Module.dwThreadID = GetCurrentThreadId();
// Check command line arguments.
TCHAR szTokens[] = _T("-/");
m_bRun = TRUE;
LPCTSTR lpszToken = FindOneOf(m_lpCmdLine, szTokens);
while (lpszToken != NULL)
{
// Register ATL and MFC class factories.
if (lstrcmpi(lpszToken, _T("Embedding"))==0 ||
lstrcmpi(lpszToken, _T("Automation"))==0)
{
AfxOleSetUserCtrl(FALSE);
break;
}
// Unregister servers.
// There is no unregistration code for MFC
// servers. Refer to <LINK TYPE="ARTICLE" VALUE="Q186212">Q186212</LINK> "HOWTO: Unregister MFC
// Automation Servers" for adding unregistration
// code.
else if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
{
VERIFY(SUCCEEDED(_Module.UpdateRegistryFromResource(IDR_ServerS2B, FALSE)));
VERIFY(SUCCEEDED(_Module.UnregisterServer(TRUE)));
m_bRun = FALSE;
break;
}
// Register ATL and MFC objects in the registry.
else if (lstrcmpi(lpszToken, _T("RegServer"))==0)
{
VERIFY(SUCCEEDED(_Module.UpdateRegistryFromResource(IDR_ServerS2B, TRUE)));
VERIFY(SUCCEEDED(_Module.RegisterServer(TRUE)));
COleObjectFactory::UpdateRegistryAll();
m_bRun = FALSE;
break;
}
lpszToken = FindOneOf(lpszToken, szTokens);
}
if (m_bRun)
{
// Comment out the next line if not using VC 6-generated
// code.
_Module.StartMonitor();
VERIFY(SUCCEEDED(_Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE)));
VERIFY(COleObjectFactory::RegisterAll());
// To run the EXE standalone, you need to create a window
// and assign the CWnd* to m_pMainWnd.
LPCTSTR szClass = AfxRegisterWndClass(NULL);
m_pMainWnd = new CWnd;
m_pMainWnd->CreateEx(0, szClass, _T("SomeName"), 0, CRect(0, 0, 0, 0), NULL, 1234);
}
return TRUE;
}
int CMyApp::ExitInstance()
{
// MFC's class factories registration is
// automatically revoked by MFC itself.
if (m_bRun)
{
_Module.RevokeClassObjects();
Sleep(dwPause); //wait for any threads to finish
}
_Module.Term();
return 0;
}
|
5. |
對(duì)于 Unicode 版本,請(qǐng)確保進(jìn)入點(diǎn)被設(shè)置為 wWinMainCRTStartup,該設(shè)置在 Project Settings 對(duì)話框中 Link 字段的 Output 類(lèi)別中。 有關(guān)其它信息,請(qǐng)參見(jiàn) Microsoft Knowledge Base 中的下列文章:
125750 (http://support.microsoft.com/kb/125750/EN-US/) PRB: 錯(cuò)誤 LNK2001: “_WinMain@16”: 不能解析的外部符號(hào)
|
6. |
將以下代碼行添加到 COM 接口、窗口過(guò)程和導(dǎo)出函數(shù)的每個(gè)成員函數(shù)的開(kāi)頭:
AFX_MANAGE_STATE(AfxGetAppModuleState());
有關(guān) AFX_MANAGE_STATE 的詳細(xì)信息,請(qǐng)查詢 VC++ 聯(lián)機(jī)文檔。 |
有關(guān)將 MFC 支持添加到 ATL COM AppWizard 項(xiàng)目的詳細(xì)信息,請(qǐng)參見(jiàn)下面的 Microsoft Knowledge Base 文章:
181505 (http://support.microsoft.com/kb/181505/EN-US/) PRB: ATL COM AppWizard 不提供對(duì) .EXE 的 MFC 支持
回到頂端
將 MFC 支持添加到 ATL DLL 項(xiàng)目
執(zhí)行上面的步驟 1 到步驟 3。
1. |
將 AppWizard 生成的 DllMain 的 DLL_PROCESS_ATTACH 和 DLL_PROCESS_DETACH 中的代碼移到 CMyApp 的 InitInstance 和 ExitInstance,并刪除 DllMain,如下所示:
BOOL CMyApp::InitInstance()
{
_Module.Init(ObjectMap, m_hInstance);
return CWinApp::InitInstance();
}
int CMyApp::ExitInstance()
{
// MFC's class factories registration is
// automatically revoked by MFC itself.
if (m_bRun)
_Module.RevokeClassObjects();
|
2. |
將以下代碼行添加到 COM 接口、窗口過(guò)程和導(dǎo)出函數(shù)的每個(gè)成員函數(shù)的開(kāi)頭:
AFX_MANAGE_STATE(AfxGetStaticModuleState());
有關(guān)其它信息,請(qǐng)參見(jiàn) Microsoft Knowledge Base 中的下列文章:
140850 (http://support.microsoft.com/kb/140850/EN-US/) HOWTO: 轉(zhuǎn)換 DLLTRACE 以使用共享庫(kù)中的 MFC
|
另外一種辦法就是將atlstr廢掉,采用其他辦法
因?yàn)閍tlstr實(shí)際起作用的是CString,而如果能夠找到CString的替代方案,就可以了。
替代方案在這里:
http://www.codeguru.com/forum/showthread.php?t=402543一般建議用std::string來(lái)完成這些不依賴微軟某種技術(shù)的做法。
在我的項(xiàng)目里面,因?yàn)椴捎玫氖莣in32 application工程,并且該項(xiàng)目已經(jīng)很大了,不想因?yàn)檫@個(gè)而在那里增加一些方案1的處理,我采用了方案2。