Home > Enterprise >  Handing custom HINSTANCE to MFC Dialog
Handing custom HINSTANCE to MFC Dialog

Time:11-09

I am porting the GUI of a very old plugin from win32 to MFC. The dialog used to be started by invoking something like:

DialogBoxParam( GetDLLInstance(), ..., GetHWND(), ..., ... )

When trying to debug why my MFC solution doesn't work, I found, that above code would fail if I replace GetDLLInstance() by nullptr. So, for the code to work, it seems to be imperative, to provide it with the correct HINSTANCE, as the default one seems to be wrong. As MFC is just a wrapper for those win32 functions, I assume it also has to be provided with this information. However, when starting a CDialogEx-derived class, I didn't find a way to set a HINSTANCE. So how do I tell the MFC widgets the correct HINSTANCE for their internal call to DialogBoxParam( ... )?

CodePudding user response:

AfxSetResourceHandle sets the HINSTANCE handle that determines where the default resources of the application are loaded.
As @RbMm said, you need to set before rundialog and restore instantly.
For Modeless Dialog Box,

HMODULE hPrevious = AfxGetResourceHandle();
AfxSetResourceHandle(hMod);
CMyDialog *pDlg = new CMyDialog();
pDlg->Create(ID_DLG, this);
AfxSetResourceHandle(hPrevious);
pDlg->ShowWindows(SW_SHOW);

For Modal Dialog Box, see the answer.

  • Related