?
Solved

DialogBox Problem

Posted on 2005-02-27
4
Medium Priority
?
963 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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

752 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