I’m trying to create a UI(dialog) in an existing dll using MFC where an application is supposed to call the method which should create this dialog(this can be any application. the dialog is specific to the dll, but the method is generic across multiple dlls). I’m using DoModal method here with a CDialog inherited class and the return value of the DoModal is -1 with result of getexacterror code 1814 which is ERROR_RESOURCE_NAME_NOT_FOUND according to the microsoft documentation. Has anyone faced similar situation and guide me through it. I’ve literally tried copying from a sample app and it still wouldn’t work.
what i am trying to create is a basic empty dialog.
The code looks like this
mydlg dlg1;
int nRet = dlg1.InitDlg();
DWORD exacterror = ::GetLastError();
where the class declaration of mydlg is like:
class mydlg : public CDialog
the method InitDlg:
BOOL InitDlg()
{
CDialog::DoModal();
}
and the assertion occurs in the entry of the DoModal method while debugging.
could anyone help me with this one
Tried with a new MFC sample app and compared what I missed.
Expecting someone who faced the same issue to give a resolution and point out my mistake.
Meow42 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
Resources are uniquely identified by three pieces of information:
- The resource ID or name
- The resource type
- The module handle (
HMODULE
) identifying the module containing the resource
The resource ID is passed to CDialog
‘s c’tor and the type (RT_DIALOG
) is inferred by the system as the implementation calls CreateDialogIndirectParam()
to construct the dialog. The module handle, however, is never passed along and MFC pulls this information out of a global state object (see Managing the State Data of MFC Modules).
By default, the global AFX_MODULE_STATE
points to the executable used to start the process. This is an implementation detail as long as code and resources are linked into a single EXE. However, managing the module state becomes your responsibility as soon as you move resources into a DLL.
Thankfully, the rules are easy to follow, as explained under Exported DLL Function Entry Points:
If you have an exported function, such as one that launches a dialog box in your DLL, you need to add the following code to the beginning of the function:
AFX_MANAGE_STATE(AfxGetStaticModuleState())
This does two things:
- It adjusts the global
AFX_MODULE_STATE
to point to the current module for use in future resource lookups - It returns an (invisible) object with automatic storage duration whose d’tor restores the previous module state
Adding this piece of code to the beginning of every function exported from the DLL that (directly or indirectly) accesses resources solves the issue of resource lookup failures.
Full details on the internals are available here: TN058: MFC Module State Implementation.