Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Load Dialog From a Resource DLL

Load Dialog From a Resource DLL

Scheduled Pinned Locked Moved C / C++ / MFC
comgraphicshelptutoriallearning
16 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G goldenrose9

    I need a sample of how to load dialog resource from a dll I found a sample here but it is not working. http://www.codeproject.com/Messages/1212280/Re-how-to-load-dialog-resource-from-a-dll.aspx

    // Load the resource dll
    HMODULE hModule = LoadLibrary("C:\\another_resource.dll");

    if(hModule != NULL)
    {
    // Loading your dialog
    HWND hDialog = CreateDialog(hModule, MAKEINTRESOURCE(ID_DIALOG), /* Handle to parent window */, /* Pointer to dialog procedure function */);

    // Loading your bitmap
    HBITMAP hBitmap = LoadBitmap(hModule, MAKEINTRESOURCE(IDB_BITMAP);

    FreeLibrary(hModule);
    }

    thanx in advance please help

    Some Day I Will Prove MySelf :: GOLD

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #3

    Is the resource ID ID_DIALOG in the calling program equal to what was used in the DLL? If you're not sure and you don't have the DLL's code to look it up, you could analyse the DLL with the resource hacker[^].

    G 1 Reply Last reply
    0
    • G goldenrose9

      I need a sample of how to load dialog resource from a dll I found a sample here but it is not working. http://www.codeproject.com/Messages/1212280/Re-how-to-load-dialog-resource-from-a-dll.aspx

      // Load the resource dll
      HMODULE hModule = LoadLibrary("C:\\another_resource.dll");

      if(hModule != NULL)
      {
      // Loading your dialog
      HWND hDialog = CreateDialog(hModule, MAKEINTRESOURCE(ID_DIALOG), /* Handle to parent window */, /* Pointer to dialog procedure function */);

      // Loading your bitmap
      HBITMAP hBitmap = LoadBitmap(hModule, MAKEINTRESOURCE(IDB_BITMAP);

      FreeLibrary(hModule);
      }

      thanx in advance please help

      Some Day I Will Prove MySelf :: GOLD

      A Offline
      A Offline
      Andrew Brock
      wrote on last edited by
      #4

      If you are using MFC you can create an instance of the dialog class from the DLL. Be sure to use MFC with dynamic linking in both the exe and dll. The way I did this in my project was: In the DLL have

      class MyDialog : public CDialog {
      //program this class as if it was part of the exe
      }

      Then an exported function to create and destroy the instance. Because in C++ the return type and parameters make up the function name, we should use C for exporting functions, however we cannot create classes in C. Any memory allocated from within a DLL should be deleted from within the DLL too, so we need a create and destroy function The following will create an instance of the class and return it through a C function and delete it when you are finished with it.

      //This function name is ?InternalGetDialog@@YAPEAXXZ if it was exported. Which is why we don't.
      void *InternalGetDialog() {
      return new MyDialog();
      }

      //This function name is ?InternalDeleteDialog@@YAXPEAVCDialog@@@Z if it was exported. Which is why we don't.
      //This is a generic function that will destroy any dialog created from this dll
      void InternalDeleteDialog(void *pDlg) {
      CDialog *pDialog = (CDialog *)pDialog; //cast so that we call the destructor
      delete pDialog;
      }

      extern "C" {
      //This function name is GetDialog when importing
      __declspec(dllexport) void *GetDialog() {
      return InternalGetDialog();
      }
      //This function name is DeleteDialog when importing
      __declspec(dllexport) void DeleteDialog(void *pDialog) {
      InternalDeleteDialog(pDialog);
      }
      }

      Then in the exe that displays the dialog

      typedef CDialog *(*GetDialogFunc)();
      typedef CDialog *(*DeleteDialogFunc)();

      void DisplayDynamicDialog() {
      HMODULE hModule = LoadLibrary("Resources.dll");
      if (hModule != NULL) {
      GetDialogFunc pGetDialogFunc = (GetDialogFunc)GetProcAddress(hModule, "GetDialog");
      DeleteDialogFunc pDeleteDialogFunc = (DeleteDialogFunc)GetProcAddress(hModule, "DeleteDialog");
      CDialog *pDialog = pGetDialogFunc();
      INT_PTR nRes = pDialog->DoModal();
      //check nRes, process what is on the dialog, ...
      pDeleteDialogFunc(pDialog);
      FreeLibrary(hModule);
      }
      }

      G 1 Reply Last reply
      0
      • C Cool_Dev

        please mention what is among LoadLibrary, CreateDialog and LaodBitmap is not working.

        G Offline
        G Offline
        goldenrose9
        wrote on last edited by
        #5

        create dialog.. Is required, I have the correct dialog id and i m able to load the dll using loadlibrary(). After this i m confused what to do next, so i need a small example that demonstrate how to show the dialog from resource dll. Thanx in advance.

        Some Day I Will Prove MySelf :: GOLD

        C 1 Reply Last reply
        0
        • A Andrew Brock

          If you are using MFC you can create an instance of the dialog class from the DLL. Be sure to use MFC with dynamic linking in both the exe and dll. The way I did this in my project was: In the DLL have

          class MyDialog : public CDialog {
          //program this class as if it was part of the exe
          }

          Then an exported function to create and destroy the instance. Because in C++ the return type and parameters make up the function name, we should use C for exporting functions, however we cannot create classes in C. Any memory allocated from within a DLL should be deleted from within the DLL too, so we need a create and destroy function The following will create an instance of the class and return it through a C function and delete it when you are finished with it.

          //This function name is ?InternalGetDialog@@YAPEAXXZ if it was exported. Which is why we don't.
          void *InternalGetDialog() {
          return new MyDialog();
          }

          //This function name is ?InternalDeleteDialog@@YAXPEAVCDialog@@@Z if it was exported. Which is why we don't.
          //This is a generic function that will destroy any dialog created from this dll
          void InternalDeleteDialog(void *pDlg) {
          CDialog *pDialog = (CDialog *)pDialog; //cast so that we call the destructor
          delete pDialog;
          }

          extern "C" {
          //This function name is GetDialog when importing
          __declspec(dllexport) void *GetDialog() {
          return InternalGetDialog();
          }
          //This function name is DeleteDialog when importing
          __declspec(dllexport) void DeleteDialog(void *pDialog) {
          InternalDeleteDialog(pDialog);
          }
          }

          Then in the exe that displays the dialog

          typedef CDialog *(*GetDialogFunc)();
          typedef CDialog *(*DeleteDialogFunc)();

          void DisplayDynamicDialog() {
          HMODULE hModule = LoadLibrary("Resources.dll");
          if (hModule != NULL) {
          GetDialogFunc pGetDialogFunc = (GetDialogFunc)GetProcAddress(hModule, "GetDialog");
          DeleteDialogFunc pDeleteDialogFunc = (DeleteDialogFunc)GetProcAddress(hModule, "DeleteDialog");
          CDialog *pDialog = pGetDialogFunc();
          INT_PTR nRes = pDialog->DoModal();
          //check nRes, process what is on the dialog, ...
          pDeleteDialogFunc(pDialog);
          FreeLibrary(hModule);
          }
          }

          G Offline
          G Offline
          goldenrose9
          wrote on last edited by
          #6

          thanx 4 ur reply with sample but i need the sample in win32 way, my program don't use MFC.

          Some Day I Will Prove MySelf :: GOLD

          1 Reply Last reply
          0
          • L Lost User

            Is the resource ID ID_DIALOG in the calling program equal to what was used in the DLL? If you're not sure and you don't have the DLL's code to look it up, you could analyse the DLL with the resource hacker[^].

            G Offline
            G Offline
            goldenrose9
            wrote on last edited by
            #7

            i have the correct id, i need a small code to show the dialog from reource dll in win32 way

            Some Day I Will Prove MySelf :: GOLD

            1 Reply Last reply
            0
            • G goldenrose9

              I need a sample of how to load dialog resource from a dll I found a sample here but it is not working. http://www.codeproject.com/Messages/1212280/Re-how-to-load-dialog-resource-from-a-dll.aspx

              // Load the resource dll
              HMODULE hModule = LoadLibrary("C:\\another_resource.dll");

              if(hModule != NULL)
              {
              // Loading your dialog
              HWND hDialog = CreateDialog(hModule, MAKEINTRESOURCE(ID_DIALOG), /* Handle to parent window */, /* Pointer to dialog procedure function */);

              // Loading your bitmap
              HBITMAP hBitmap = LoadBitmap(hModule, MAKEINTRESOURCE(IDB_BITMAP);

              FreeLibrary(hModule);
              }

              thanx in advance please help

              Some Day I Will Prove MySelf :: GOLD

              A Offline
              A Offline
              Andrew Brock
              wrote on last edited by
              #8

              Firstly you need a message handler, essentialy the same as your main message handler, usually WndProc

              BOOL CALLBACK DlgProc(HWND hDlg, UINT nMessage, WPARAM wParam, LPARAM lParam) {
              switch (nMessage) {
              case WM_INITDIALOG:
              //This is where you do all your initialisation. This is the first place after all the controls have been created, so you can use GetDlgItem()
              return TRUE;

              	case WM\_COMMAND:
              		//This is the same as it is in the WndProc, handle button clicks, etc...
              		switch (LOWORD(wParam)) {
              			case IDOK: //This is usually the OK or Done button
              				return TRUE; 
              
              			case IDCANCEL: //This is usually the Close or Cancel button
              				DestroyWindow(hDlg); //close the dialog
              				return TRUE;
              		}
              		break;
                      } 
              } 
              return FALSE; 
              

              }

              Then you just need to use that as the "procedure function"

              void DisplayDynamicDialog(HWND hParent) {
              HMODULE hModule = LoadLibrary("Resources.dll");
              if(hModule != NULL) {
              hDialog = CreateDialog(hModule, MAKEINTRESOURCE(ID_DIALOG), hParent, (DLGPROC)DlgProc);
              ShowWindow(hDialog, SW_SHOW);
              FreeLibrary(hModule); //This might need to be called later, after the dialog has been closed since it is modeless
              }
              }

              Finally, just to reiterate what Thaddeus Jones said, the ID you supply needs to be the ID defined in the DLL, not the ID defined in the exe, hence the resource.h that you include needs to be from the dll. To avoid confusion, I recommend using quoted string names instead.

              G 1 Reply Last reply
              0
              • G goldenrose9

                I need a sample of how to load dialog resource from a dll I found a sample here but it is not working. http://www.codeproject.com/Messages/1212280/Re-how-to-load-dialog-resource-from-a-dll.aspx

                // Load the resource dll
                HMODULE hModule = LoadLibrary("C:\\another_resource.dll");

                if(hModule != NULL)
                {
                // Loading your dialog
                HWND hDialog = CreateDialog(hModule, MAKEINTRESOURCE(ID_DIALOG), /* Handle to parent window */, /* Pointer to dialog procedure function */);

                // Loading your bitmap
                HBITMAP hBitmap = LoadBitmap(hModule, MAKEINTRESOURCE(IDB_BITMAP);

                FreeLibrary(hModule);
                }

                thanx in advance please help

                Some Day I Will Prove MySelf :: GOLD

                A Offline
                A Offline
                Abhi Lahare
                wrote on last edited by
                #9

                you can also use combination of LoadResource[^] and FindResouce[^]

                1 Reply Last reply
                0
                • G goldenrose9

                  I need a sample of how to load dialog resource from a dll I found a sample here but it is not working. http://www.codeproject.com/Messages/1212280/Re-how-to-load-dialog-resource-from-a-dll.aspx

                  // Load the resource dll
                  HMODULE hModule = LoadLibrary("C:\\another_resource.dll");

                  if(hModule != NULL)
                  {
                  // Loading your dialog
                  HWND hDialog = CreateDialog(hModule, MAKEINTRESOURCE(ID_DIALOG), /* Handle to parent window */, /* Pointer to dialog procedure function */);

                  // Loading your bitmap
                  HBITMAP hBitmap = LoadBitmap(hModule, MAKEINTRESOURCE(IDB_BITMAP);

                  FreeLibrary(hModule);
                  }

                  thanx in advance please help

                  Some Day I Will Prove MySelf :: GOLD

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  Firstly your call to CreateDialog() is incomplete, so this code will not even compile. Secondly are you sure you are making the right call (i.e. is this a Modal or Modeless dialog)? Take a look here[^] for some more information.

                  I must get a clever new signature for 2011.

                  G 1 Reply Last reply
                  0
                  • G goldenrose9

                    create dialog.. Is required, I have the correct dialog id and i m able to load the dll using loadlibrary(). After this i m confused what to do next, so i need a small example that demonstrate how to show the dialog from resource dll. Thanx in advance.

                    Some Day I Will Prove MySelf :: GOLD

                    C Offline
                    C Offline
                    Cool_Dev
                    wrote on last edited by
                    #11

                    see from msdn documentation http://msdn.microsoft.com/en-us/library/ms644996%28v=vs.85%29.aspx#modeless_box[^]

                    G 1 Reply Last reply
                    0
                    • L Lost User

                      Firstly your call to CreateDialog() is incomplete, so this code will not even compile. Secondly are you sure you are making the right call (i.e. is this a Modal or Modeless dialog)? Take a look here[^] for some more information.

                      I must get a clever new signature for 2011.

                      G Offline
                      G Offline
                      goldenrose9
                      wrote on last edited by
                      #12

                      i know it is incomplete, therefore i m in need of a sample in win32 way.

                      Some Day I Will Prove MySelf :: GOLD

                      L 1 Reply Last reply
                      0
                      • C Cool_Dev

                        see from msdn documentation http://msdn.microsoft.com/en-us/library/ms644996%28v=vs.85%29.aspx#modeless_box[^]

                        G Offline
                        G Offline
                        goldenrose9
                        wrote on last edited by
                        #13

                        thanx, but i need help in displaying a dialog from a resouce dll.

                        Some Day I Will Prove MySelf :: GOLD

                        1 Reply Last reply
                        0
                        • A Andrew Brock

                          Firstly you need a message handler, essentialy the same as your main message handler, usually WndProc

                          BOOL CALLBACK DlgProc(HWND hDlg, UINT nMessage, WPARAM wParam, LPARAM lParam) {
                          switch (nMessage) {
                          case WM_INITDIALOG:
                          //This is where you do all your initialisation. This is the first place after all the controls have been created, so you can use GetDlgItem()
                          return TRUE;

                          	case WM\_COMMAND:
                          		//This is the same as it is in the WndProc, handle button clicks, etc...
                          		switch (LOWORD(wParam)) {
                          			case IDOK: //This is usually the OK or Done button
                          				return TRUE; 
                          
                          			case IDCANCEL: //This is usually the Close or Cancel button
                          				DestroyWindow(hDlg); //close the dialog
                          				return TRUE;
                          		}
                          		break;
                                  } 
                          } 
                          return FALSE; 
                          

                          }

                          Then you just need to use that as the "procedure function"

                          void DisplayDynamicDialog(HWND hParent) {
                          HMODULE hModule = LoadLibrary("Resources.dll");
                          if(hModule != NULL) {
                          hDialog = CreateDialog(hModule, MAKEINTRESOURCE(ID_DIALOG), hParent, (DLGPROC)DlgProc);
                          ShowWindow(hDialog, SW_SHOW);
                          FreeLibrary(hModule); //This might need to be called later, after the dialog has been closed since it is modeless
                          }
                          }

                          Finally, just to reiterate what Thaddeus Jones said, the ID you supply needs to be the ID defined in the DLL, not the ID defined in the exe, hence the resource.h that you include needs to be from the dll. To avoid confusion, I recommend using quoted string names instead.

                          G Offline
                          G Offline
                          goldenrose9
                          wrote on last edited by
                          #14

                          :thumbsup: thanks a lot, worked like a charm, once again thanx.

                          Some Day I Will Prove MySelf :: GOLD

                          1 Reply Last reply
                          0
                          • G goldenrose9

                            i know it is incomplete, therefore i m in need of a sample in win32 way.

                            Some Day I Will Prove MySelf :: GOLD

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #15

                            OK so what part of this[^] are you having trouble with?

                            I must get a clever new signature for 2011.

                            G 1 Reply Last reply
                            0
                            • L Lost User

                              OK so what part of this[^] are you having trouble with?

                              I must get a clever new signature for 2011.

                              G Offline
                              G Offline
                              goldenrose9
                              wrote on last edited by
                              #16

                              sir i had received the answer, thanx for giving ur valuable time sir.

                              Some Day I Will Prove MySelf :: GOLD

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups