CWinApp theApp; what's for?

i've frequently asked to myself why Visual C++ 6.0 Wizard for console applications includes always the statement:
//------------------------------------
// The one and only application object
CWinApp theApp;
//------------------------------------

It's to let to users the chance to call 'AfxGetApp()->.....', or there's a wiser reason?
pFaz69Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nil_dibCommented:
its the instance/object of your "main" application class (which is derived from CWinApp). no object, no running program.

nil_dib
0
pFaz69Author Commented:
My console program runs without problems without the code row above) if i don't use funcs like AfxGetApp()); so why Visual C++ Wizard includes always this row?
0
nil_dibCommented:
your console program has a main() function (!) which is called at the execution of the application.
The entry to your MFC application is the creation of an object from your "main" app class (runs as a thread(derived from CWinApp derived from CWinThread)).

nil_dib
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

mikeblasCommented:
A CWinApp is necessary because you're using MFC (or, at least, you've asked the console Wizard for MFC support).

It's there to maintain state information for the application and help MFC initialize.

If your application works fine without it, it's because you're not using MFC--or not using enough of MFC--to run into a problem.

..B ekiM
0
pFaz69Author Commented:
i'd like to know something more about that. My program is composed by a main() func (!) and five classes: three of them inherites from CWinThread and two from CAsyncSocket: it works pretty well without the 'CWinApp theApp' statement. So: i don't think i'm using little MFC. Maybe the job of 'CWinApp theApp' statement is performed by some other initializations; but witch is exactly this job?
0
abk102299Commented:
Hi,

I don't think the presence of the 'CWinApp theApp' object
is necessary in console applications. So there is probably no specific
job done by 'CWinApp theApp' except as storage of the unique
application specific information ( as instance handle, resource handle,
application name, help file path, etc. ) and availability of some
CWinApp methods that are still useful in console mode
( such as resource loading or profile management ). The later allows programmers
to write some program modules in an application mode insensitive way
( so they are portable from windows application to console application )
using the AfxGetApp() function for the information access.
So I think you are right in your initial guess. From other side
the 'CWinApp theApp' statement looks optional that is clear form
the MFC AfxWinInit source code following.
See the CWinApp::SetCurrentHandles() implementation for more info.

BOOL AFXAPI AfxWinInit(HINSTANCE hInstance, HINSTANCE hPrevInstance,
      LPTSTR lpCmdLine, int nCmdShow)
{
      ASSERT(hPrevInstance == NULL);

      // handle critical errors and avoid Windows message boxes
      SetErrorMode(SetErrorMode(0) |
            SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX);

      // set resource handles
      AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
      pModuleState->m_hCurrentInstanceHandle = hInstance;
      pModuleState->m_hCurrentResourceHandle = hInstance;

      // fill in the initial state for the application
      CWinApp* pApp = AfxGetApp();
      if (pApp != NULL)
      {
            // Windows specific initialization (not done if no CWinApp)
            pApp->m_hInstance = hInstance;
            pApp->m_hPrevInstance = hPrevInstance;
            pApp->m_lpCmdLine = lpCmdLine;
            pApp->m_nCmdShow = nCmdShow;
            pApp->SetCurrentHandles();
      }

      // initialize thread specific data (for main thread)
      if (!afxContextIsDLL)
            AfxInitThread();

      return TRUE;
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pFaz69Author Commented:
thank you, abk for your research.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.