在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,
CWnd::FromHandle
(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函数。网上有个说法说是
#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
是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 双核处理器
无主题
hOldHandle = AfxGetResourceHandle();
AfxSetResourceHandle(hDll);
...
use the resource in the dll
...
AfxSetResourceHandle(hOldHandle);
上网搜了下,发现模块句柄的设置问题。然后作出如下解决:
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,
CWnd::FromHandle
(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函数。网上有个说法说是
#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
是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了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!