Search code examples
c++winapivisual-studio-2013window

Why isn't my Win32(c++, visual studio 2013) Window showing up?


Here is the code:

#include <windows.h>
#include <windowsx.h>


// the WindowProc callback function prototype
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);

// win32 entry point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
    ///*
    // window handler
    HWND hwnd;

    // struct for window information
    WNDCLASSEX wc;

    // clear out the window class for use
    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    // setting wc struct with window values/properties
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpszClassName = "TestWindow";
    wc.lpfnWndProc = WindowProc;

    // register window before creation/use
    RegisterClassEx(&wc);

    // creating the window class
    hwnd = CreateWindowEx(NULL, "TestWindow", "First Win32 Program", WS_OVERLAPPEDWINDOW, 100, 100, 1000, 1000, NULL, NULL, hInstance, NULL);

    // show the window 
    ShowWindow(hwnd, nCmdShow);//*/

}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam){
    return 0;
}

Here is the compiler/debug/runtime console output:

'wintest.exe' (Win32): Loaded 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\lpk.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\usp10.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\uxtheme.dll'. Cannot find or open the PDB file.
The program '[3484] wintest.exe' has exited with code 0 (0x0).

It's a very basic simple program, and all it does is suppose to pop open an win32 window of size 1000x1000 starting at upper left corner.


Solution

  • The first mistake that I can see is in your window procedure. Any messages that you do not explicitly handle should be passed on to DefWindowProc.

    LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
    {
        return DefWindowProc(hwnd, message, wparam, lparam);
    }
    

    This is the absolute minimum you need for a window procedure. In reality you'll want to handle at least WM_CLOSE and WM_DESTROY, and then more than that as you add functionality.

    Your broken window procedure causes CreateWindowEx to fail when it sends the WM_NCCREATE message to the window during creation.

    Another glaring problem is that you don't perform any error checking at all. By ignoring the return values of every API call you make, you have no means whatsoever of diagnosing where your program is failing.

    And finally, you don't include a message loop. Thus even if you can get your window to show up, the process will immediately terminate.

    This program will show a window. Clearly there is more work to be done, but it is a start.

    #include <windows.h>
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
    {
        switch(message)
        {
            case WM_CLOSE:
                DestroyWindow(hwnd);
                return 0;
            case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
        }
        return DefWindowProc(hwnd, message, wparam, lparam);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, 
        int nCmdShow)
    {
        WNDCLASSEX wc = { sizeof(WNDCLASSEX) };
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.hInstance = hInstance;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.lpszClassName = "TestWindow";
        wc.lpfnWndProc = WindowProc;
    
        if (!RegisterClassEx(&wc))
            return 1;
    
        HWND hwnd = CreateWindowEx(0, "TestWindow", "First Win32 Program", 
            WS_OVERLAPPEDWINDOW, 100, 100, 1000, 1000, NULL, NULL, hInstance, NULL);
        if (!hwnd)
            return 1;
    
        ShowWindow(hwnd, nCmdShow);
    
        MSG msg;
        while (GetMessage(&msg, 0, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return 0;
    }