Solved

CWinApp

Posted on 2004-09-16
6
821 Views
Last Modified: 2013-11-20
Hi all,
  Is it safe to derive two different classes from CWinApp in the same application???

0
Comment
Question by:struggling_coder_3203
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12079409
What for? It is not usual and highly non-recommended by Microsoft documentation.
You can derive another CWinThread object instead, or you can create 2 different application projects with some inter-communication.
0
 

Author Comment

by:struggling_coder_3203
ID: 12079651
I know that the circumstance would be very odd in which to do so, but lets just say that I cannot get a pointer to the one and only app, and I want to use the functionality of the CWinApp class. Could it cause many problems???

p
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12081758
AfxGetApp returns a pointer to the CWinApp - you then cast it to your app to get at any functions/variables in your app's class.
eg.

CMyApp* pApp = (CMyApp*)AfxGetApp();
pApp->DoMyFunction();


Your app has only one running instance, why could you require two different CWinApp based classes.  If you want common functionality between two different app you could do

class CMyAppBase : public CWinApp
{
void SomeCommonFunction();
....

class CMyAppThis : public CMyAppBase
class CMyAppTheOther : public CMyAppBase

then you just have the files for CMyAppBase shared between two projects.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:tsaditya
ID: 12082573
The CWinApp class is the base class from which you derive a Windows application object. An application object provides member functions for initializing your application (and each instance of it) and for running the application.

Each application that uses the Microsoft Foundation classes can only contain one object derived from CWinApp. This object is constructed when other C++ global objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the Microsoft Foundation Class Library. Declare your derived CWinApp object at the global level.

When you derive an application class from CWinApp, override the InitInstance member function to create your application's main window object.

CWinApp is designed to make sure that only application object is present in a given instance. CWinApp and its descendants are called Singleton Classes.


0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12090617
You can derive any number of classes from CWinApp class. The problem is that you can create only one CWinApp or CWinApp-derived variable. Once you try to create another CWinApp instance, you get large number of assertions, exceptions etc. when program is initialized.

tsaditya - CWinApp is not defined as singleton  by classic C++ way (private constructor and CreateInstance function). We can create MFC application and add second instance of CWinApp class, it is successfully compiled and linked:

CTestApp theApp;
CTestApp theApp1;

However, such program cannot be executed successfully, so we can think that CWinApp is actually singleton.

struggling_coder_3203 - possibly you need to describe your initial problem. The way you are trying to solve it is very difficult, I beleive there is another way.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 12105677
I guess it's

AFX_MANAGE_STATE(AfxGetStaticModuleState( ));

that is missing - if that happens in a DLL.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question