Call to CreateWindow failed!

I am following this tutorial: https://docs.microsoft.com/en-us/cpp/windows/walkthrough-creating-windows-desktop-applications-cpp?view=msvc-160

I must have made a mistake because "Call to CreateWindow failed!" pops up. I am overlooking it and would appreciate some guidance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <windows.h>
#include <tchar.h>

int CALLBACK WinMain(
	_In_ HINSTANCE hInstance,
	_In_opt_ HINSTANCE hPrevInstance,
	_In_ LPSTR     lpCmdLine,
	_In_ int       nCmdShow
);

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

LRESULT CALLBACK WndProc(
	_In_ HWND   hWnd,
	_In_ UINT   message,
	_In_ WPARAM wParam,
	_In_ LPARAM lParam
) {
	return 0;
}

static TCHAR szTitle[] = _T("Hello Windows Desktop");
static TCHAR szWindowClass[] = _T("Hello");

void setUpWindowClass(WNDCLASSEX* const wcex, const HINSTANCE hInstance);

int CALLBACK WinMain(_In_ HINSTANCE hInstance,	
	_In_opt_ HINSTANCE hPrevInstance,
	_In_ LPSTR     lpCmdLine,
	_In_ int       nCmdShow){
	
	WNDCLASSEX wcex;
	setUpWindowClass(&wcex, hInstance);
	if (!RegisterClassEx(&wcex)){
		MessageBox(NULL,
			_T("Call to RegisterClassEx failed!"),
			NULL,
			NULL);
		return 1;
	}
	HWND hWnd = CreateWindow(
		szWindowClass,
		szTitle,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT,
		500, 100,
		NULL,
		NULL,
		hInstance,
		NULL
	);
	if (!hWnd){
		MessageBox(NULL,
			_T("Call to CreateWindow failed!"),
			NULL,
			NULL);
		return 1;
	}
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
	return 0;
}

void setUpWindowClass(WNDCLASSEX * const wcex, const HINSTANCE hInstance) {
	wcex->cbSize = sizeof(WNDCLASSEX);
	wcex->style = CS_HREDRAW | CS_VREDRAW;
	wcex->lpfnWndProc = WndProc;
	wcex->cbClsExtra = 0;
	wcex->cbWndExtra = 0;
	wcex->hInstance = hInstance;
	wcex->hIcon = LoadIcon(hInstance, IDI_APPLICATION);
	wcex->hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex->hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcex->lpszMenuName = NULL;
	wcex->lpszClassName = szWindowClass;
	wcex->hIconSm = LoadIcon(wcex->hInstance, IDI_APPLICATION);
}


Update: I returned 1 from the WndProc method, but now nothing gets displayed.

Actually, if I put a breakpoint at return 0; then a pane appears on the task bar and hovering over it displays an clear window with nothing. Nothing appears on the screen when it is clicked.
Last edited on
I figured it out:

wcex->lpfnWndProc = WndProc;

should be

wcex->lpfnWndProc = DefWindowProc;

Also, later in the tutorial it gives code to display the window properly with
1
2
3
4
5
6
7
LRESULT CALLBACK WndProc(
	_In_ HWND   hWnd,
	_In_ UINT   message,
	_In_ WPARAM wParam,
	_In_ LPARAM lParam) {
        return DefWindowProc(hWnd, message, wParam, lParam);
}
Last edited on
Took me about a half hour to figure out what was going on (I'm retired and don't code much anymore), but finally got this working...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// cl Test.cpp /O1 /Os kernel32.lib user32.lib
#include <windows.h>
#include <tchar.h>


LRESULT CALLBACK WndProc(_In_ HWND   hWnd, _In_ UINT message, _In_ WPARAM wParam, _In_ LPARAM lParam) 
{
 if(message==WM_DESTROY)
 { 
    PostQuitMessage(0);
    return 0;
 }   
 
 return (DefWindowProc(hWnd, message, wParam, lParam));
}


int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
 TCHAR szTitle[] = _T("Hello Windows Desktop");
 TCHAR szWindowClass[] = _T("Hello");
 WNDCLASSEX wcex;
 MSG messages;
 
 wcex.cbSize = sizeof(WNDCLASSEX);
 wcex.style = CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc = WndProc;
 wcex.cbClsExtra = 0;
 wcex.cbWndExtra = 0;
 wcex.hInstance = hInstance;
 wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
 wcex.lpszMenuName = NULL;
 wcex.lpszClassName = szWindowClass;
 wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
 if(!RegisterClassEx(&wcex))
 {
  MessageBox(NULL,	_T("Call to RegisterClassEx failed!"),	NULL,	NULL);
  return 1;
 }
 HWND hWnd = CreateWindow(szWindowClass,szTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,500, 100,NULL,NULL,hInstance,NULL);
 if(!hWnd)
 {
    MessageBox(NULL,_T("Call to CreateWindow failed!"),NULL,NULL);
    return 1;
 }
 ShowWindow(hWnd, nCmdShow);
 while(GetMessage(&messages,NULL,0,0))
 {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
 }

 return 0;
}


Maybe it was a series of programs that finally would build up to a working "Hello, World!" GUI program, but as you presented it in your 1st post, the Window Procedure was completely faulty in that there was no call to DefWindowProc(). That is what caused the CreateWindow() call to fail. Program also lacked a Message Pump, i.e., the while loop.
Last edited on
Topic archived. No new replies allowed.