Simple Win32 Hello World: RegisterClassEx

Hi folks,

I'm taking a look at a simple Win32 Hello World example in VS2015.

I have a question about RegisterClassEx:

After one has assigned values to your WNDCLASSEX structure, you need to register the window. The code given for doing this in the tutorial i'm following is:

1
2
3
4
5
6
7
8
9
if (!RegisterClassEx(&wcex))
	{
		MessageBox(NULL,
			_T("Call to RegisterClassEx failed!"),
			_T("Win32 Guided Tour"),
			NULL);

		return 1;
	}


Could someone please explain to me what's happening here? Is the function basically saying "If wcex doesn't exist, disply this error message"?

Or is RegisterClassEx also checking the parameters of wcex, to make sure it's legal and correct?
So RegisterClassEx returns a 1 if all is ok, and a 0 if there's a problem, is that it?

Thanks! ............D
Last edited on
RegisterClassEx returns a value. If the function fails, the value returned is 0.

!0 is true so the code governed by the if is executed if the return value from RegisterClassEx indicates failure.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms633587%28v=vs.85%29.aspx
Last edited on
Ah, thanks, 0! is true, i forgot that, now it makes more sense!

Sorry, i have a couple more (really nooby) questions:

In another tutorial, it says you should register your window with:
RegisterClassEx(&wcex);, which is different.

And i right in thinking that the version from my first post is just a more fancy version (with built-in error checking)?

Also, a question about syntax: in my first code example, i don't see any "else" statement to go with the if. So how is it the compiler jumps to the "return 1" part if the if statement evaluates to false?
Last edited on
In another tutorial, it says you should register your window with:
RegisterClassEx(&wcex);, which is different.

And i right in thinking that the version from my first post is just a more fancy version (with built-in error checking)?

The registration is the same. The only difference is the treatment of the return value. In the second example it is ignored/discarded. In the first, it is checked.


Also, a question about syntax: in my first code example, i don't see any "else" statement to go with the if. So how is it the compiler jumps to the "return 1" part if the if statement evaluates to false?

It doesn't.
The registration is the same. The only difference is the treatment of the return value. In the second example it is ignored/discarded. In the first, it is checked.


Ok, but if the return value is ignored/discarded, then the function does nothing?! What's the point?

It doesn't.

Care to expand on that?.........i don't understand the syntax of the function, could you explain?

Sorry if i'm asking dumb questions, i've only been learning C++ for a few months..........









Ok, but if the return value is ignored/discarded, then the function does nothing?! What's the point?

The function has side effects. Those side effects take place regardless of what is done with the return value, so I suppose the point is: the side effects.


are to expand on that?.........i don't understand the syntax of the function, could you explain?

I don't know what you mean by "syntax of the function." The only way any of the compound statement governed by an if statement is executed is if that if statement evaluates to true. If it evaluates to false, none of the compound statement is executed.

You have an incomplete code snippet here. Presumably there is more code following the closing brace of the compound statement.
I'm assuming whoever wrote the code you posted in your first post is calling RegisterClassEx() from WinMain().

Let's expand on that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <windows.h>

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) {

	//Do things...

	//If RegisterClassEx() fails
	if (!RegisterClassEx(&wcex)) {

		//Display error message

		//Return 1 to indicate error and terminate the program
	}

	//If the program is still executing, we can assume everything went fine.
	//We continue with the initialization...

}


In your example, return 1; simply indicates that there was an error and we're ending the program prematurely.

Also:

Ok, but if the return value is ignored/discarded, then the function does nothing?! What's the point?


Not true, as cire was saying. Functions can do all sorts of things without you taking advantage of the things they return.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int function() {
	std::cout << "Hello" << std::endl;
	return 123;
}



int main() {
	//Integer 'i' will be initialized with the value returned by function(), in this case 123.
	int i = function();

	//The function is invoked again, but we're not using the return value for anything. The return value is effectively discarded.
	function();

	return 0;
}


The program still prints "Hello" twice, even though we only used the function's return value once, and discarded it the second time.
Last edited on
Topic archived. No new replies allowed.