Creating a dialog from a DLL resource

I want (using MFC) to create a dialog which resource
It's resource will come from a separate dll File.

I do I do it ?
Dmitry_Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
danelroismanConnect With a Mentor Commented:
Create resource Dll with Dialog resource.

In Exe project:

1)Load the dll - LoadLibrary API
2)Find the resoutce - FindResource API
3)Create pointer to dialog and load the dialog form dll
 
CDialog * MyDialog;
MyDialog = (CDialog *)LoadResource( hModule, hResource );
MyDialog.Create(R_ID,...);

4)Show the dialog
MyDialog.DoModal();
....

Daniel.

For more information see MSDN Library

0
 
VizCommented:
- create your dialog template with the resource editor
- run the class wizard, shortcut Ctrl-W
- choose the radio button "Create a new class" in the dialog "Adding a Class"
- in the dit box, type the name of your class, for example "CDlgMyDialog"
- add the follwing directive in the file that will use you dialog box
#include "DlgMyDialog.h"
- add the following lines where you want to call you dialog box

      CDlgMyDialog Dlg;
      Dlg.DoModal();

If your resource and the dialog box code is in a dll, just add AFX_EXT_CLASS in your header file, like this :

class AFX_EXT_CLASS CDlgMyDialog : public CDialog

If just the resource is in your DLL, there's no problem, it'll work the same way.
Create your resource in the EXE, then, when it works correctly, move your resource from the EXE to the DLL resource file

0
 
Dmitry_Author Commented:
I want to work with the resource in the external DLL (loading by LoadLibrary function) and the rest of the code is in the exe.
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
danelroismanCommented:
From Codeguru.com
-------------------

It is nice to be able to make a DLL project that includes its own resources (such as a dialog resource) and then be able to call it from another project. Seems simple but I wnet nuts untill I found out the following:

In the DLL function that pops up the dialog you must manage the state so that the DLL code uses the DLL's resources.


extern __declspec(dllexport) void ShowEditDialog(int &MyData1, int &MyData2)
{
      //ensure we are using our own resources
      AFX_MANAGE_STATE(AfxGetStaticModuleState());
      CMyLocalDialog dlg;
      dlg.Arg1 = MyData1; //specific local data for MyLocalDialog
      dlg.Arg2 = MyData2;
      dlg.DoModal();
      MyData1 = dlg.Arg1; //data after processing
      MyData2 = dlg.Arg2;
}

Here's an update sent in by Johan Nilsson

The program calling this exported function will never be able to use the function GetLastError() to check for problems that may have occured (I know it from experience). This is because the AFX_MANAGE_STATE macro creates a temporary object on the stack which, when it is automatically destroyed on function exit, will clear any error codes set for the current thread (it calls into TlsGetValue, deep in MFC).

I've provided a workaround for this, which I've been using myself. Not so pretty, but it works. Example on how to fix the ShowEditDialog fn :


extern __declspec(dllexport) void ShowEditDialog(int &MyData1, int &MyData2)
{
      DWORD dwLastErr = NO_ERROR;

      //
      // surround the code in brackets, which will cause the temporary
      // object created by AFX_MANAGE_STATE to be destroyed before leaving
      // the exported function.
      //
      // NOTE : Do NOT call MFC code outside of these brackets.
      //
      {
            AFX_MANAGE_STATE(AfxGetStaticModuleState());
            CMyLocalDialog dlg;
            dlg.Arg1 = MyData1; //specific local data for MyLocalDialog
            dlg.Arg2 = MyData2;
            dlg.DoModal();
            MyData1 = dlg.Arg1; //data after processing
            MyData2 = dlg.Arg2;

            //
            // save possible errors
            //
            dwLastErr = ::GetLastError();
      }

      //
      // only set error if none is currently set.
      // (last error will always be NO_ERROR _unless_
      // TlsGetValue failed earlier)
      //
      if (::GetLastError() == NO_ERROR)
            ::SetLastError(dwLastErr);
}


0
 
Dmitry_Author Commented:
danelroisman:
I want to create a DLL that will conatin ONLY resource and no code (for multilangual support) and "tell" to my application use resource from this DLL.
You sollution does not allow me to create modalless dilaogs.
0
 
Dmitry_Author Commented:
Daniel,
Now in my applicaton, I'm usig modalless dialogs, using new operator for allocate memory for the dialog class and delete for free the memory.
In your solution, how to free memory?
0
 
danelroismanCommented:
It is not necessary for Win32-based applications to free resources loaded by using the LoadResource function. A resource is automatically freed when its module is unloaded. For Win16 applcation use FreeResource function.
The function is obsolete.

If you use "new CDialog" - simply delete the pointer on the end of function after you get all information from the resource dialog.

Thanx for the points.

Glad to help you,

Daniel
0
All Courses

From novice to tech pro — start learning today.