0

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.

MarkyMark
  • 93
  • 1
  • 7
  • 1
    ...and after trying to show the window you immediately exit. [See here for how to loop](https://msdn.microsoft.com/en-us/library/windows/desktop/ff381409(v=vs.85).aspx). – Ken Y-N Jul 01 '16 at 08:16
  • You should not change the default setting for the character set from Unicode to MBCS. If you aren't using Unicode, you are needlessly wasting resources to implement an application with limited features. – IInspectable Jul 01 '16 at 08:33
  • 1
    **Any** example program you find that creates a window will include a message loop. How did you manage to overlook this? – Jonathan Potter Jul 01 '16 at 08:45
  • *Cannot find or open the PDB file* Visual Studio cannot find debugging information. See this [link](http://stackoverflow.com/a/12954908/4603670) There are other problems with your code - mentioned below - but that's not this error. – Barmak Shemirani Jul 01 '16 at 08:46
  • https://msdn.microsoft.com/en-us/library/bb384843.aspx – Cody Gray - on strike Jul 01 '16 at 14:38

1 Answers1

5

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;
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    The erroneous window procedure returns `0` for [WM_NCCREATE](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632635.aspx), causing the call to `CreateWindowEx` to return `NULL`. This can be easily diagnosed when checking for errors, as you suggest. The window doesn't show up, because it never gets off the ground. – IInspectable Jul 01 '16 at 08:31