Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Create() function of modeless dialog is failed

truongptrtruongptr Posts: 5Member
My application loaded resource from DLL. And it is failed when creating a modeless dialog (it worked fine if I didn't load resource from DLL). I was using a custom control in the modeless dialog. But this control has been registered.

Please help me to give solution for this issue.

Thanks,
-Truong

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : My application loaded resource from DLL. And it is failed when
    : creating a modeless dialog (it worked fine if I didn't load resource
    : from DLL). I was using a custom control in the modeless dialog. But
    : this control has been registered.
    :
    : Please help me to give solution for this issue.
    :
    : Thanks,
    : -Truong
    :
    [color=Blue]
    Is this is an MFC application? If so, where is your CDialog-based class? In a DLL or in a main application?
    [/color]
  • truongptrtruongptr Posts: 5Member
    Yes, this is an MFC application. My CDialog-based class is in a DLL.
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : Yes, this is an MFC application. My CDialog-based class is in a DLL.
    :
    [color=Blue]If your dialog template also in the same DLL, you need to wrap the call to Create() into one of a dialog methods and use AFX_MANAGE_STATE macro:[/color]
    [code]
    void CYourDialog::MakeModeless ()
    {
    AFX_MANAGE_STATE (AfxGetStaticModuleState ());
    Create ();
    }
    [/code]
    [color=Blue]And then use that method as your creation method in the application:[/color]
    [code]
    ...
    CYourDialog box;
    box.MakeModeless ();
    ...
    [/code]
  • truongptrtruongptr Posts: 5Member
    It did not work. It gave a assert message when calling AfxFindResourceHandle()function inside CDialog::Create().

    The resource and the CDialog-based class are not in a DLL. They are in two difference DLL.


  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : It did not work. It gave a assert message when calling
    : AfxFindResourceHandle()function inside CDialog::Create().
    :
    : The resource and the CDialog-based class are not in a DLL. They are
    : in two difference DLL.

    [color=Blue]Then you need to load that DLL and set the handle, like it is shown here:
    [link=http://msdn.microsoft.com/en-us/library/d8ws31ff(VS.71).aspx]http://msdn.microsoft.com/en-us/library/d8ws31ff(VS.71).aspx[/link]

    After the dialog is loaded (after Create() call) you should probably set the resource handle back to the one before the call. That is what macro does, only it does it for CURRENT DLL and you need a different one. Try this code:[/color]
    [code]
    void application_code_or_any_dll_code::open_my_dialog ()
    {
    [color=Green]//
    // Preserve the default handle, because it is a
    // global variable - you need to restore it back
    //[/color]
    HINSTANCE hPreviousModule = AfxGetResourceHandle ();
    HINSTANCE hDLL = ::LoadLibrary ();
    AfxSetResourceHandle (hDLL);
    [color=Green]//
    // Load a dialog
    //[/color]
    CMyDialog dlg;
    dlg.Create ();
    [color=Green]//
    // Restore all back
    //[/color]
    ::FreeLibrary (hDLL);
    AfxSetResourceHandle (hPreviousModule);
    }
    [/code]
  • truongptrtruongptr Posts: 5Member

    I have already set resource in the InitInstance() of the application class. And it causes this issue.
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    :
    : I have already set resource in the InitInstance() of the application
    : class. And it causes this issue.
    :
    [color=Blue]This resource is a global variable. You should not set it in InitInstance(), because that will affect the whole program - your main program will not be able to function. You have to set that resource just before making a dialog and when dialog is loaded - return back the resource which has been there before that - see the code I posted.[/color]
  • truongptrtruongptr Posts: 5Member
    Thanks for your help. The purpose of this is I want to support multilingual to my application. For each language, I will create a separated resource and add it in a DLL .And set resource from DLL to application when begin to initialize application. So I have done it in the InitInstance() function.

    This issue just occurs when I load resource from DLL (as I said before). I have set 'No Fair Create' option to be true for the dialog and debugged through the DDX_Control when initializing dialog and I found that the system failed when finding resource of the custom control (have not this resource). But I don't know why? because I have tried opening the "x.res" which is created after compiling the resource dll by VS and I have still seen this custom control.

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : Thanks for your help. The purpose of this is I want to support
    : multilingual to my application. For each language, I will create a
    : separated resource and add it in a DLL .And set resource from DLL to
    : application when begin to initialize application. So I have done it
    : in the InitInstance() function.
    :
    : This issue just occurs when I load resource from DLL (as I said
    : before). I have set 'No Fair Create' option to be true for the
    : dialog and debugged through the DDX_Control when initializing dialog
    : and I found that the system failed when finding resource of the
    : custom control (have not this resource). But I don't know why?
    : because I have tried opening the "x.res" which is created after
    : compiling the resource dll by VS and I have still seen this custom
    : control.
    :
    :
    [color=Blue]I see...[/color]
Sign In or Register to comment.