Reputation: 5580
Standard data types or Windows data types?
I would use Windows data types to make my code consistent with the Win32 API.
On the other hand, I would use standard data types to protect from coding errors more.
Using standard data types seems safer, but also feels like going to a 'wear-white' party in all black.
What should be decided here?
Upvotes: 2
Views: 1138
Reputation: 283891
Windows vs C++ types is a completely orthogonal issue to Unicode-safety.
If you have std::string s
, then you can do const_cast<LPSTR>(s.c_str())
just fine without sacrificing the compiler error when you compile as Unicode.
Upvotes: 1
Reputation: 10863
I agree with @fritzone as a practical solution. Mine is slightly different.
I have a strong preference for wrapping external APIs. I will write a layer of code inside which the WinApi is used, and always use Windows types within that layer. This will be the only part of the application that includes Windows.h. The API calls into those layer functions will use types that I choose and (as for @fritzone) I will explicitly cast values to WIndows types as needed. Every cast is a mistake waiting to happen.
The other advantage of layering is that it's a great place to put tracing code. The WinApi can be very chatty and hard to step through in a debugger, so I like to have tracing code for every WinApi function, that can be switched on if needed.
That's harder to do with MFC or ATL, which tend to become rather pervasive. It's been my preference across a wide variety of different external APIs.
Upvotes: 0
Reputation: 6925
hmm - i think the constness of the winapi is quite good. Did you have any examples?
but to your questions:
i would recommend to use the stdint types for your own stuff but when interfacing with winAPI use their types and convert to and from them to avoid difficulties about type widths and confusion what for example WORD means.
Upvotes: 1
Reputation: 35458
This is highly dependent on the project you are working on. Try to follow the already established coding style.
My favourite way of doing things:
void*
, char*
, etc...) and when I call them I also use this.LPVOID
, LPTSTR
, ...) or if I get the variable from one of my functions (which has "normal" data types), but needs to go to to a Win32 API call, I specifically typecast to the required type. Sometimes this also helps to see if I have some conflicting types.Upvotes: 2