Page 1 of 1

Unusual behavior...for me.

Posted: July 24th, 2019, 1:51 am
by albinopapa
I decided to take a break from game-centric programming and try my hand at some Win32 stuffs. I've messed around before with some success and have never run into an issue quite like what I'm currently experiencing.

In WinMain I'm trying to create a window. I have my WNDCLASSEX structure filled out and registered with RegisterClassEx and I get the ATOM that the function returns just fine. I go to call CreateWindowEx() using the same constexpr const char* string for lpszclassname that I used for the WNDCLASSEX structure that I registered and I get error number 1407 "Cannot find window class".

I do a search and this seems to happen if the string is a temporary, but I've never had issues with this before. I thought string literals were stored in the program's data section same as global static variables. After commenting some things out and thinking the issue was invalid arguments or an incorrect HINSTANCE, I tried just using the string literal directly and this worked. Not sure what the difference is. If the string is still around then what should it matter what the address is?

Ok, figured it out. The string you use in the WNDCLASSEX struct must have same address as the one you use for the CreateWindowEx() call. So, it's not the string that matters, but the same address. Dang, all these years and not running into that issue by luck. Damn that was frustrating.

Re: Unusual behavior...for me.

Posted: July 24th, 2019, 1:53 am
by albinopapa
The only reason I was doing this was because I want to have different Window classes, one for the main "parent" window and one for the child windows like buttons and text boxes. The parent window will use the WNDCLASSEX to get registered with the system, but child windows don't have to do that, so I thought I'd take the registration of a window outside the creation ( constructor or Initialize function ) of a Window class since not all Windows would need to register.

Re: Unusual behavior...for me.

Posted: July 24th, 2019, 1:13 pm
by chili
albinopapa wrote:
July 24th, 2019, 1:51 am
Ok, figured it out. The string you use in the WNDCLASSEX struct must have same address as the one you use for the CreateWindowEx() call. So, it's not the string that matters, but the same address. Dang, all these years and not running into that issue by luck. Damn that was frustrating.
This seems insane to me :lol:

Re: Unusual behavior...for me.

Posted: July 24th, 2019, 5:31 pm
by albinopapa
I agree, because the same string literal for both should work as well. Nevertheless, no matter what else I tried didn't work. The only thing that worked was using the same variable or string literal for both registering the WNDCLASSEX structure and the call to CreateWindowEx(). I'm guessing the compiler sees that the two string literals are the same and just uses the same static address for both.

Re: Unusual behavior...for me.

Posted: July 27th, 2019, 8:32 am
by chili
Well either way, good piece of trivia to know. Might come in handy someday, so thanks for sharing :)