How to detect if the Windows DWORD_PTR type is supported, using an ifdef?

可紊 提交于 2020-01-11 09:52:08

问题


There are some new integer types in the Windows API to support Win64. They haven't always been supported; 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)


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2723284/how-to-detect-if-the-windows-dword-ptr-type-is-supported-using-an-ifdef

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!