Solved

CWinApp

Posted on 2004-09-16
6
818 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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