What's Wrong With My Code?

I'm new to using the Windows API, and this is my first program that does not run in the CMD Console. It gives me a lot of errors when I try to compile it,
and I don't know what they mean. Here is my code:


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
#include <Windows.h>
#include <StdAfx.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR nCmdLine int nCmdShow)
{
	const wchar_t CLASS_NAME[] = L'WindowClass";
	WNDCLASS wc = { };
	wc.lpfnWndProc = WindowProc;
	wc.lpszClassName = CLASS_NAME;
	wc.hInstance = hInstance;
	RegisterClass(&wc);

	HWND hwnd = CreateWindowEx(
		0,
		CLASS_NAME,
		L"game",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
		NULL,NULL,hInstance,NULL);
	if(hwnd == 0)
		return 0;

	ShowWindow(hwnd,nCmdShow);
	nCmdShow = 1;

	MSG msg = { };
	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{

		case WM_CREATE:


		Break;

		case WM_COMMAND:


		Break;

		case WM_DESTROY:
			PostQuitMessage (0);
			return 0;

	}

	return DefWindowProc(hwnd, message, wParam, lParam);
} 


And here are the errors I get (A lot of them are just me forgetting to add a comma or semicolon):

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
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(1): warning C4627: '#include <Windows.h>': skipped when looking for precompiled header use
1>          Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(6): error C2144: syntax error : 'int' should be preceded by ','
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(8): error C2001: newline in constant
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(8): warning C4066: characters beyond first in wide-character constant ignored
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(9): error C2440: 'initializing' : cannot convert from 'wchar_t' to 'const wchar_t []'
1>          There are no conversions to array types, although there are conversions to references or pointers to arrays
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(9): error C2146: syntax error : missing ';' before identifier 'WNDCLASS'
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(9): error C2146: syntax error : missing ';' before identifier 'wc'
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(9): error C2275: 'WNDCLASS' : illegal use of this type as an expression
1>          c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winuser.h(1592) : see declaration of 'WNDCLASS'
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(9): error C2065: 'wc' : undeclared identifier
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(9): error C2059: syntax error : '{'
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(9): error C2143: syntax error : missing ';' before '{'
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(10): error C2065: 'wc' : undeclared identifier
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(10): error C2228: left of '.lpfnWndProc' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(11): error C2065: 'wc' : undeclared identifier
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(11): error C2228: left of '.lpszClassName' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(12): error C2065: 'wc' : undeclared identifier
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(12): error C2228: left of '.hInstance' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(13): error C2065: 'wc' : undeclared identifier
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(45): error C2065: 'Break' : undeclared identifier
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(50): error C2065: 'Break' : undeclared identifier
1>c:\users\max\documents\visual studio 2010\projects\game\game\game.cpp(58): error C2065: 'message' : undeclared identifier


Also, if you think there is something I could do to make my code better, please tell me.
Last edited on
The first thing you have to do is swap the order in which you include the headers

1
2
3
4
#include <StdAfx.h>
#include <Windows.h>

...


When you're using precompiled header, it must be the first one included.

If you'd rather not use a precompiled header, you can disable it in the project preferences. Then you can get rid of stdafx.h (after copying it's contents)

Then have another go fixing the error (starting off by checking the delimiters for your first string)

Andy
Line 8, you have a single quote and then end with a double quote. Single quotes are usually used as single character values rather than strings.

PS: A good hint for this was that the code after line 8 is all purple.
Last edited on
Should I replace this:

1
2
3
4
5
	WNDCLASS wc = { };
	wc.lpfnWndProc = WindowProc;
	wc.lpszClassName = CLASS_NAME;
	wc.hInstance = hInstance;
	RegisterClass(&wc);


With this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;     
    wincl.style = CS_DBLCLKS;                 
    wincl.cbSize = sizeof (WNDCLASSEX);

    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                
    wincl.cbClsExtra = 0;                    
    wincl.cbWndExtra = 0;                      
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    if (!RegisterClassEx (&wincl))
        return 0;
?

The second one is the default windows app code from Dev C++ (I use Visual C++ 2010, I just found the Dev C++ code on the internet.)
Last edited on
It would be better to use the longer version while you are learning (though you could use the short variable name should you feel like it).

Note that there are a few changes that should be made

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    WNDCLASSEX wincl;

    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName; // fix! CLASS_NAME / szClassName
    wincl.lpfnWndProc = WindowProcedure;     
    wincl.style = CS_HREDRAW | CS_VREDRAW; // add CS_DBLCLKS if you need double-clicks
    wincl.cbSize = sizeof (WNDCLASSEX);
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); // might need MAKEINTRESOURCE()
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); // might need MAKEINTRESOURCE()
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL; // TODO - add menu here if needed       
    wincl.cbClsExtra = 0;                    
    wincl.cbWndExtra = 0;                      
    wincl.hbrBackground = (HBRUSH) (COLOR_BACKGROUND + 1); // must + 1
    // see MSDN entry for WNDCLASSEX

    if (!RegisterClassEx (&wincl))
        return 0;
Last edited on
Topic archived. No new replies allowed.