Create() function of modeless dialog is failed

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

  • : 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]
  • Yes, this is an MFC application. My CDialog-based class is in a DLL.
  • : 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]
  • 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.


  • : 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]

  • I have already set resource in the InitInstance() of the application class. And it causes this issue.
  • :
    : 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]
  • 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.

  • : 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.

Howdy, Stranger!

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

Categories