Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why CWinApp in a Win32 console

Why CWinApp in a Win32 console

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BadKarma
    wrote on last edited by
    #1

    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.

    G N 2 Replies Last reply
    0
    • B BadKarma

      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.

      G Offline
      G Offline
      Graham Bradshaw
      wrote on last edited by
      #2

      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).

      1 Reply Last reply
      0
      • B BadKarma

        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.

        N Offline
        N Offline
        Nibu babu thomas
        wrote on last edited by
        #3

        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 into AfxWinInit, you will see that it needs a CWinApp object, see the call AfxGetApp(). AfxGetApp is able to get an application pointer due the following line in the CWinApp 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups