What Does Afx_Manage_State(Afxgetstaticmodulestate()) Do Exactly

What does AFX_MANAGE_STATE(AfxGetStaticModuleState()) do exactly

Every .exe and .dll has an internal resource handle, pointing to your dialogs and other resources. If you call a function in your DLL, the current resource handle is pointing to the resources in the .exe, which is wrong and needs to be changed to the resources of the DLL.

This is what AFX_MANAGE_STATE does.

error LNK2005: _DllMain@12 already defined in MSVCRT.lib

If you read the linker error thoroughly, and apply some knowledge, you may get there yourself:

The linker links a number of compiled objects and libraries together to get a binary.

Each object/library describes

  • what symbols it expects to be present in other objects
  • what symbols it defines

If two objects define the same symbol, you get exactly this linker error. In your case, both mfcs80.lib and MSVCRT.lib define the _DllMain@12 symbol.

Getting rid of the error:

  1. find out which of both libraries you actually need
  2. find out how to tell the linker not to use the other one (using e.g. the tip from James Hopkin)

Changing from AfxGetAppModuleState to AfxGetStaticModuleState gives linker error: LNK1169: one or more multiply defined symbols found

The question was mainly with the linker error than AfxGetAppModuleState or AfxGetStaticModuleState itself. Sorry if there was any confusion in the question I asked.
This linker error occurs when the symbol is defined in another module which this module refers or the order of the header files with MFC and non MFC are declared.
It was a tough time for me to solve this by organizing the header. I had to park this task aside so that I can pick this again in free time!

MFC state invalid when DLL called through LoadLibrary

Here's another suggestion. In your App variable, add an AFX_MODULE_STATE* variable named m_pModuleState, and initialize it at the end of the InitInstance funciton,

m_pModuleState = AfxGetModuleState();

Modify your callback function to set the application state before opening the dialog, and then set back the original state before exiting the function

void callback()
{
//Get the original state
AFX_MODULE_STATE* pOriginalState = AfxGetModuleState();

//Set the mfc state
AfxSetModuleState(((CTheApp1App*)&theApp)->m_pModuleState);

//Do stuff here
CDialog1 dlg;
dlg.DoModal();

//Set the mfc state back to its original state
AfxSetModuleState(pOriginalState);
}

And keep your plugin as it was in your example

extern "C" void GS_EXTERNAL_ENTRY plugin_main(TFunc func)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState( ));

func();
CDialog1 dlg;
dlg.DoModal();
}

This way, you would call AFX_MANAGE_STATE in your plugins, but when some of the plugin make a call to the callback function, you make sure to set the app's state so it can find the good dialog resources and execute state-specific functions



Related Topics



Leave a reply



Submit