I’ve noticed that in c/c++ a lot of Win32 API structs need to be told how big they are.
i.e someStruct.pbFormat = sizeof(SomeStruct)
Why is this the case? Is it just for legacy reasons? Also any idea what “pb” stands for too?
EDIT: oops, yeah I meant “cbFormat”
This is for backward compatibility when Windows API is extended.
Imagine the following declarations
which you are calling like this:
A future OS version may extend this to
However, if you have compiled against the “older” SDK,
GetWinDatahas no chance to figure out you don’t know aboutextraData. If it would fill it in regardless, it would overwrite data on the stack. BOOOM!That’s why, the “size known to the caller” is added to the structure, and new members are appended at the end. The
GetWinDataimplementation can inspect the size and decide “this poor guy doesn’t know about all the new features yet”.