views:

604

answers:

2

For example, I want to build a DLL exporting a function called ShowDialog, which displays a dialog and changes the text in one of the dialog buttons. The dialog and all other resources should be bundled in the DLL.

I did a quick test and when the DLL tries GetDlgItem() with a certain control ID, it gets a null pointer back. I'm assuming more steps are required to handle the resources properly in this case.

+3  A: 

From MSDN:

By default, MFC uses the resource handle of the main application to load the resource template. If you have an exported function in a DLL, such as one that launches a dialog box in the DLL, this template is actually stored in the DLL module. You need to switch the module state for the correct handle to be used. You can do this by adding the following code to the beginning of the function:

AFX_MANAGE_STATE(AfxGetStaticModuleState());
demoncodemonkey
+4  A: 

Do you do something like the following at the entry points to your DLL?

AFX_MANAGE_STATE(AfxGetStaticModuleState( ))

Check this technote here about MFC module state. There are various types of MFC module state, module state is the one you most commonly interact with.

This swaps the current module state with the state returned from AfxGetStaticModuleState until the end of the current scope.

Problems with resources in DLLs will occur if the AFX_MODULE_STATE macro is not used. By default, MFC uses the resource handle of the main application to load the resource template. This template is actually stored in the DLL. The root cause is that MFC's module state information has not been switched by the AFX_MODULE_STATE macro. The resource handle is recovered from MFC's module state. Not switching the module state causes the wrong resource handle to be used.

1800 INFORMATION
Thanks. Even though this reply carries more info I selected the other one to be the correct answer, since calling AFX_MANAGE_STATE at the entry point of the DLL doesn't solve the problem.
sharkin
Yes it does. The entry points to the DLL are any point that external code can call into your module. I presume you thought I meant something like in DLLMain or some such place
1800 INFORMATION
Yes I did, sorry for the misunderstanding.
sharkin