.cpp,VC 預設僅對 .cpp 啟用 MFC 支援。stdafx.h 中正確包含 <afx.h> 並調整運行時庫為多執行緒版本。修改 stdafx.h,確保 afx.h 在 windows.h 之前包含:
// stdafx.h
#if !defined(AFX_STDAFX_H__...)
#define AFX_STDAFX_H__...
#if _MSC_VER > 1000
#pragma once
#endif
#define WIN32_LEAN_AND_MEAN
#include <afx.h> // 必須在 windows.h 之前
#include <windows.h>
// C Runtime Headers
#include <stdlib.h> <malloc.h> <memory.h> <tchar.h>
#ifdef _DEBUG
#pragma comment(lib, "libcmtd.lib")
#else
#pragma comment(lib, "libcmt.lib")
#endif
#endif
即可在程式中正常使用 CString。
afx.h 預設帶 DllMain,需額外步驟:
#pragma comment(lib,...))。DLLMODUL.CPP 並加入專案,內容:#include "stdafx.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define new DEBUG_NEW
// global data
#ifdef _X86_
extern "C" { int _afxForceUSRDLL; }
#else
extern "C" { int __afxForceUSRDLL; }
#endif
stdafx.h 中同樣將 afx.h 置於 windows.h 前。完成後即可在 DLL 中使用 CString。
afx.h → windows.hlibcmtd.lib / 發布 libcmt.libDLLMODUL.CPP 強制連結 MFC 狀態