Solved

CWinApp

Posted on 2004-09-16
6
814 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 92
ORA-01403: no data found 43 98
post4 challenge 28 99
zeroFront challenge 7 95
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now