views:

53

answers:

2

I was programming the example code from Frank Luna's book "Introduction to 3D Game Programming with DirectX 10". The code is the first Win32 example in the Appendix A: Windows Programming section.

Right now, the program compiles under both VC++ 2008/2010, but no window appears, although the debug session has started and I have to forcefully close it. I have no idea where it is, I'm not using Win32 Console mode, I have closed all other windows and no other IDE or session of VC++ is running.

Any idea why this might be happening?

PS: I have also checked my Processes. It is indeed running.

    #include <Windows.h>

    HWND ghMainWnd = 0;

    bool InitWindowsApp(HINSTANCE instanceHandle, int show);
    int Run();

    LRESULT CALLBACK
        WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

    int WINAPI 
        WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nShowCmd )
    {
        if( !InitWindowsApp(hInstance, nShowCmd) )
            return 0;

        return Run();
    }

    bool InitWindowsApp( HINSTANCE instanceHandle, int show )
    {
        WNDCLASS wc;

        wc.style         = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = instanceHandle;
        wc.hIcon         = LoadIcon(0, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(0, IDC_ARROW );
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wc.lpszMenuName  = 0;
        wc.lpszClassName = L"BasicWndClass";

        if( !RegisterClass(&wc) ) 
        {
            MessageBox(0, L"RegisterClass FAILED", 0, 0);
            return false;
        }

        ghMainWnd = CreateWindow(
            L"BasicWndClass",
            L"Win32Basic",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            0,
            0,
            instanceHandle,
            0);

        if( ghMainWnd = 0 )
        {
            MessageBox( 0, L"Window Creation FAILED", 0, 0 );
            return false;
        }

        ShowWindow( ghMainWnd, show );
        UpdateWindow( ghMainWnd );

        return true;
    }

    int Run()
    {
        MSG msg = {0};
        BOOL bRet = 1;

        while( bRet = GetMessage( &msg, 0, 0, 0 ) != 0 )
        {
            if( bRet == -1 )
            {
                MessageBox( 0, L"GetMessage FAILED", 0, 0 );
                break;
            }
            else
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }

        return (int)msg.wParam;
    }

    LRESULT CALLBACK
        WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
    {
        switch( msg )
        {
        case WM_LBUTTONDOWN:
            MessageBox( 0, L"Hello, World", 0, 0 );
            return 0;

        case WM_KEYDOWN:
            if( wParam == VK_ESCAPE )
                DestroyWindow( ghMainWnd );
            return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }

        return DefWindowProc(hWnd, msg, wParam, lParam);
    }
+1  A: 

Wild guess: _UNICODE is not defined by project settings. Use CreateWindowW, RegisterClassW, to avoid dependency.

Pavel Radzivilovsky
Thanks for the suggestion, but this doesn't work. However, I'll keep that in mind in case I ever run into such a problem ^^ (I also checked, Unicode is defined by project settings in both versions of VC++)
SoulBeaver
WinMain parameters seem wrong though (hardly related). the command line is PTSTR. Perhaps your winmain is not even called?
Pavel Radzivilovsky
PSTR is indeed what's written in the book. And kirk identified what the problem was. VC++ 2010 identifies PSTR as `CHAR *PSTR` and PTSTR as `typedef LPWSTR PTSTR`. So instead of a string the example uses a char *. Hope that helped ^^
SoulBeaver
+4  A: 

Change this:

if( ghMainWnd = 0 ) 
        { 
            MessageBox( 0, L"Window Creation FAILED", 0, 0 ); 
            return false; 
        }

to:

if( ghMainWnd == 0 ) 
        { 
            MessageBox( 0, L"Window Creation FAILED", 0, 0 ); 
            return false; 
        } 

Two equals signs instead of one. :)

kirk.burleson
DAMN IT. I hate those mistakes! Gah! Works now, accepting your answer. Thanks a lot!
SoulBeaver
Use: `if (0 == ghMainWnd) // ...` instead to prevent problems like this -- if you leave out the second `=`, it won't compile (since that would try to assign some value to `0`, which isn't allowed).
Jerry Coffin
+1 for a good idea. Adopting this immediately.
SoulBeaver