February 6th, 2007

Why can't I create my dialog box? Rookie mistake #1

Each dialog box resource is specified either by an integer ordinal or by a string name. But a simple typo will turn one into the other.

#define DLG_OPEN 1
#define DLG_WARN_REMOVEABLE 2
DLG_OPEN DIALOG 32, 32, 267, 73
...
BEGIN
  ...
END
DLG_WARN_REMOVABLE DIALOG 32, 32, 267, 73
...
BEGIN
  ...
END
DialogBox(hInstance, TEXT("DLG_OPEN"),
          hwnd, OpenDialogProc);
DialogBox(hInstance, MAKEINTRESOURCE(DLG_WARN_REMOVEABLE),
          hwnd, WarnRemoveableDialogProc);

Do you see the two “classic rookie mistakes”?

It may be easier to spot if you take the resource file and send it through the preprocessor first:

1 DIALOG 32, 32, 267, 73
...
BEGIN
  ...
END
DLG_WARN_REMOVABLE DIALOG 32, 32, 267, 73
...
BEGIN
  ...
END

The first call to DialogBox passes TEXT("DLG_OPEN") as the resource name. But notice that there is no resource with that name. The preprocessor turned DLG_OPEN into 1 thanks to the line #define DLG_OPEN 1 in the header file. Therefore, the call to DialogBox fails since there is no dialog box named DLG_OPEN. The dialog box you want goes by the integer name 1.

DialogBox(hInstance, MAKEINTRESOURCE(DLG_OPEN),
          hwnd, OpenDialogProc);

The second mistake is more subtle. Notice that the name of the second dialog is spelled inconsistently. The header file calls it DLG_WARN_REMOVEABLE, but the resource file calls it DLG_WARN_REMOVABLE. As a result, the preprocessor macro is not invoked, and the result is a dialog that goes by the string name TEXT("DLG_WARN_REMOVABLE"). However, the code asks for MAKEINTRESOURCE(DLG_WARN_REMOVEABLE), which doesn’t exist.

To fix the second issue, you first have to decide what you really wanted. You probably wanted an integer dialog resource, in which case the fix is to correct the resource file:

DLG_WARN_REMOVEABLE DIALOG 32, 32, 267, 73

On the other hand, if you really wanted the dialog box to be a named resource (note: this is extremely rare), then you need to request it by name:

DialogBox(hInstance, TEXT("DLG_WARN_REMOVABLE"),
          hwnd, WarnRemoveableDialogProc);

We’ll look at a few more “rookie mistakes” over the next couple of days.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.