i'm fighting with MFC and dynamicly linking DLLs with LoadLibrary. It seems that I cannot get the MFC state right when the app calls DLL, and the DLL calls back in the same call. Ultimately, it leads to tons of asserts.
Here is code mock-up of what i'm doing.
The application is just normal, straight from the wizard MFC app. I've got button somewhere and this is the button's handler:
void callback() { AFX_MANAGE_STATE(AfxGetStaticModuleState( )); CDialog1 dlg; dlg.DoModal(); } typedef void (*TPluginMainFunc)(void*); void CTheApp1View::OnTestRun1() { static HMODULE hPluginMFCShared = LoadLibrary( _T("PluginMFCShared") ); if ( hPluginMFCShared ) { TPluginMainFunc func = (TPluginMainFunc) GetProcAddress( hPluginMFCShared, "plugin_main" ); if ( func ) { func(callback); } } }
Then the 'PluginMFCShared' looks like this:
typedef void (*TFunc)(); extern "C" void GS_EXTERNAL_ENTRY plugin_main(TFunc func) { AFX_MANAGE_STATE(AfxGetStaticModuleState( )); func(); CDialog1 dlg; dlg.DoModal(); }
So, the idea is that the app (CTheApp1View::OnTestRun1) loads a library and calls a function directly passing in a callback pointer. The library would use that callback to execute something from the app before continuing.
I thought AFX_MANAGE_STATE will take care of the MFC state, but there seem to be something more that needs to be done.
A test project could be found at (make sure TheApp1 project is set to be the start-up project): SystemOfPlugins.zip
Any ideas?
Thanks for any suggestions.
DoModal
needs to be in the state of wherever it's defined. – Mark Ransom