Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AfxGetInstanceHandle() return null in regular mfc dll

Tags:

dll

mfc

Application loads the regular dll. Each function in this dll contains AFX_MANAGE_STATE(AfxGetStaticModuleState()). In the preprocessor definitions included macro _USRDLL and _AFXDLL. For example, export function from regular dll (mydll.dll):

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

application:

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

The project was transferred out of 6 studios at 2010. At 6 AfxGetInstanceHandle() return correct value. GetLastError() return 0. Why AfxGetInstanceHandle() return NULL? How to fix it? vs2010_sp1, win7_x64

like image 518
made_in_russia Avatar asked Dec 06 '25 20:12

made_in_russia


1 Answers

The best way is to store the handle you receive as parameter in DllMain in a global variable, or, in case it's an MFC dll, it should already be stored in CWinApp::m_hInstance.

like image 193
sashoalm Avatar answered Dec 09 '25 20:12

sashoalm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!