在 Win32 Application 中使用 MFC

简介: 在 Virtual C++ 6.0 创建的 Win32 Application 和 Win32 Console Application 中使用 MFC 是可能的,主要的困难在于绕过 MFC 提供的 WinMain 函数。

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.

}

 


注: 有网友指出 VC7 中, 需要将第一步的 _AFXDLL 改成 __PROF 则同样可以编译。

目录
相关文章
|
3月前
|
C++
MFC -- 网络通信编程
MFC -- 网络通信编程
13 0
|
12月前
|
Windows
win32&mfc————win32消息机制(二)
win32&mfc————win32消息机制(二)
141 0
|
12月前
|
Windows
win32&mfc————win32消息机制(一)
win32&mfc————win32消息机制(一)
119 0
|
C++
MFC -- 网络通信编程
MFC -- 网络通信编程
71 0
MFC -- 网络通信编程
|
Windows
【错误记录】Windows 控制台程序编译报错 ( WINDOWS.H already included. MFC apps must not #include <Windows.h> )
【错误记录】Windows 控制台程序编译报错 ( WINDOWS.H already included. MFC apps must not #include <Windows.h> )
777 0
【错误记录】Windows 控制台程序编译报错 ( WINDOWS.H already included. MFC apps must not #include <Windows.h> )
解决Visual Studio 2005中找不到MFC80UD.dll的问题
今天用VS2005建立一个MFC项目,在运行时遇到下面这个找不到MFC80UD.dll的问题。 这个问题可能是Manifest 引起的,因此我们可以通过修改项目->属性->清单工具->输入输出,把“嵌入清单”选“否”,然后编译、链接、运行即可。
1199 0