AfxGetApp problem

Feb 1, 2012 at 1:13pm
Hi,

an MFC question. Within my CMainFrame class I want to access my App class methods. I've tried the following code:

CPOSAnywhereClientNTApp *app = dynamic_cast<CPOSAnywhereClientNTApp*>(::AfxGetApp());

IPAddress commandLineIPAddress = app->getTargetIPAddress();

if (!commandLineIPAddress.isNull())
{
// do something
}

I should get back a value from getTargetIPAddress() but I'm not. Does the code above make sense? Is the <CPOSAnywhereClientNTApp> legal? Any help would be much appreciated. Thanks.
Feb 1, 2012 at 7:16pm
Since I continue to be NOT the MFC guy, I have refrained from answering, but since nobody else seems to have an answer for you, I'll give it a shot.

The dynamic_cast operator works using RTTI. I have heard some compilers don't properly implement this, so it might be your case. Watch out. I only use the Microsoft compiler and it works beautifully, so don't ask me if I know of any others that might not work correctly. :-(

Having said that and assuming your compiler correctly implements RTTI, the variable app should receive either a valid pointer or NULL if the returned object ISN'T of type CPOSAnywhereClientNTApp. You need to add a check in your code for this.

Now, your IP address call: If the object is not of the correct type, that line should just crash the program unless the method getTargetIPAddress() doesn't use any member data. Does it use member data? If it uses member data AND it is not crashing, I think it is safe to say that dynamic_cast is properly doing its job; else the possible problems are:

1. The pointer in app is not of the correct type and the app doesn't crash because getTargetIPAddress() doesn't access member data.
2. The pointer in app is of the correct type but the logic inside getTargetIPAddress() is flawed. In this case, debug.

And one more thing: Use code tags. http://cplusplus.com/articles/z13hAqkS/
Last edited on Feb 1, 2012 at 7:18pm
Feb 1, 2012 at 7:40pm
I would go along with that.
Feb 2, 2012 at 11:12am
Thanks guys, I finally managed to find the answer here: http://www.codeproject.com/Articles/5891/How-to-send-a-user-defined-message-with-SendMessag

I needed to create a static member function that returned a pointer to my App class. The sample code presented on the site mentioned is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Header-File Test_SDI.h 
class CTest_SDIApp : public CWinApp
{
public:
static CTest_SDIApp *GetApp(); // GetApp() is necessary 
//to get the pointer anywhere else
void DocToApp() ; // Test-function that will be called
}
// Implementation-File Test_SDI.cpp
CTest_SDIApp *CTest_SDIApp::GetApp() // GetApp() is necessary 
//to get the pointer anywhere else
{
CWinApp *pApp = AfxGetApp() ;
if (pApp == 0)
{
// error handling
}
return (CTest_SDIApp*) pApp;
}
void CTest_SDI::DocToApp() // Test-function that will be called
{
AfxMessageBox(CTest_SDI::DocToApp was called ...) ;
}
// Implementation-File Test_SDIDoc.cpp
#include Test_SDI.h // If you forget to include 
//Test_SDI.h, then the members of 
// CTest_SDIDoc don't know class CTest_SDIApp and 
//its member-functions
BOOL CTest_SDIDoc::AppToDoc() // The function 
//AppToDoc() calls DocToApp()
{
AfxMessageBox(
  CTest_SDIDoc::AppToDoc was called and calls DocToApp()) ;
CTest_SDIApp::GetApp()->DocToApp() ; 
}
Topic archived. No new replies allowed.