null
null

Reputation: 807

Creating and Loading DialogBox from DLL

I've created a dialog box inside a Win32 DLL (using resource editor) and now want to show it as application program (using this DLL) calls DisplayDialog, but it is not working.

// AppProgram.cpp
...
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch (msg) {
    case WM_COMMAND:
      switch (LOWORD (wParam)) {
          case IDM_FILE_NEW_DIALOG:
              DisplayDialog (hInst, hWnd);
              break;
          ...
      }
      break;
      ....
  }
 return DefWindowProc(hWnd, msg, wParam, lParam);
}

My DLL appears like

#include "stdafx.h"
#include "myDLL.h"

EXPORT BOOL CALLBACK DisplayDialog (HINSTANCE hInst, HWND hWnd) {
   DialogBox (hInst, MAKEINTRESOURCE (IDD_DIALOG1), hWnd, reinterpret_cast<DLGPROC> (DiagProc));
   // MessageBox works here
}
...

I've tested that this DLL displays dialog if the dialog belongs to AppProgram. Here, I want to display dialog when it is a part of DLL.

Please suggest whether we should create dialog inside DLL or should pass it from program. + how to show dialog in given scenario. Thanks in advance.

Upvotes: 0

Views: 2992

Answers (2)

Raymond Chen
Raymond Chen

Reputation: 45173

The hInst parameter is the handle to the module that contains the dialog resource. If you want to get the dialog from the DL's resourcesL, then pass the handle to the DLL rather than the handle to the main application.

Upvotes: 1

Simon Mourier
Simon Mourier

Reputation: 138950

Something like this:

HMODULE module  = LoadLibrary("MyDll.dll");
HRSRC res = FindResource(module, "#1234", RT_DIALOG);
DLGTEMPLATE* pTemplate = (DLGTEMPLATE*)LoadResource(module, res);
DialogBoxIndirect(0, pTemplate, hwnd, dlgproc);

Upvotes: 0

Related Questions