【问题标题】:AfxGetInstanceHandle() return null in regular mfc dllAfxGetInstanceHandle() 在常规 mfc dll 中返回 null
【发布时间】:2012-01-18 18:54:22
【问题描述】:

应用程序加载常规 dll。此 dll 中的每个函数都包含 AFX_MANAGE_STATE(AfxGetStaticModuleState())。在预处理器定义中包括宏 _USRDLL 和 _AFXDLL。例如,从常规 dll (mydll.dll) 导出函数:

BOOL RM_GetModule(IRMPage** ppInterface)
   {
       AFX_MANAGE_STATE(AfxGetStaticModuleState());
       HINSTANCE hInst = AfxGetInstanceHandle(); //**return NULL**
       ........
   }

应用:

BOOL CReMain2Dlg::OnInitDialog()
   {
       CDialog::OnInitDialog();
       ..........
       HINSTANCE hmCurrentModule;
       if((hmCurrentModule = AfxLoadLibrary("mydll.dll"))){
           (FARPROC&)pPageItem->pfGetModule = GetProcAddress(  
                                           hmCurrentModule, "RM_GetModule");
           pPageItem->pfGetModule(&(pPageItem->pPage)); //call
           ..........
       }        
   }

该项目于 2010 年从 6 个工作室转出。在 6 AfxGetInstanceHandle() 返回正确的值。 GetLastError() 返回 0。为什么 AfxGetInstanceHandle() 返回 NULL?如何解决? vs2010_sp1、win7_x64

【问题讨论】:

    标签: dll mfc


    【解决方案1】:

    最好的方法是将作为参数接收到的句柄存储在 DllMain 中的全局变量中,或者,如果它是 MFC dll,它应该已经存储在 CWinApp::m_hInstance 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      • 2012-12-17
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多