新建了一个工程A(MFC DLL),Regular dll using shared MFC DLL,插入一个对话框资源,并生成CPanel类。定义全局函数: 
CDialog __declspec(dllexport) *Opendlg(CWnd *parent) 

AFX_MANAGE_STATE(AfxGetStaticModuleState()); 
CPanel *dlg=new CPanel(); 
dlg->Create(IDD_PANEL,parent); 
dlg->ShowWindow(SW_SHOW); 
return dlg; 

另建一个工程B(MFC EXE),基于对话框,在OnInitDialog中Opendlg(this),就会出错。为什么? 
如果把A编译成Release版本却没有问题!? 
还有如果改成弹出模式对话框也不会出错 

解决方案 »

  1.   

    试试下面方法:CDialog *m_pdlg; // m_pdlg是你工程B中CWnd全局类的一数据成员然后再CWnd全局类构造函数中 m_pdlg = Opendlg(this);
      

  2.   

    是断言错误
    在CWnd::AssertValid() line 890 + 25 bytesvoid CWnd::AssertValid() const
    {
    ...
    else
    {
    // should be a normal window
    ASSERT(::IsWindow(m_hWnd)); // should also be in the permanent or temporary handle map
    CHandleMap* pMap = afxMapHWND();
    ASSERT(pMap != NULL);//停在这句了...
    }
      

  3.   

    这是Call StackCDialog::AssertValid() line 803
    AfxAssertValidObject(const CObject * 0x0012fe74 {CTestDlg}, const char * 0x5f4d1330 THIS_FILE, int 258) line 108
    CWnd::CreateDlgIndirect(const DLGTEMPLATE * 0x100194a0, CWnd * 0x0012fe74 {CTestDlg hWnd=???}, HINSTANCE__ * 0x10000000) line 260
    CDialog::CreateIndirect(const DLGTEMPLATE * 0x100194a0, CWnd * 0x0012fe74 {CTestDlg hWnd=???}, void * 0x00000000, HINSTANCE__ * 0x10000000) line 223
    CDialog::CreateIndirect(void * 0x100194a0, CWnd * 0x0012fe74 {CTestDlg hWnd=???}, HINSTANCE__ * 0x10000000) line 200 + 22 bytes
    CDialog::Create(const char * 0x000007d0, CWnd * 0x0012fe74 {CTestDlg hWnd=???}) line 182 + 20 bytes
    CDialog::Create(unsigned int 2000, CWnd * 0x0012fe74 {CTestDlg hWnd=???}) line 543 + 29 bytes
    Opendlg(CWnd * 0x0012fe74 {CTestDlg hWnd=???}) line 10
      

  4.   

    Call stack:CWnd::AssertValid() line 890 + 25 bytesCDialog::AssertValid() line 803AfxAssertValidObject(const CObject * 0x0012fe74 {CTestDlg}, const char * 0x5f4d1330 THIS_FILE, int 258) line 108CWnd::CreateDlgIndirect(const DLGTEMPLATE * 0x100194a0, CWnd * 0x0012fe74 {CTestDlg hWnd=???}, HINSTANCE__ * 0x10000000) line 260CDialog::CreateIndirect(const DLGTEMPLATE * 0x100194a0, CWnd * 0x0012fe74 {CTestDlg hWnd=???}, void * 0x00000000, HINSTANCE__ * 0x10000000) line 223CDialog::CreateIndirect(void * 0x100194a0, CWnd * 0x0012fe74 {CTestDlg hWnd=???}, HINSTANCE__ * 0x10000000) line 200 + 22 bytesCDialog::Create(const char * 0x000007d0, CWnd * 0x0012fe74 {CTestDlg hWnd=???}) line 182 + 20 bytesCDialog::Create(unsigned int 2000, CWnd * 0x0012fe74 {CTestDlg hWnd=???}) line 543 + 29 bytesOpendlg(CWnd * 0x0012fe74 {CTestDlg hWnd=???}) line 10
      

  5.   

    AFX_MANAGE_STATE(AfxGetStaticModuleState()); 
    CPanel *dlg=new CPanel(); 
    改为
    CPanel *dlg=new CPanel(); 
    AFX_MANAGE_STATE(AfxGetStaticModuleState()); 其实还是还是返回dlg的hwnd比较好。
      

  6.   

    CDialog __declspec(dllexport) *Opendlg(CWnd *parent) 参数不要用窗口指针. 用句柄, 改成CDialog __declspec(dllexport) *Opendlg(HWND hWnd) 
    {
    ...
    dlg->Create(IDD_PANEL, CWnd::FromHandle(hWnd) ); 
    ...
    }
    实际运行中,窗口指针是会改变的,而句柄会保持不变。