Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preprocessor definitions for Universal Windows Platform?

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.

like image 786
jww Avatar asked Mar 17 '16 21:03

jww


People also ask

What are preprocessor definitions in Visual Studio?

The preprocessor performs preliminary operations on C and C++ files before they are passed to the compiler. You can use the preprocessor to conditionally compile code, insert files, specify compile-time error messages, and apply machine-specific rules to sections of code.

What is universal Windows platform used for?

Universal Windows Platform (UWP) is a computing platform created by Microsoft and first introduced in Windows 10. The purpose of this platform is to help develop universal apps that run on Windows 10, Windows 10 Mobile, Windows 11, Xbox One, Xbox Series X/S and HoloLens without the need to be rewritten for each.

What is universal Windows platform in Visual Studio?

Develop apps that target a wide range of devices including PC, mobile, Xbox, HoloLens, IoT, and Surface Hub. Community 2022.

Does C# have macros?

No, C# does not support preprocessor macros like C. Visual Studio on the other hand has snippets. Visual Studio's snippets are a feature of the IDE and are expanded in the editor rather than replaced in the code on compilation by a preprocessor.


1 Answers

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.

like image 192
Colin Robertson Avatar answered Nov 07 '22 19:11

Colin Robertson