0
votes

I'm trying to launch a modal dialog from a DLL loaded by an MFC application. I'm using VS2010 and both the EXE and DLL use MFC in a static library.

I call DoModal() in my DLL to launch the dialog, with the parent being a CWnd* pointing to the main window from the MFC app. The dialog resource is in the DLL.

This eventually leads to the MFC library function CWnd::CreateDlgIndirect, which has this debug check:

#ifdef _DEBUG
    if ( AfxGetApp()->IsKindOf( RUNTIME_CLASS( COleControlModule ) ) )
    {
        TRACE(traceAppMsg, 0, "Warning: Creating dialog from within a COleControlModule application is not a supported scenario.\n");
    }
#endif

AfxGetApp() returns NULL so the code in the debug check fails. If I compile in release, the dialog appears, but doesn't seem to work (all the fields are empty even though I set defaults, some button's don't appear).

I've tried adding AFX_MANAGE_STATE(AfxGetStaticModuleState()); to the top of the function that launches the dialog, and it doesn't make any difference.

What am I missing?

Edit: here's the code I use to call the dialog.

HMODULE oldResMod = AfxGetResourceHandle();

AFX_MANAGE_STATE(AfxGetStaticModuleState());
AfxSetResourceHandle(GetThisModule());

CWnd wndParent;
wndParent.Attach(parent);

CExportOptionsDlg dlg(&wndParent);
dlg.project_name = project->GetName();

if (dlg.DoModal() != IDOK)
{
    wndParent.Detach();
    AfxSetResourceHandle(oldResMod);
    return false;       // cancelled
}

// ... (get some data from the dialog members) ...

wndParent.Detach();
AfxSetResourceHandle(oldResMod);
return true;            // OK
1
I think CreateIndirect is for modeless dialogs? You probably want InitModalIndirect before calling DoModal but it would be helpful to show YOUR code. The problem is not likely in the MFC debug assert but rather a bug in usage causing the assert to fail. - AJG85
You might want to read Pat Brenner's (snr MFC engineer at MS) comments here... - Tom Kirby-Green

1 Answers

1
votes

Check that you've actually created a CWinApp somewhere in your current module (DLL/EXE).

Every module should have one, and only one, CWinApp object. Typically, you would make the CWinApp object a global variable such that it is created and destroyed when the module is loaded and unloaded, respectively.