1

I need to gain access to a member of main frame and can be changed by reference. Here is a free function to get main frame

inline CMainFrame* GetMainFrame()
{
    CMainFrame* mainFrame = dynamic_cast<CMainFrame*>(AfxGetMainWnd());
    ASSERT(mainFrame);
    return mainFrame;
}

I called above function to my Dialog of an MFC application and found the main frame pointer NULL.

How can I gain access of main frame member which can be changed by reference?

Thanks in advance.

  • Is the `CWnd*` returned by `AfxGetMainWnd()` non-NULL before your dynamic_cast? – Richard Critten Feb 01 '16 at 10:42
  • 1
    I'm a big fan of pasta, too, but I suggest against writing spaghetti-code. Encapsulate the data in an object, and pass that to the dialog, that needs access to it. – IInspectable Feb 01 '16 at 10:49
  • 1
    Give us more information about your program and try to step info the MFC source code of `AfxGetMainWnd()`, this may give some hints about what is going on. – Jabberwocky Feb 01 '16 at 11:33
  • This function can return NULL for 3 reasons: `1` The pointer returned from `AfxGetMainWnd` is not of type `CMainFrame`. `2` The pointer to the mainframe window wasn't properly stored in the TLS (likely to cause several debug assertions). `3` The function is called from a thread other than the owning thread of the mainframe window. – IInspectable Feb 01 '16 at 20:42
  • Yes @llnspectable it was called before the main frame is created. That's why it's NULL. Some cases i found whe nit was called from another thread other than main thread – Tajuddin Khandaker Apr 22 '17 at 19:51

0 Answers0