6

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.

Community
  • 1
  • 1
jww
  • 97,681
  • 90
  • 411
  • 885

1 Answers1

5

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.

Colin Robertson
  • 519
  • 1
  • 4
  • 8
  • OK, the edits are good (unfortunately, I cannot upvote it again). The lack of Bcrypt/CNG on Windows Phone 8 and Windows Store 8 made this a problem that needed solid solution, and I think your observations will work nicely. As with the other question, I need some time to test it. I'm working on a SSE2->NEON port, so I need to stay with it rather break for this testing. Give me a week or two to test it. – jww May 07 '16 at 06:37
  • 1
    Make sure your finished product passes the WACK as well -- create a dummy Store project, include your library, then choose to create Store packages and run the App Compat tests. You don't actually have to submit anything to the Store to verify this. – Peter Torr - MSFT May 08 '16 at 21:48
  • This may be related: [What is WINAPI_FAMILY_ONECORE_APP?](http://stackoverflow.com/q/38288622) – jww Jul 10 '16 at 04:50