Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

DialogBox Problem

Posted on 2005-02-27
4
Medium Priority
?
976 Views
Last Modified: 2010-08-05
In one COM object project (DLL, in-proc, no MFC
support) I need to show a dialog box. I added the box in the resources and everything is fine. Till that moment I'm using the default resource name IDD_DIALOG1.

Because I don't have MFC, I need to use Win32 DialogBox function (I need modal dialog).
So I wrote something like this:

int nRes = DialogBox(NULL,
MAKEINTRESOURCE(IDD_DIALOG1), NULL,
(DLGPROC)DlgFunc);
DWORD dwErr = GetLastError();
      .
      .
      .


LRESULT CALLBACK DlgFunc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam ) {
      switch( message )
      {
            case WM_INITDIALOG:
                        return TRUE;

            case WM_COMMAND:
                  if (LOWORD(wParam) == IDOK || LOWORD(wParam) ==
IDCANCEL)
                  {
                        EndDialog(hDlg, LOWORD(wParam));
                        return TRUE;
                  }
                  break;
      }
    return FALSE;
}


When I debug this fragment of code, I receive, that nRes = -1; dwErr = 1814; // ERROR_RESOURCE_NAME_NOT_FOUND
When I try the same code in a normal Wi32 App, everything is fine and I see the dialog box.
I also tried to put real hwnd and hinst in DialogBox function - the same result
Can you help me please how can I show my dialog box?
0
Comment
Question by:Neoliten
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 1000 total points
ID: 13413604
First parameter of DialogBox function is:
hInstance
[in] Handle to the module whose executable file contains the dialog box template.

When you set this parameter to NULL, program tries to search for IDD_DIALOG1 in the caller executable resources. You need to set here COM Dll instance. I don't remember where can you get it. For example, in non-COM Dll hInstance is passed as parameter to DllMain and may be kept in global variable for later use. I think something similar must be also in COM Dll.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13415353
AlexFM is right about the instance handle. You can obtain it by calling 'GetModuleHandle()' with a NULL parameter, e.g.

HINSTANCE hInst;

hInst = (HINSTANCE) GetModuleHandle ( NULL);

(HINSTANCE and HMODULE are in fact the same).

Pass this handle to 'DialogBox()' and it shoud work.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13417680
lpModuleName
If this parameter is NULL, GetModuleHandle returns a handle to the file used to create the calling process.

??
0
 
LVL 6

Expert Comment

by:stsanz
ID: 13418486
Your COM DLL should have a DLLMain function which is called on DLL loading.
Just memorize the hinstDLL parameter and use it in the DialogBox call.

HINSTANCE  g_hinstDLL ;

BOOL WINAPI DllMain(
  HINSTANCE hinstDLL,
  DWORD fdwReason,
  LPVOID lpvReserved
)
{
  switch (fdwReason)
  {
    case DLL_PROCESS_ATTACH:
      g_hinstDLL = hinstDLL ;
      break ;
    default:
      break ;
  }
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question