Cory Gross
Cory Gross

Reputation: 37206

Why use Windows defined data types?

I have been doing some Winsock programming lately. I do not do much stuff within the Windows SDK. I haven't built an actual Win32 application since I was young and I did not think about it then, but now I look at some of the types that have been defined that simply wrap around other types that are platform agnostic and I wonder why anyone would use them when they didn't have to. I do not see any advantages over using PSTR or DWORD over there native counter-parts. In the Winsock Reference there is a page that lists all the structs and typedefs that are defined. They typedef all the structures to capital letters and I do not understand the overall strategy that was trying to be accomplished when the decision in designing the Windows SDK was made to typedef all your types to capital letters like this.

I do not understand why anyone, even when writing code in Windows would ever use the capitalized versions if they didn't have too, simply because it seems it would make the code harder to port if ever desired in the future. I always use the native types and original Winsock structures when I can. I tried to do some searching for an answer to this question, but perhaps I cannot think how to phrase a query properly. Is there some advantage to this that I am missing? Can anyone shed some light on this for me?

Upvotes: 1

Views: 283

Answers (1)

0xC0000022L
0xC0000022L

Reputation: 21369

The advantage is that the typedefs from the Windows SDK will adjust as needed. The probably best example in recent history would be DWORD_PTR which behaves correctly out of the box in x64 builds when used properly, while you would have to go through hoops in order to make this work with what you call "native types" (i.e. those from the language standard).

Also keep in mind that some of the Winsock functions are not exact counterparts of the original BSD socket functions. So it makes sense to keep things artificially separate, be it for legacy reasons or brevity.

The same can be said for certain Win32 types that are not defined in the language standard because they adjust depending on your build, such as LPTSTR.

Last but not least if you ever engaged in cross-platform programming and can't rely on (all or some) types from stdint.h for various reasons, you'll appreciate types such as DWORD, because they imply a certain width, which is relevant whenever talking to some interface (API).

Upvotes: 2

Related Questions