How to access Member functions of Application class
-
I have created a Dialog based application using appwizard. now, i want to access a user defined function in the application class from my dialog class. how can i do that? Shenthil
the application class object is taken global named theApp which you can see in your global list simple extern that variable in the file where u want to use that variable then call the user defined functions using the theApp object
-
I have created a Dialog based application using appwizard. now, i want to access a user defined function in the application class from my dialog class. how can i do that? Shenthil
-
I have created a Dialog based application using appwizard. now, i want to access a user defined function in the application class from my dialog class. how can i do that? Shenthil
Like the MFC Reference states, your application class is derived from CWinApp. There is a global MFC function
AfxGetApp
which returns a pointer to your class's parent CWinApp. You can use the below method to access the class. Here, we assume that your the implementation of your dialog class (.cpp file) #includes the application class's header file. This is usually done for you when you create an application, but make sure just in case. If you have not included it, you will receive errors like 'Unknown variable'// Achieve a pointer to the application class
CMyApp* ptrApp = DYNAMIC_DOWNCAST( CMyApp, AfxGetApp() );if ( !ptrApp )
TRACE0("Failed to get application pointer\n");// Use the pointer
UINT uiReturnValue = ptrApp->MyFunction(PARAM param1, PARAM param2);The
DYNAMIC_DOWNCAST
macro does a safe downcast fromCWinApp*
returned byAfxGetApp
into theCMyApp*
type. As your application class is derived fromCWinApp
, the cast is perfectly legal and will yeild a pointer to your derived class. You can use a similar method with any MFC class, for example, you can useGetParentFrame
method of getting a view's parent frame (CFrameWnd*
) then cast it down to your derivedCMyFrameWindow
class. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.