• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1496
  • Last Modified:

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?
0
pFaz69
Asked:
pFaz69
1 Solution
 
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
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
 
pFaz69Author Commented:
thank you, abk for your research.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now