Search code examples
c++dllmfccdialog

C++ CDialogEx losses ParentWnd when run from DLL


I've got a situation. There is a huge app (C++ MFC). I write a .dll module with a dockable pane.

Pane interface structure:

 Pane -> CMFCToolBar  
      -> CSplitterWndEx -> CListCtrl 
                        -> CDialogEx

That's how i create my DialogEx:

int CMyPane::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    ///////////////////////////////////////////
    ////////// TAB CTRL ///////////////////////
    ///////////////////////////////////////////

    const int dwResTabCtrlStyle = WS_CHILD | WS_VISIBLE | TCS_VERTICAL;// | LVS_SINGLESEL;  
    
    if(!m_SptitterWndEx.AddTabCtrl(0, 1, &m_tabCtrl, CMFCTabCtrl::STYLE_3D, CMFCBaseTabCtrl::LOCATION_TOP, CSize(10,10)))
        return -1;

    {
        AFX_MANAGE_STATE(AfxGetStaticModuleState());
        
        m_DialogEx.Create(CAccuracyResultPage::IDD, NULL);
    }

    m_DialogEx.SetParent(&m_tabCtrl);
    if(!m_DialogEx.GetParent())
        return -1;

    str.LoadString( AfxGetStaticModuleState()->m_hCurrentResourceHandle, IDS_RESULT_TAB);
    m_tabCtrl.AddTab(&m_DialogEx, str, 0);  

    AdjustLayout();

    return 0;
}

I getting assert on CDialogEx::PreTranslateMessage. The reason is the when it gets it's parents

_AFXWIN_INLINE CWnd* CWnd::GetParent() const
    { ASSERT(::IsWindow(m_hWnd)); return CWnd::FromHandle(::GetParent(m_hWnd)); }

m_hWnd is not a Wnd. But the CDialog looks perfectly OK, it has m_pParentWnd, but it's not m_tabCtrl.

So my questions are: Why doesn't CDialogEx set its parent?! And how it can be dealed with?!


Solution

  • Where is your dialog template residing? Is it in the same dll? If not, then i think that's your problem.

    My guess is, if your dialog template resides in a different dll then windows might search in the HWND=>CWnd map of the module state of that dll. If that happens then it wont be able to find the CWnd in the map and will create a temporary CWnd object and set it as the parent of the dialog.