在 Win32 Application 和 Win32 Console Application 中使用 MFC
2012-06-02 14:22
441 查看
在 Win32 Application
和 Win32 Console Application 中使用 MFC
在Virtual C++ 6.0创建的Win32 Application 和 Win32 Console Application 中使用 MFC 是可能的,主要的困难在于绕过MFC提供的WinMain函数。下面我提供一个方法以供参考:
进入 Project-->Setting--> C/C++ Page,做以下修改:
1. 在Preprocessor definitions中加入_AFXDLL,加入后的设置大概是这样的:
WIN32,_DEBUG / NODEBUG,[_CONSOLE],[_MBCS],_AFXDLL
加入的_AFXDLL是关键 ,它欺骗MFC LIB,避免连接 MFC 的 WinMain 函数。
2. 修改Project Options,将 /MT或者 /ML标志改为 /MD。
原因是在 afxver_.h 中会检查_AFXDL, _MT, _DLL 标志是否同时设置,否则报错。尽管链接 For Multi-Threaded 版本的 Library 会损失一些性能,但是这个标志的存在并不导致编译器把 Project 编译成 DLL。
3. 在Project的 stdafx.h 中包含必要的头文件,或者直接从MFC AppWizard创建的stdafx.h中拷贝:
4. 在Project的WinMain / main中加入MFC的初始化代码,以下是_tWinMain和_tmain的情况:
此外,在Virtual C++ 6.0创建的Win32 Dynamic-Link Library中也可以单独使用MFC,这样可以避免Project被MFC AppWizard和ATL COM AppWizard添加CWinApp实例,方法简单,如下构造Project的DllMain函数即可:
和 Win32 Console Application 中使用 MFC
在Virtual C++ 6.0创建的Win32 Application 和 Win32 Console Application 中使用 MFC 是可能的,主要的困难在于绕过MFC提供的WinMain函数。下面我提供一个方法以供参考:
进入 Project-->Setting--> C/C++ Page,做以下修改:
1. 在Preprocessor definitions中加入_AFXDLL,加入后的设置大概是这样的:
WIN32,_DEBUG / NODEBUG,[_CONSOLE],[_MBCS],_AFXDLL
加入的_AFXDLL是关键 ,它欺骗MFC LIB,避免连接 MFC 的 WinMain 函数。
2. 修改Project Options,将 /MT或者 /ML标志改为 /MD。
原因是在 afxver_.h 中会检查_AFXDL, _MT, _DLL 标志是否同时设置,否则报错。尽管链接 For Multi-Threaded 版本的 Library 会损失一些性能,但是这个标志的存在并不导致编译器把 Project 编译成 DLL。
3. 在Project的 stdafx.h 中包含必要的头文件,或者直接从MFC AppWizard创建的stdafx.h中拷贝:
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers #include <afx.h> #include <afxwin.h> // MFC core and standard components #include <afxext.h> // MFC extensions #include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls #ifndef _AFX_NO_AFXCMN_SUPPORT #include <afxcmn.h> // MFC support for Windows Common Controls #endif // _AFX_NO_AFXCMN_SUPPORT
4. 在Project的WinMain / main中加入MFC的初始化代码,以下是_tWinMain和_tmain的情况:
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { int nRetCode = 0; if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { TRACE0("Fatal Error: MFC initialization failed./n"); nRetCode = 1; } else { // Actual WinMain codes ... AfxWinTerm(); } return nRetCode; } int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { cerr << _T("Fatal Error: MFC initialization failed") << endl; nRetCode = 1; } else { // Actual main codes ... AfxWinTerm(); } return nRetCode; }
此外,在Virtual C++ 6.0创建的Win32 Dynamic-Link Library中也可以单独使用MFC,这样可以避免Project被MFC AppWizard和ATL COM AppWizard添加CWinApp实例,方法简单,如下构造Project的DllMain函数即可:
#include <afxdllx.h> static AFX_EXTENSION_MODULE ProjectDLL = { NULL, NULL }; extern "C" int APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) { // Remove this if you use lpReserved. UNREFERENCED_PARAMETER(lpReserved); if (dwReason == DLL_PROCESS_ATTACH) { // Extension DLL one-time initialization. if (!AfxInitExtensionModule(ProjectDLL, hInstance)) { TRACE0("Project.DLL initialize its extension module failed!/n"); return FALSE; } // CDynLinkLibrary’s destructor will be called in AfxTermExtensionModule. new CDynLinkLibrary(ProjectDLL); } else if (dwReason == DLL_PROCESS_DETACH) { TRACE0("Project.DLL terminating.../n"); // Terminate the library before destructors are called. AfxTermExtensionModule(ProjectDLL); } return TRUE; // ok. }
相关文章推荐
- 在 Win32 Application 和 Win32 Console Application 中使用 MFC
- 在 Win32 Application 和 Win32 Console Application 中使用 MFC
- 如何在win32 Application 中使用MFC
- 在 Win32 Application 和 Win32 Console Application 中使用 MFC
- 在 Win32 Application 和 Win32 Console Application 中使用 MFC
- Windows Mobile下使用Native C++(WTL, MFC, Win32)开发,如何为对话框加入菜单
- win32和MFC下如何使用控制台窗口
- VS IDE环境下,windows GUI(Qt MFC,win32)使用控制台实时打印调试信息
- win32和MFC使用临界区区别
- Win32 Console Application、Win32 Application、MFC三者之间的联系和区别
- 在Win32应用程序中使用MFC类库
- win32和MFC下如何使用控制台窗口
- 在Win32应用程序中使用MFC类库
- 如何在Win32 Console Application (Win32控制台)程序中使用MFC
- 关于OpenFileDialog在MFC和OPENFILENAME在win32中的使用
- (GDI+) 如何在 MFC 或 Win32 应用程序中使用GDI+
- Win32 Console Application、Win32 Application、MFC三者之间的联系和区别
- Win32 Application 、Win32 Console Application、MFC工程项目
- MFC调用win32窗口显示调试信息,使用AllocConsole 函数
- [技术学习]使用 wine 在Unix平台进行 WIN32/MFC 开发