Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7091233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:09:33+00:00 2026-05-28T08:09:33+00:00

So I have this MFC dialog program I am working with. The dialogs are

  • 0

So I have this MFC dialog program I am working with. The dialogs are written but now I am having difficulty passing data around from dialog to dialog. I have the following structure _dlgDataHandler set up in a class derived from CWinApp and have have created a “new” statement for a pointer to this type.

//…….SRK.h file

class CSRK_App : public CWinApp
    {
public:

    CFSB_App();

     // added the following data structure for data passing withing the program

    typedef struct _dlgDataHandler {
      char RepetitionRadio[24];
          // another member
          // yet another member and so on as necessary
    } *dlgDataHandlerPtr;

      // extern dlgDataHandlerPtr dlgDataHandler;

// Overrides
     // ClassWizard generated virtual function overrides
     //{{AFX_VIRTUAL(CSRK_App)
     public:
     virtual BOOL InitInstance();
     //}}AFX_VIRTUAL

// Implementation

    //{{AFX_MSG(CSRK_App)
    // NOTE - the ClassWizard will add and remove member functions here.
        //    DO NOT EDIT what you see in these blocks of generated code !
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
 }; 

//……. SRK.cpp A pointer to a new dataHandler created in this block about 2/3 the way down

// CSRK_App initialization

BOOL CSRK_App::InitInstance()
{
AfxEnableControlContainer();

// Standard initialization
// If you are not using these features and wish to reduce the size
//  of your final executable, you should remove from the following
//  the specific initialization routines you do not need.
//SetRegistryKey(_T("Local AppWizard-Generated Aplications"));

#ifdef _AFXDLL
Enable3dControls();         // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic();   // Call this when linking to MFC statically
#endif

//CSRK_Dlg dlg;
CDialogMain dlg("SRK - Beta");    // added 12/27 **
m_pMainWnd = &dlg;

//const char* m_pszHelpFilePath = NULL;
//free((void*)m_pszHelpFilePath);
//m_pszHelpFilePath=_tcsdup(_T("c:\SRKHelp.rtf"));

// the following line added to allocate memory for the structure
    dlgDataHandlerPtr dlgDataHandler = new _dlgDataHandler;

dlg.SetWizardMode();          // added 12/27 **
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
}
else if (nResponse == IDCANCEL)
{
    // TODO: Place code here to handle when the dialog is
    //  dismissed with Cancel
}

// Since the dialog has been closed, return FALSE so that we exit the
//  application, rather than start the application's message pump.
return FALSE;
}

In the dialog .cpp files, there are five, I need to be able to get data from the AFX variables “m_” and load them into this dataHandler Structure (or another one like it) so that they can be used in other dialogs and parts of the program, specifically my actual code when all the dialog data collection is done. Someone said to use AfxGetApp() so that I could have a handle on the current instance but I do not understand what they are talking about. And yes I have read about it in many forums, I just don’t get it. I further realize this is probably not the best way to do it. I am trying to learn MFC/OOP with what time I have available, but for now, I am just trying to get a handle on the basic process as I can tune it later once I understand how to collect and pass simple data around.

I further don’t understand how calling AfxGetApp() will help me get a handle on the members of CSRK_App. It inherited CWinApps public members but AfxGetapp() can’t see what CSRK_App has… can it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:09:33+00:00Added an answer on May 28, 2026 at 8:09 am

    First, to explain the AfxGetApp advice you have received. There is some extra hand-waving using ‘new’ and a pointer, but this is basically using a global variable for the structure that holds your data. This is not the best way to do what you are trying to do. There are many pitfalls.

    AfxGetApp() is an MFC call that returns a pointer to your main App class derived from CWinApp.
    If you want to use that returned pointer, you need to cast it as a CSRK_App* pointer with:

    CSRK_App* pApp = static_cast <CSRK_App*> ( AfxGetApp());

    Then you can use pApp->dlgDataHandlerPtr->… to access the variables you need.

    Now, for the pitfalls. Someone else may chime in with a reason why the ‘new’ and the pointer are helpful, but I do not see any advantage to this approach as compared to just having a local variable dlgDataHandler inside your CSRK_App class. That would simplify the code.

    The next issue is that all your data is public in a struct. Any dialog class that can call AfxGetApp can read or write any data in that struct. You have no way to control access.

    Also, all of your dialog classes must now include SRK_App.h so they know the structure, and have access to all other variables in that App class.

    A cleaner, object-oriented approach would be to declare the struct (class) for the data in a separate .h file that could be included in the dialog classes. Then, you would pass a pointer/reference to this data into the constructor of the dialog classes. The dialog class would have no need to know anything about the App class.

    For an even higher level of segregation, the dialog classes can be written so they only get a copy of the dlgDataHandler class passed in before calling .DoModal(), and then after the DoModal call returns with IDOK, the App class can have control over which data from the dialog gets updated into the dlgDataHandler class. The advantage of this approach is that it insures that no matter how the dialog class is programed, the user can always “Cancel” the dialog without modifying any data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a program built in MFC. I have this weird situation
I'm still working on a Data Acquisition program in MFC and am getting stuck
I have an old MFC utility written with VS2008 project. We have used this
I have this method on a webpart: private IFilterData _filterData = null; [ConnectionConsumer(Filter Data
I have a simple MFC program which displays the progressbar..I used the below code
How can I print a document using MFC Dialog Based Application? I have made
More exactly, the web browser control. In a WM6 dialog based MFC project. This
I stumbled upon a curious problem with MFC. I have a Dialog where I
I have written a runtime-created dialog class that doesn't use any resource files based
I am trying to change cursor of a button in MFC dialog. i have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.