Solved

CWinApp

Posted on 2004-09-16
6
805 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Expert Comment

by:tsaditya
Comment Utility
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
Comment Utility
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
Comment Utility
I guess it's

AFX_MANAGE_STATE(AfxGetStaticModuleState( ));

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
has77  challenge 9 67
Use of condition with 'serial' in ansible 2 36
mapAB Challlenge 35 84
Detect file exist or not 3 76
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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

13 Experts available now in Live!

Get 1:1 Help Now