Understanding the win32 data types

This tip submitted by Muhammad Shahid on 2011-11-04 04:44:57. It has been viewed 11841 times.
Rating of 5.3 with 38 votes



I think that Win32 API is not very hard to use as we think about it. The basic problem are the data types that the Win32 API does defined, like WCHAR, BOOL, CHAR, LPSTR etc. When we learn C++, we do not learn about these data types, and when we return to Win32 API, the problems begin to exist. As you know that C/C++ is very dynamic language. We can create our own Data Types as they were defined in the pure language specifications. Win32 API uses the same dynamic features of C and defines the custom Data Types. So Win32 API is not very hard to use yet most powerful.

So if the LPSTR is defined in the Win32 API as follows...
//Pointer to the long string, note "long" is useless in Win32 API.
typedef char* LPSTR;
//Same
typedef char* PSTR;
//In the C++, this is the same as previous.
char* lpstr;
//These all represent only a single data type that is a pointer to string.

//another example
//in Win32 API
typedef const wchar_t* LPWCSTR;
//in C++ language
const wchar_t* lpwcstr;

//yet another
//in Win32 API
typedef int UInt;
//in C++
int i;
//

I think that Win32 API is not very hard to use as we think about it. The basic problem are the data types that the Win32 API does defined, like WCHAR, BOOL, CHAR, LPSTR etc. When we learn C++, we do not learn about these data types, and when we return to Win32 API, the problems begin to exist. As you know that C/C++ is very dynamic language. We can create our own Data Types as they were defined in the pure language specifications. Win32 API uses the same dynamic features of C and defines the custom Data Types. So Win32 API is not very hard to use yet most powerful.

So if the LPSTR is defined in the Win32 API as follows...
//Pointer to the long string, note \"long\" is useless in Win32 API.
typedef char* LPSTR;
//Same
typedef char* PSTR;
//In the C++, this is the same as previous.
char* lpstr;
//These all represent only a single data type that is a pointer to string.

//another example
//in Win32 API
typedef const wchar_t* LPWCSTR;
//in C++ language
const wchar_t* lpwcstr;

//yet another
//in Win32 API
typedef int UInt;
//in C++
int i;
//I hope you did find the bug, yes, UInt is not defined in Win32 API but in my own C++ source file. So this is all the power of C/C++, every one can create custom data types easily!


It is up to you that you use the Win32 API replacable data types or pure C++ data types. The result would be the same. I hope when you will next see the strange Win32 API data types, you will not let them being strange. Do not forget that Win32 API itself is implemented in C that is very very close to C++!

It is up to you that you use the Win32 API replacable data types or pure C++ data types. The result would be the same. I hope when you will next see the strange Win32 API data types, you will not let them being strange. Do not forget that Win32 API is itself is implemented in C that is very very close to C++!



More tips

Help your fellow programmers! Add a tip!