问题
We have a C++ library, and we received a few requests to support UWP. I'm investigating the port now. I'm looking through Microsoft's C/C++ Preprocessor Reference | Predefined Macros for Visual Studio 2015, but I don't see anything related to UWP.
I found How to: Use Existing C++ Code in a Universal Windows Platform App, but they look like the old defines for those Metro UI apps:
WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PC_APP)
WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP)
WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
I'd expect to see something specific to the latest iteration of the Windows Runtime. I thought we might be able to detect Windows 10 and UWP via _WIN32_WINNT_WIN10
, but there does not appears such a macro if I am parsing Using the Windows Headers from MSDN correctly.
Also some APIs are only available for Windows 10 and UWP Windows Store apps, so we need to detect when some APIs are missing (i.e., Windows Phone 8, Windows Store 8, Windows 10, and Windows Store 10).
What are the preprocessor macros used to detect UWP?
A related question may be Detect Windows Kit 8.0 and Windows Kit 8.1 SDKs, but I'm not sure at the moment.
回答1:
The macros you mention may date back to Windows 8, but with a couple of additions for server apps and drivers, they're still the ones used for Universal Windows apps.
Note that there are no predefined macros built into the Visual C++ compiler specific to UWP. All the macros for UWP flavors are defined in a winapifamily.h
header that comes with the Windows 10 SDK. You can find it in the shared include file directory installed as part of the SDK, for example, C:\Program Files (x86)\Windows Kits\10\Include\10.0.10586.0\shared\winapifamily.h
, modulo your installation drive and SDK version.
The comments in winapifamily.h
are extensive, and do a good job of describing every use case you'll need for these macros. You can use them for conditional compilation, or set up deprecation warnings for UWP-incompatible code by using _WINAPI_DEPRECATED_DECLARATION
on function declarations.
Edit to add an example:
In code that comes after you've included winapifamily.h, you can switch on a macro that's defined in a particular version of the Windows SDK to do something specific to that version, like so:
#include <winapifamily.h>
// ...
#if defined (WINAPI_FAMILY_SYSTEM)
// WINAPI_FAMILY_SYSTEM is new to Windows 10, so do Windows 10-specific
// calls here
#elif (WINAPI_PARTITION_APP == 1)
// This value is forced to 1 in Windows 8.1, but it's different in
// Windows 8.0 so make Windows 8.1-specific calls here
#else
// Make Windows 8.0-specific calls here
#endif
This is just one (untested by me) way to make SDK version-specific code based on reading what's in the headers. This is independent of the compiler version (defined in _MSC_VER
or _MSC_VER_FULL
) or the platform NTDDI_*
values.
来源:https://stackoverflow.com/questions/36072350/preprocessor-definitions-for-universal-windows-platform