4 ms·
This is what a Win32 app's source code looks like: https://www.paulgriffiths.net/program/c/srcs/winhellosrc.html https://www.paulgriffiths.net/program/c/srcs/w
by bmh 7y ago
This is what a Win32 app's source code looks like:
https://www.paulgriffiths.net/program/c/srcs/winhellosrc.html https://www.paulgriffiths.net/program/c/srcs/winhellosrc.htm...
- Ididntdothis 7y agoIt's not much effort to wrap Win32 into a pretty nice C++ library. I guess in a sense this would be MFC but with modern C++ you can do much better.
- AnIdiotOnTheNet 7y agoIt's not much effort to wrap Win32 in any language, which is more than you can say for something like QT.
- leetcrew 7y agomaybe I'm just ignorant, but I always have a chuckle at stuff like this: wndclass.cbSize = sizeof(wndclass); why does a struct need to be told its own size?
- Ididntdothis 7y agoBecause different versions of the struct have different sizes. That’s a C artifact.
- leetcrew 7y agoI figured it's one of those structs where the macro can resolve to a few different types depending on what's #define'd, but the implementation should still be able to use sizeof wherever it's needed. I do believe there's a good reason; I just can't figure out what it is. edit for any curious readers: I found an answer here https://stackoverflow.com/questions/30416191/why-do-some-winapi-functions-need-sizes-of-structs-passed-as-their-parameters/30416321 https://stackoverflow.com/questions/30416191/why-do-some-win...
- Ididntdothis 7y agoYou can and should use sizeof in your code. The Windows code needs to know the size so it knows what version of the header the code was compiled against. There are probably other ways to do this but sizeof is not bad. That’s how they can get backwards compatibility over decades.
- deleted 7y ago[deleted]
- asveikau 7y agoMany Microsoft APIs do that so that they can add members at the end of the struct in future versions. Inside the implementation of the API they might say: if (wndClass->cbSize == SIZE_IN_PREVIOUS_SDK_VERSION) { // Code to handle legacy structure goes here } If they add to the struct and don't make accesses to the new members conditional on cbSize's value, then it's a bad pointer dereference. The caller allocated it with the "old" size, likely on their stack, and some unrelated value might be located where the new members would be, or it could be a bad pointer.