If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Now if I want to launch another dialog called CYourDialog from CMyDialog and CYourDialog is defined from different module state, then DoModal would fail for CYourDialog. Is there any way to make DoModal work for CYourDialog in this case? Thanks.

Re: A question regarding AFX_MANAGE_STATE

You must add the AFX_MANAGE_STATE macro at the beginning of all the exported functions in regular DLLs that dynamically link to MFC to set the current module state to the one for the DLL. This is done by adding the following line of code to the beginning of functions exported from the DLL:

Re: A question regarding AFX_MANAGE_STATE

Originally Posted by LarryChen

Now if I want to launch another dialog called CYourDialog from CMyDialog and CYourDialog is defined from different module state, then DoModal would fail for CYourDialog. Is there any way to make DoModal work for CYourDialog in this case? Thanks.

You should set the module state correctly before loading any resources from a DLL, so also before you create the second dialog.

Cheers, D Drmmr

Please put [code][/code] tags around your code to preserve indentation and make it more readable.

As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Re: A question regarding AFX_MANAGE_STATE

Originally Posted by D_Drmmr

You should set the module state correctly before loading any resources from a DLL, so also before you create the second dialog.

That is exactly my question actually. In my case, Basically CMyDialog and CYourDialog exist in different module states. CMyDialog is not dismissed yet when CYourDialog needs to be launched. So in order to launch CYourDialog, I need to switch the module state from CMyDialog to CYourDialog. But I don't know what to do to achieve that. Any idea how to do that? Thanks.

Re: A question regarding AFX_MANAGE_STATE

Re: A question regarding AFX_MANAGE_STATE

Originally Posted by VictorN

You have to do it within your DLL.

I know I have to do it within my dll. But the problem is that in my case, the template for CYourDialog exists outside the dll. So within the dll, if I want to launch CYourDialog I need to switch the module state from dll to CYourDialog. But I don't know how to do it. Any idea? Thanks.

Re: A question regarding AFX_MANAGE_STATE

Re: A question regarding AFX_MANAGE_STATE

Now I like to approach this dialog launching thing step by step. I am not clear conceptually on this topic so please bear with me. First of all, I attached a project. In this project, an executable tries to launch a dialog defined in a dll. Unfortunately I got a linker error "error LNK2019: unresolved external symbol "public: virtual __thiscall CMyDialog::~CMyDialog(void)" (??1CMyDialog@@UAE@XZ) referenced in function "public: void __thiscall CtestMFCExtensionDlg::OnBnClickedStart(void)" (?OnBnClickedStart@CtestMFCExtensionDlg@@QAEXXZ)". I couldn't figure out why. Please help me fix this issue first. Thank you very much!

Re: A question regarding AFX_MANAGE_STATE

Originally Posted by LarryChen

That is exactly my question actually. In my case, Basically CMyDialog and CYourDialog exist in different module states. CMyDialog is not dismissed yet when CYourDialog needs to be launched. So in order to launch CYourDialog, I need to switch the module state from CMyDialog to CYourDialog. But I don't know what to do to achieve that. Any idea how to do that? Thanks.

You cannot set the module state to a dialog, you set it to the instance handle of a library. Whenever you load resources from a DLL (e.g. create a dialog, which loads the dialog template from the resources), the module state needs to be set to the instance handle of that library. After the resources have been loaded, you simply set it back to the previous value. This is what AFX_MANAGE_STATE does (resetting the instance handle at scope exit.

When you use an extension DLL, the story is different. See the MSDN docs.

Cheers, D Drmmr

Please put [code][/code] tags around your code to preserve indentation and make it more readable.

As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Re: A question regarding AFX_MANAGE_STATE

The sample goes. Resource only DLL includes IDD_YOUR dialog template. Regular DLL includes IDD_MY dialog template, but implements both classes, CMyDialog and CYourDialog. Hope this close enough to what you're trying to implement.

Re: A question regarding AFX_MANAGE_STATE

Originally Posted by D_Drmmr

You cannot set the module state to a dialog, you set it to the instance handle of a library. Whenever you load resources from a DLL (e.g. create a dialog, which loads the dialog template from the resources), the module state needs to be set to the instance handle of that library. After the resources have been loaded, you simply set it back to the previous value. This is what AFX_MANAGE_STATE does (resetting the instance handle at scope exit.

When you use an extension DLL, the story is different. See the MSDN docs.