在DLL彈出一個對話框,開始彈出對話框時出現“Debug Assertion Failed”,
上網搜了下,發現子產品句柄的設定問題。然後作出如下解決:
1. 定義兩個全局變量:
HINSTANCE g_hModuleInstance = NULL;
HINSTANCE g_hOldModuleInstance = NULL;
2.在DllMain函數中對g_hModuleInstance進行指派:
g_hModuleInstance = hInstance;
3.在要使用對話框資源的類建立兩個函數設定子產品句柄。
void CAlternatExtractFun::SetResourceHandle()
{
g_hOldModuleInstance = AfxGetModuleState()-
> m_hCurrentResourceHandle;
AfxGetModuleState()-> m_hCurrentResourceHandle = g_hModuleInstance;
}
void CAlternatExtractFun::RestoreResourceHandle()
AfxGetModuleState()-> m_hCurrentResourceHandle
=g_hOldModuleInstance;
4.在使用對話框資源的函數裡調用這兩個函數:
void CAlternatExtractFun::Execute()
SetResourceHandle();
CAlterExtractDlg dlg;
dlg.DoModal();
RestoreResourceHandle();
但是運作時dlg.DoModal();這一行出錯:Debug Assertion Failed!
然後調試進行:
在 CDialog::DoModal()函數中的
TRY
// create modeless dialog
AfxHookWindowCreate(this);
if (CreateDlgIndirect(lpDialogTemplate,
CWnd::FromHandle
(hWndParent), hInst))
在運作CreateDlgIndirect(lpDialogTemplate,
(hWndParent), hInst)這一句出現Debug Assertion Failed!
再進入CreateDlgIndirect函數:
BOOL CWnd::CreateDlgIndirect(LPCDLGTEMPLATE lpDialogTemplate,
CWnd* pParentWnd, HINSTANCE hInst)
#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()-> IsKindOf( RUNTIME_CLASS( COleControlModule ) 出錯。
然後上網搜尋CreateDlgIndirect函數。網上有個說法說是
是VS C++ 2005+sp1的一個bug.下面是網友的一個算法:
樓主wangjia184(我就是傳說中的。。。。。。SB)2006-10-24 14:36:40 在
VC/MFC / 基礎類 提問
先說說程式的結構
這是一個VS2005的插件,屬ATL
ATL中隐式連結到一個MFC DLL,MFC DLL中是有Dialog
以前使用 盜版的VS2005 中文版, 沒打更新檔。一切正常
這2天把系統重裝了,換成了正版的VS2005英文版,還打了SP1(Beta)更新檔
問題就出現了!
當Dialog DoModal()的時候,報錯。
錯誤堆棧如下:
DoModal() 調用了 CreateDlgIndirect()
CreateDlgIndirect 中最開始有一段:
#ifdef _DEBUG
if ( AfxGetApp()-> IsKindOf( RUNddTIME_CLASS( COleControlModule
) ) )
{
TRACE(traceAppMsg, 0, "Warning: Creating dialog from within
a COleControlModule application is not a supported
scenario.\n");
}
#endif
其中的AfxGetApp傳回NULL, 我的DLL入口是DllMain,這個AfxGetApp肯定會傳回
NULL
是以在這裡報錯了
使用Release 版本運作,一切正常, 好像這段是打了SP1加上去的。
我暈死了 難道以後都要在Release版本下調試?
問題點數:50、回複次數:5
不知大家怎麼認為,還有我的問題怎麼解決呢?
我的編譯環境:
win xp sp2
VS C++ 2005+ sp1
intel 雙核處理器
回複人: sjdev
2008-3-9 12:09:45
無主題
hOldHandle = AfxGetResourceHandle();
AfxSetResourceHandle(hDll);
...
use the resource in the dll
AfxSetResourceHandle(hOldHandle);
回複人: clever101
2008-3-9 12:11:55
hDll?照這樣還得先LoadLibrary了