Using MFC in Win32 Application and Win32 Console Application

zhaozj2021-02-08  280

Using MFC in Win32 Application and Win32 Console Application Win32 Application and Win32 Conso Application in Virtual C 6.0 is possible, the main difficulties are bypassing the Winmain function provided by the MFC. Below I offer a method for reference:

Enter Project -> Setting -> C / C Page, do the following modifications:

1. Join _afxdll in PreProcessor Definitions, which is probably like this:

Win32, _debug / nodebug, [_ console], [_ mbcs], _AFXDLL

Joined _afxdll is the key, it deceives MFC LIB to avoid connecting the MFC's WinMain function.

2. Modify Project Options, change the / mt or / ml flag to / md. The reason is that the _afxdl, _mt, _dll flag is set in AFXVER_.H, otherwise the error is reported. Although the LIBRARY of the Convei-Threaded version will lose some performance, the existence of this flag does not cause the compiler to compile Project into a DLL.

3. In the stdafx.h of Project, it contains the necessary header files, or copies directly from the STDAFX.H created from the MFC AppWizard:

#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers #include #include // MFC core and standard components #include // MFC extensions #include // MFC Support for Internet Explorer 4 Common Controls #ifNDEF _AFX_NO_AFXCMN_SUPPORT #InClude // MFC Support for Windows Common Controls #ENDIF / / / _AFX_NO_AFXCMN_SUPPORT

4. Add the initialization code of the MFC in Project's WinMain / Main, the following is the case of _twinmain and _tmain:

Extern "C" int winapi _twinmain (Hinstance Hinstance, Hinstance Hprevinstance, LPTSTSTR LPCMDLINE, INT NCMDSHOW) {INT NRETCODE = 0;

IF (! AFXWININIT (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;}

In addition, the Win32 Dynamic-Link Library created in Virtual C 6.0 can also be used separately, which avoids Project to add a CWINAPP instance by the MFC AppWizard and ATL COM AppWizard, and the method is simple, and the DLLMAIN function of Project is constructed:

#include

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.} ◆

转载请注明原文地址:https://www.9cbs.com/read-2847.html

New Post(0)