Getting out of consoles

Pages: 123
I just got done with classes for the day, and the next week actually. (Spring break ftw). But this looks like solid code for Win32. I guess I was just always looking at the whole picture and getting lost, it does seem much simpler broken up into pieces like this. I'm gonna give your examples a try and do some tweaking/adding onto it to see what I get.

On the QT note, I have heard good things about. I had it downloaded awhile ago, but my C++ knowledge wasn't too far along so it was no use then. I may give it another shot now that I know a lot more about the language.

I will likely be posting here again soon with questions regarding some of these examples.

I appreciate you helping
Any time.

Breaking something up into categories is the whole philosophy behind OOP (Object-oriented-programming). While I am still new to it, I REALLY like being able to declare a button class only once. Then I define the sizes/positions for each button once, then I can use all of the functions for those buttons with only a single line of code each. There is almost no extra work if I want to add more buttons.
Last edited on
I've ran into an error with my createWindow() function. Not sure what is going on

1
2
3
4
5
6
7
8
9
10
HWND hwnd = CreateWindow(
		szWindowClass,
		szTitle,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT,
		500, 100,
		NULL,
		NULL,
		hInstance,
		NULL);


All I know is that it failed :(
If you've got an error then tell us what the error is.

_ If it does not compile, paste the compiler messages
_ If it returns NULL, then check out GetLastError() and FormatMessage()
_ If it crashes, provide a minimal example
1
2
3
4
5
6
7
8
9
10
if(!hwnd)
	{
		MessageBox(
			NULL,
			_T("CreateWindow function failed"),
			_T("Error"),
			NULL);

		return 1;
	}


This is ran, and my main window doesn't show up. I'm brand new to win32, but I have looked at GetLastError() and FormatMessage(), though I'm not sure how to implement it
Retrieving Last Error Code
http://msdn.microsoft.com/en-us/library/windows/desktop/ms680582(v=vs.85).aspx

¡¿Why do they use void main() in their examples?!
Yea that code is gibberish to me right now. I literally just started Win32 today. I don't know how to make that tell me the error returned from CreateWindow. I've tried just outputting GetLastError() and that doesnt work, ive tried casting it to various types and that doesn't work.

I'm at a loss
I would try pasting the entire function into your code, and call it where you display your error message box
1
2
3
4
if(!hwnd
{
    ErrorExit(TEXT("CreateWindow"));
}
Last edited on
Ok that worked perfectly actually. Thank you!

It says "Cannot find window class"

Window class is WNDCLASSEX I assume? I have it created, and it's getting used a bit
I think the issue is dealing with
wcex.lpfnWndProc = WndProc;

According to the example on MSDN, this is correct. But I get errors when building.


Error	1	error LNK2019: unresolved external symbol "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) referenced in function _WinMain@16


I tried just setting lpfnWndProc, but then I get the missing window class error. I have no idea if they're related but it seems like it
It says that you need to register it.
http://msdn.microsoft.com/en-us/library/ms633575%28v=VS.85%29.aspx


wcex.lpfnWndProc = WndProc; You are setting a callback there. ¿Do you have that function?
It may be a little easier if you show your code.
Last edited on
I do have the function, but the tutorial hasn't had as us do anything with it, and I dont know what it does. Here comes the whole 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <strsafe.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

void ErrorExit(LPTSTR lpszFunction) 
{ 
    // Retrieve the system error message for the last-error code

    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    // Display the error message and exit the process

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
        (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); 
    StringCchPrintf((LPTSTR)lpDisplayBuf, 
        LocalSize(lpDisplayBuf) / sizeof(TCHAR),
        TEXT("%s failed with error %d: %s"), 
        lpszFunction, dw, lpMsgBuf); 
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    ExitProcess(dw); 
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wcex;
	

	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style  = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;		//The fuck?
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = (LPCWSTR)"Class\0";	//szWindowClass?
	wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));


	if(!RegisterClassEx(&wcex))
	{
		MessageBox(NULL,
			_T("Call to RegisterClassEx failed"),
			_T("Hey there window"),
			NULL);

		return 1;
	}

	static TCHAR szWindowClass[] = _T("Class\0");
	static TCHAR szTitle[] = _T("This is a f*****' window");

	HWND hwnd = CreateWindow(
		szWindowClass,
		szTitle,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT,
		500, 100,
		NULL,
		NULL,
		hInstance,
		NULL);

	if(!hwnd)
	{
		ErrorExit(TEXT("CreateWindow"));


		return 1;
	}

	ShowWindow(hwnd,
    nCmdShow);
	UpdateWindow(hwnd);

	return 0;
}
Looks like you declare WndProc but dont define it.
I don't know how it is supposed to be defined. :(
If you want to use a function you need to define it.
'unresolved external symbol' tells that the linker did not found the definition.
That applies to any library that you could use (also none)

So your question should have been:
_¿where is WndProc() defined? (against what do you link)
_¿what could be an appropriate body for the function?

¿Can you tell me when this callback will get called?
Last edited on
For testing purposes LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM){return 0;} should be fine.
I believe the function is to handle messages.
Last edited on
I added the return 0, still no luck. Getting the same message about the missing window class.

@ne555,

I don't know where WndProc is called. I have just started Win32 and not sure what it is used for.
Try renaming the class, I dont believe the explicit null termination is necessary.
I have reworked the class name. Moved the t_char array as a global, and used that for both places. Solved that issue, but I have another.

CreateWindow failed with code 0. Operation completed successfully.


Which makes no sense, but means it's running

1
2
3
4
5
6
7
if(!hwnd)
	{
		ErrorExit(TEXT("CreateWindow"));


		return 1;
	}


still. Which I'm not sure why if it says it's not running into problems
This works.
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <strsafe.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
	return 0;
}

void ErrorExit(LPTSTR lpszFunction) 
{ 
    // Retrieve the system error message for the last-error code

    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    // Display the error message and exit the process

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
        (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); 
    StringCchPrintf((LPTSTR)lpDisplayBuf, 
        LocalSize(lpDisplayBuf) / sizeof(TCHAR),
        TEXT("%s failed with error %d: %s"), 
        lpszFunction, dw, lpMsgBuf); 
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    ExitProcess(dw); 
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASS wcex;
	MSG Msg;

	//wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style  = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;		//The fuck?
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = _T("ClassA");	//szWindowClass?
	wcex.hIcon = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	

	if(!RegisterClass(&wcex))
	{
		/*MessageBox(NULL,
			_T("Call to RegisterClassEx failed"),
			_T("Hey there window"),
			NULL);*/
		ErrorExit(TEXT("RegisterClass"));
		return 1;
	}

	static TCHAR szWindowClass[] = _T("ClassA");
	static TCHAR szTitle[] = _T("This is a f*****' window");

	HWND hwnd = CreateWindow(
		szWindowClass,
		szTitle,
		WS_OVERLAPPEDWINDOW | WS_VISIBLE,
		CW_USEDEFAULT, CW_USEDEFAULT,
		500, 100,
		NULL,
		NULL,
		hInstance,
		NULL);

	if(!hwnd)
	{
		ErrorExit(TEXT("CreateWindow"));
		return 1;
	}

	ShowWindow(hwnd,
    nCmdShow);
	UpdateWindow(hwnd);
	while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
	//return 0;
}
Last edited on
Pages: 123