FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Dobbs M-Dev
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
November 01, 2002

An Application-Crashing Windows Bug

(Page 3 of 4)
November 2002/User Interface Programming

Listing 1 Source code for the crash-test application


#include <windows.h>
#include "resource.h"

#ifdef USE_DIALOG

BOOL CALLBACK mainDlgProc(
    HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    if ( WM_COMMAND == msg ) {
#ifdef CRASH
        DestroyWindow( hwnd );
#else
        EndDialog( hwnd, 0 );
#endif
        return TRUE;
    }
    return FALSE;
}

#else // USE_DIALOG

LRESULT CALLBACK mainWndProc(
    HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    int i;
    static BOOL isInside = FALSE;

    switch ( msg ) {
    case WM_CREATE:
        for ( i = 0; i < 8; i++ ) {
            char buf[ 20 ];
            wsprintf( buf, "Button %d", i + 1 );
            CreateWindowEx( 0, "Button", buf,
                WS_CHILD | WS_VISIBLE | WS_TABSTOP,
                40, 20 + i * 30, 100, 18,
                hwnd, (HMENU) 101 + i,
                GetModuleHandle( 0 ), 0 );
        }
        break;

    case WM_COMMAND:
        OutputDebugString( "***WM_COMMAND\r\n" );
        if ( !isInside ) {
            isInside = TRUE;
#ifdef CRASH
            DestroyWindow( hwnd );
#else
            PostMessage( hwnd, WM_CLOSE, 0, 0 );
#endif
            isInside = FALSE;
        }
        break;

    case WM_DESTROY:
        PostQuitMessage( EXIT_SUCCESS );
        break;

    default:
        return DefWindowProc( hwnd, msg , wParam, lParam );
    }

    return 0;
}

#endif // USE_DIALOG

int WINAPI WinMain( HINSTANCE hinst,
    HINSTANCE unused1, LPSTR unused2, int unused3 )
{
#ifdef USE_DIALOG
    return DialogBox( hinst, MAKEINTRESOURCE( IDD_DIALOG ),
        HWND_DESKTOP, mainDlgProc );
#else
    HWND hwndMain;
    WNDCLASSEX wc = { 0 };
    MSG msg;

    wc.cbSize = sizeof( WNDCLASSEX );
    wc.lpszClassName = "MainWindow";
    wc.lpfnWndProc = mainWndProc;
    wc.hCursor = LoadCursor( 0, IDC_ARROW );
    wc.hbrBackground = (HBRUSH)( COLOR_BTNFACE + 1 );
    wc.hInstance = GetModuleHandle( 0 );
    RegisterClassEx( &wc );

    hwndMain = CreateWindowEx( 0,
        "MainWindow","Main window",
        WS_VISIBLE | WS_OVERLAPPEDWINDOW,
        30, 30, 200, 300, 0, 0, hinst, 0 );

    while ( GetMessage( &msg, 0, 0, 0 ) ) {
        if ( !IsDialogMessage( hwndMain, &msg ) ) {
            TranslateMessage( &msg );
            DispatchMessage ( &msg );
        }
    }

    return msg.wParam;
#endif
}

Previous Page | 1 | 2 | 3 | 4 Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK