Why are the standard datatypes not used in Win32 API?

c, c++, types, winapi

Solution

Yes, you should use the correct data-type for the arguments for functions, or you are likely to find yourself with trouble.

And the reason that these types are defined the way they are, rather than using `int`, `char` and so on is that it removes the "whatever the compiler thinks an `int` should be sized as" from the interface of the OS. Which is a very good thing, because if you use compiler A, or compiler B, or compiler C, they will all use the same types - only the library interface header file needs to do the right thing defining the types.

By defining types that are not standard types, it's easy to change `int` from 16 to 32 bit, for example. The first C/C++ compilers for Windows were using 16-bit integers. It was only in the mid to late 1990's that Windows got a 32-bit API, and up until that point, you were using `int` that was 16-bit. Imagine that you have a well-working program that uses several hundred `int` variables, and all of a sudden, you have to change ALL of those variables to something else... Wouldn't be very nice, right - especially as SOME of those variables DON'T need changing, because moving to a 32-bit int for some of your code won't make any difference, so no point in changing those bits.

It should be noted that `WCHAR` is NOT the same as `const char` - `WCHAR` is a "wide char" so `wchar_t` is the comparable type.

So, basically, the "define our own type" is a way to guarantee that it's possible to change the underlying compiler architecture, without having to change (much of the) source code. All larger projects that do machine-dependant coding does this sort of thing.

Problem

I have been learning Visual C++ Win32 programming for some time now. Why are there the datatypes like `DWORD`, `WCHAR`, `UINT` etc. used instead of, say, `unsigned long`, `char`, `unsigned int` and so on? I have to remember when to use WCHAR instead of const char *, and it is really annoying me. Why aren't the standard datatypes used in the first place? Will it help if I memorize Win32 equivalents and use these for my own variables as well?

Original source

Related problems