Why CWinApp in a Win32 console
-
Hi, i'm currious, I'm writing a winnt service application. So I start of with a basic win32 console application. There is not a CWinApp in the main file just the _tmain function.
#include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { return 0; }
Then I remembered that i need some MFC classes or at least the support for it. I recreate the project, but now i check out the option to include standard MCF headers. Now i get:
CWinApp theApp; using namespace std; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed\n")); nRetCode = 1; } else { // TODO: code your application's behavior here. } return nRetCode; }
Why is this? The CWinApp object theApp is never used. What does it do? Any insights would be appreciated.
Learn from the mistakes of others, you may not live long enough to make them all yourself.
-
Hi, i'm currious, I'm writing a winnt service application. So I start of with a basic win32 console application. There is not a CWinApp in the main file just the _tmain function.
#include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { return 0; }
Then I remembered that i need some MFC classes or at least the support for it. I recreate the project, but now i check out the option to include standard MCF headers. Now i get:
CWinApp theApp; using namespace std; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed\n")); nRetCode = 1; } else { // TODO: code your application's behavior here. } return nRetCode; }
Why is this? The CWinApp object theApp is never used. What does it do? Any insights would be appreciated.
Learn from the mistakes of others, you may not live long enough to make them all yourself.
BadKarma wrote:
The CWinApp object theApp is never used
Not strictly true. It's a global variable, which means its constructor will be called before
_tmain
is executed.CWinApp::CWinApp
does a whole load of MFC initialisations for you, that will then be used by other MFC functions that you call (or are called for you by the framework). -
Hi, i'm currious, I'm writing a winnt service application. So I start of with a basic win32 console application. There is not a CWinApp in the main file just the _tmain function.
#include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { return 0; }
Then I remembered that i need some MFC classes or at least the support for it. I recreate the project, but now i check out the option to include standard MCF headers. Now i get:
CWinApp theApp; using namespace std; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed\n")); nRetCode = 1; } else { // TODO: code your application's behavior here. } return nRetCode; }
Why is this? The CWinApp object theApp is never used. What does it do? Any insights would be appreciated.
Learn from the mistakes of others, you may not live long enough to make them all yourself.
BadKarma wrote:
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
BadKarma wrote:
Why is this? The CWinApp object theApp is never used. What does it do?
Look up
CWinApp
constructor and then step intoAfxWinInit
, you will see that it needs aCWinApp
object, see the callAfxGetApp()
.AfxGetApp
is able to get an application pointer due the following line in theCWinApp
constructor.pModuleState->m_pCurrentWinApp = this;
Some MFC specific initialization is also done in these functions and stored in thread local storage. This happens in AfxTls.cpp line number 100, comes via AfxClassInit().Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
modified on Thursday, June 12, 2008 8:51 AM