CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    A question regarding AFX_MANAGE_STATE

    As we know, if you want to launch a dialog from a dll, you have to call AFX_MANAGE_STATE to switch module state from current module state to dll module state. Here is the code,
    Code:
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    CMyDialog dlg;
    dlg.DoModal();
    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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: A question regarding AFX_MANAGE_STATE

    According to http://msdn.microsoft.com/en-us/libr...tx(VS.80).aspx
    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:

    Code:
     AFX_MANAGE_STATE(AfxGetStaticModuleState( ))
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: A question regarding AFX_MANAGE_STATE

    Quote Originally Posted by LarryChen View Post
    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

  4. #4
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding AFX_MANAGE_STATE

    Quote Originally Posted by D_Drmmr View Post
    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.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: A question regarding AFX_MANAGE_STATE

    You have to do it within your DLL.
    Victor Nijegorodov

  6. #6
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding AFX_MANAGE_STATE

    Quote Originally Posted by VictorN View Post
    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.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: A question regarding AFX_MANAGE_STATE

    Strange design!
    Why not to use extension dll in such a case instead?
    Victor Nijegorodov

  8. #8
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding AFX_MANAGE_STATE

    Quote Originally Posted by VictorN View Post
    Strange design!
    Why not to use extension dll in such a case instead?
    May I ask why you think this is a strange design? What can an extension dll do in my case? Thanks.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: A question regarding AFX_MANAGE_STATE

    At least it does not need any AFX_MANAGE_STATE at all.
    Victor Nijegorodov

  10. #10
    Join Date
    Jul 2005
    Posts
    1,030

    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!
    Attached Files Attached Files

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: A question regarding AFX_MANAGE_STATE

    Did you include .lib file containing info about CMyDialog class?
    Victor Nijegorodov

  12. #12
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: A question regarding AFX_MANAGE_STATE

    Quote Originally Posted by LarryChen View Post
    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

  13. #13
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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.
    Attached Files Attached Files
    Best regards,
    Igor

  14. #14
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding AFX_MANAGE_STATE

    Quote Originally Posted by VictorN View Post
    Did you include .lib file containing info about CMyDialog class?
    Yes. I included .lib file by setting Linker/input/Additional dependencies. If you check the project, you will see it. Anything wrong or missing? Thanks.

  15. #15
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding AFX_MANAGE_STATE

    Quote Originally Posted by D_Drmmr View Post
    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.
    I tried to do it like this,
    Code:
    HMODULE hDll = GetModuleHandle(_T("MyDll.dll"));
    AFX_MANAGE_STATE((AFX_MODULE_STATE*)&hDll);
    CYourDialog dlg;
    dlg.DoModal();
    Here MyDll.dll is the dll where CYourDialog is defined. But I got an exception in AfxFindResourceHandle. What is wrong? Thanks.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured