2011-06-30 62 views
0

我想從MFC應用程序加載的DLL啓動模式對話框。我正在使用VS2010,EXE和DLL都在靜態庫中使用MFC。無法從MFC DLL創建模式對話框

我在我的DLL中調用DoModal()來啓動對話框,父對象是一個CWnd *指向MFC應用程序的主窗口。對話框資源位於DLL中。

這最終導致了MFC庫函數CWnd::CreateDlgIndirect,其中有這樣的調試檢查:

#ifdef _DEBUG 
    if (AfxGetApp()->IsKindOf(RUNTIME_CLASS(COleControlModule))) 
    { 
     TRACE(traceAppMsg, 0, "Warning: Creating dialog from within a COleControlModule application is not a supported scenario.\n"); 
    } 
#endif 

AfxGetApp()返回NULL所以在調試檢查代碼失敗。如果我在發佈版中編譯,會出現對話框,但似乎不起作用(即使我設置了默認值,所有字段都是空的,但某些按鈕不會出現)。

我嘗試添加AFX_MANAGE_STATE(AfxGetStaticModuleState());到啓動對話的功能的頂部,它沒有任何區別。

我錯過了什麼?

編輯:這是我用來調用對話框的代碼。

HMODULE oldResMod = AfxGetResourceHandle(); 

AFX_MANAGE_STATE(AfxGetStaticModuleState()); 
AfxSetResourceHandle(GetThisModule()); 

CWnd wndParent; 
wndParent.Attach(parent); 

CExportOptionsDlg dlg(&wndParent); 
dlg.project_name = project->GetName(); 

if (dlg.DoModal() != IDOK) 
{ 
    wndParent.Detach(); 
    AfxSetResourceHandle(oldResMod); 
    return false;  // cancelled 
} 

// ... (get some data from the dialog members) ... 

wndParent.Detach(); 
AfxSetResourceHandle(oldResMod); 
return true;   // OK 
+0

我認爲'CreateIndirect'是用於無模式對話框?在調用'DoModal'之前,您可能需要'InitModalIndirect',但顯示您的代碼會很有幫助。該問題不可能在MFC調試斷言中,而是導致斷言失敗的使用中的錯誤。 – AJG85

+0

您可能想閱讀Pat Brenner(MS的snr MFC工程師)評論[這裏是...](http://connect.microsoft.com/VisualStudio/feedback/details/376123/warning-creating-dialog-from-within -a-colecontrolmodule - 應用 - 是 - 不被支持的情況下) –

回答

1

檢查您是否已經在當前模塊(DLL/EXE)中的某處創建了CWinApp。

每個模塊應該只有一個CWinApp對象。通常,您將使CWinApp對象成爲一個全局變量,以便分別在模塊加載和卸載時創建並銷燬它。