'Confusing: Retrieve Parent Wnd in CDialog, that is given with SetParent

In a CView derived class I give the dialog the parent via:

CTestDlg& dlg = m_TestDLg;
dlg.SetParent(this);
if (dlg.DoModal() != IDOK)
    return;

Inside CTestDlg I try to retrieve the parent with

CMyView* pTView_ = (CMyView*)GetParent();        // Wrong Parent pointer
CMyView* pTView2 = (CMyView*)GetParentOwner();   // Wrong Parent pointer  
CMyView* pTView3 = (CMyView*)m_pParentWnd;       // Wrong Parent pointer

I thought with SetParent() I can get back it with GetParent().

Even the doc in MSDN confuses me. What really happens with Set/GetParent()?



Solution 1:[1]

Not the answer exactly, but this Only ONE gives me the corrent Parent I wanted.
(So I mark it as solution for me)

CView* GenericGetActiveView()
{
  // Retrieve Active View
  CView* pActiveView = NULL;    

  CWnd* pWndMain = AfxGetMainWnd();
  if (NULL != pWndMain)
  {
    if (pWndMain->IsKindOf(RUNTIME_CLASS(CMDIFrameWnd)))
    {
        // MDI application
        CMDIChildWnd* pChild = ((CMDIFrameWnd*)(pWndMain->MDIGetActive();
        if (pChild)
            pActiveView = pChild->GetActiveView();  
    }
    else if (pWndMain->IsKindOf(RUNTIME_CLASS(CFrameWnd))) 
    {   
        // SDI appllication 
        pActiveView = ((CMainFrame*)pWndMain)->GetActiveView();
    }
    else
        TRACE(_T("Attention no MDI / SDI Main Wnd found!\n"));
  }

  return pActiveView;
}

@IInspectable pointed me to this direction in his comment. (MFC has a handle map internally that can easily cause surprising results)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tom Tom