Child Window and Dialog Box Procedures

Child window dialog boxes are an integral feature of Windows applications, in both 3.x and 98/95/NT. Those converting applications from the older version to the newer versions will be happy to know that the DialogBox function can be used precisely as it has been in the past. The following subprocedure might be used by any version to call a resource dialog box titled SELECTFILE, with message-handling responses provided by the exported procedure: FileSelectDlgProc.

int DialogProc( HWND hwnd, HINSTANCE hInst )
{
    static FARPROC  lpProc;                    // 3.x, 98/95/NT
    int    iReturn;
    lpProc = MakeProcInstance( FileSelectDlgProc, hInst );
    iReturn = DialogBox( hInst, “SELECTFILE”, hwnd,  lpProc );
    FreeProcInstance( lpProc );
    return( iReturn );
}

Windows 98/95/NT, however, introduces a few changes that make the preceding code not only unnecessarily verbose, but also wastefully redundant.

The first change is in the MakeProcInstance API function call. While this API call is both functional and necessary under Windows 3.x, under 98/95/NT, MakeProcInstance has become a macro. This macro, quite simply, returns the first argument … and does nothing more. Thus, calling MakeProcInstance under 98/95/NT, aside from providing backward compatibility, has the singularly useless effect of making lpProc equal to FileSelectDlgProc.

Likewise, the FreeProcInstance function has also become a macro. However, in this case, the macro has even less real value, since FreeProcInstance does precisely nothing.

The upshot of this is that the lpProc variable can be replaced in the DialogBox API call with FileSelectDlgProc reference, without needing to declare the lpProc variable or call MakeProcInstance at all. And, of course, without calling FreeProcInstance afterward.

The second change has to do with the FreeProcInstance API call. Again, under Windows 3.x, this API function was necessary and operational, used to release memory allocated by the DialogBox API call for the child window dialog box. But under Windows 98/95/NT, the FreeProcInstance call has also been redefined, again to provide backward compatibility, as a macro that does precisely nothing.

Therefore, given these two revisions in MakeProcInstance and FreeProcInstance, the DialogProc function can be rewritten:

int DialogProc( HWND hwnd, HINSTANCE hInst )
{
    int    iReturn;                             // 98/95/NT only
    iReturn = DialogBox( hInst, “SELECTFILE”, hwnd,
                         FileSelectDlgProc );
    return( iReturn );
}

Given the modification shown, you could simplify this subprocedure even further by omitting the iReturn variable in favor of a direct return. Or, you could discard it entirely in favor of calling the DialogBox API directly, unless there were other tasks to be accomplished before or after calling the DialogBox function, as there often are. But, in either case, the process of calling a dialog box is much simpler in Windows 98/95/NT than in 3.x.

Remember, this revision is optional. Windows 3.x subprocedures will still compile and execute under 98/95/NT without changes.

© 1998 SYBEX Inc. All rights reserved.