views:

164

answers:

2

There are some new integer types in the Windows API to support Win64. They haven't always been suppoprted; e.g. they aren't present in MSVC6.

How can I write an #if condition to detect if these types are supported by <windows.h>?

(My code needs to compile under many different versions of Microsoft Visual C++, including MSVC6. So I need to provide my own definitions of these types, with an #if to disable them in newer compilers).

(For searchers, the full list of types is: DWORD_PTR, INT_PTR, LONG_PTR, UINT_PTR, ULONG_PTR)

+4  A: 

The macro MSC_VER is a value that is within the range [1200, 1300) for MSVC 6. So you can use #if MSC_VER>=1200 && MSC_VER<1300.

EDIT: As Anders said, this is not really that valid of a test beyond "is my compiler MSVC 6". However, you can also use:

#if defined(MAXULONG_PTR)

Since DWORD_PTR is a value type, it has a maximum value defined for it in basetsd.h.

MSN
Just because the compiler is VC6 does not mean that those types are not defined, usually you would use VC6 along with a somewhat up to date platform sdk
Anders
Anders' comment should be the accepted answer, update your platform sdk, don't try to code around outdated versions of it.
Blindy
"#ifdef MAXULONG_PTR" is perfect, thanks!
user9876
Blindy: We ship source code to customers. If we claim VC6 support, our code need to just work. Requiring a newer platform SDK would cause support calls from people who don't have it, or haven't configured it correctly. This annoys the customers, costs them time (== lost sales due to delayed products) and money, and costs us time and money to respond. We _could_ just say "VC2003 required", as that's a simple enough message for people to understand. And we will do that eventually... but not yet.
user9876
+3  A: 

Since those types are typedefs there's not a great, reliable way to determine whether or not they're defined at the pre-processor stage. MSN's suggestion of testing the compiler version is pretty good, but as Anders indicated in a comment, a more recent SDK might be in use (I think VC6 was supported up through the 2003 SDK - I'm not sure if those types are in that SDK or not).

You could check for something the SDK does define as a macro that uses these types, like GetWindowLongPtr:

#if !defined( GetWindowLongPtr)
typedef DWORD DWORD_PTR;
#endif

Kludgy, but I think you might be stuck with kludgy.

Michael Burr
I was going to suggest #ifdef checking for the UintToPtr macro, it seems to be defined in my 2003 platform sdk, but not the old vc6 headers, but GWLP is so much cooler. I'm not sure if GWLP is a macro on x64, so maybe a !_WIN64 check is needed, or you could check for the GWLP_* defines instead.
Anders
Or MAXULONG_PTR. Since you won't have DWORD_PTR without it.
MSN