A simple Win32 question?

Started by
3 comments, last by Anon Mike 14 years, 2 months ago
The WINCLASSEX struct has a member called cbSize which is the size of the structure. Why do we have to tell the structure its size?
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
Advertisement
WNDCLASSEX WndClassEx;WndClassEx.cbSize = sizeof(WNDCLASSEX);


[edit] - Sorry I read too fast :)

A lot of the Microsoft stuctures do this. I think this is kind of a weird form of versioning in their code behind the scenes. I think it also is because most structures are passed by pointer, so it is hard to tell what was actually passed and how big it really is.

[Edited by - Rattrap on February 12, 2010 8:36:05 AM]

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Thanks :)
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
Quote:Original post by Rattrap
I think this is kind of a weird form of versioning in their code behind the scenes.


Yes this is exactly it. The functions uses it to know which version of the Windows SDK the program was compiled with, so that it knows what behavior to use. This way when Microsoft adds new functionality to a function they can make the struct bigger, and older programs using smaller struct size will get the behavior they expect (which keeps them from crashing).

There's nothing particularly wierd about it. API's evolve over time. Microsoft wants to be able to do that without having to create a new version of the API every single time. Introducing new API's also makes it harder for developers to adopt them because their programs won't work at all on older systems without special care. Having a size field is in some ways simpler than a version field and it's easy/effiecient to get the size so why not.

WNDCLASSEX is actually an excellent example. If the original structure (WNDCLASS) had had the size field they wouldn't have had to create the RegisterClassEx API. Programs would just set the size to whatever they knew about and call the single API.
-Mike

This topic is closed to new replies.

Advertisement