天天看点

一个使用动态链接库资源出错的难题

在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了

继续阅读