Tip : Usage of AFX_MANAGE_STATE
What is the use of AFX_MANAGE_STATE
We are familiar with Dynamic Link Libraries (DLLs) and most of our daily application development activities are using it. If you have an exported function in DLL and you don’t use any resources from the DLL itself then you could invoke it from an executable module and it will work fine.
But you want to launch a dialog from the DLL module and which is from it’s own resource part. What will happen if you invoke the domodal of a dialog from any of the exported function in the DLL. No doubt, the domodal will return -1.
How we can do that
Actually what is happening here, once we try to launch a dialog from the DLL from its own resource – but the application holds the resource handle of the main application. So we need to switch the module state for the correct handle to be used. We can do this by adding the following code to the beginning of the exported function in the DLL,
AFX_MANAGE_STATE( AfxGetStaticModuleState() );
This swaps the current module state with the state returned from AfxGetStaticModuleState( ) until the end of the current scope. This is a macro used to protect an exported function in a DLL.
Regular DLL exported function
Main application function
That’s working fine in the Regular DLL, now if we try the same routine with the MFC Extension DLL it may lead to a link error, please refer the below link for further details
Posted By :Arun M. S.