DllMain加载其他DLL造成的死锁问题及其解决办法

使用VS 2008新建一个MFC ActiveX工程,因为在工程里要用到GDI+。我习惯把初始化GDI+库的代码放在应用程序类的InitInstance函数,对应的销毁代码放在ExitInstance函数。具体如下:


先在应用程序类里定义一个数据成员:


     

  1. ULONG_PTR   m_gdiplusToken;  

 


然后添加初始化GDI+库的代码和对应的销毁代码:


    

  1. BOOL CImagePreviewXApp::InitInstance()  
  2. {  
  3.     BOOL bInit = COleControlModule::InitInstance();  
  4.     if (bInit)  
  5.     {  
  6.         // TODO: Add your own module initialization code here.  
  7.         // Initialize GDI+ 的初始化代码,建议放在//InitInstance函数  
  8.         GdiplusStartupInput gdiplusStartupInput;  
  9.         GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);  
  10.     }  
  11.     return bInit;  
  12. }  
  13. // CImagePreviewXApp::ExitInstance - DLL termination  
  14. int CImagePreviewXApp::ExitInstance()  
  15. {  
  16.     // TODO: Add your own module termination code here.  
  17.     GdiplusShutdown(m_gdiplusToken);  
  18.     return COleControlModule::ExitInstance();  
  19. }   

 


   结果在编译时老是出现一个问题,就是编译时输出窗口:


1>Embedding manifest...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Registering output...


     到了这里,VS 2008就像停滞一样,半天不反应,直到我取消生成。我以为是VS 2008的bug,因为我用VS 2010编译这个工程并无这个现象(这个实在有点令人奇怪!)。到论坛一问,蒋晟大侠告知:在DllMain的封装函数InitInstance中有加载其他DLL造成了死锁。


     怎么解决这个问题呢?一种方法是写两个接口函数分别实现初始化GDI+库和对应的销毁功能。我懒得写两个接口函数,干脆把这个工作放在ActiveX控件类(派生自COleControl的那个类)的构造函数和析构函数里。


from:http://blog.csdn.net/clever101/article/details/5629512

原文地址:https://www.cnblogs.com/lidabo/p/3435092.html