none
Platform defined macros for windows store app

    Question

  • What are the platform defined macros for a windows store app provided by Visual Studio 2012. I am looking for scenarios like:

    1. Conditional compilation for Windows store app code vs legacy desktop code. Like _WIN32 is there something as _WINRT?

    2. Conditional compilation for intel and ARM targets in a windows store app.

    If there is a list of all such macros, that will be extremely useful.

    Thanks in advance.

    Monday, April 08, 2013 12:12 PM

Answers

  • (1) I cover this in  part 3 of my Dual-use Coding Techniques for Games blog series. In summary

    • Prefer use of the standard _WIN32_WINNT Windows Defines for selecting the correct Win32 API (i.e. many Win32 APIs required for use in Windows Store apps are the Vista (0x0600), Windows 7 (0x0601), or Windows 8 (0x0602) version.
    • You can make use of the winapifamily.h macros, but you should be careful with exactly how you use them. Adding your own preprocessor define is probably safer in the long run. The main reason to use winapifamily.h macros would be when writing your own shared APIs.
    • __cplusplus_winrt can be useful in some cases but remember that technically you can still be writing code for a Windows Store app without the /ZW switch.

    (2) I'd recommend sticking with the standard platform control defines here:

    • _M_ARM indicates an ARM target
    • _M_IX86 indicates a 32-bit X86 target
    • _M_X64 indicates a 64-bit native X64 target



    Monday, April 08, 2013 6:52 PM
  • I'd really recommend against the pattern mentioned above.

    You can usually do stuff like:

    #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
     // This code is for Win32 desktop apps
     #else
     // This code is for WinRT Windows Store apps
     #endif

    In the cases where you need to know if you are building for Windows phone 8, you can use something like:

    #if defined(WINAPI_FAMILY) && WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
     /// This code is for Windows phone 8
    #endif

    Of course, a better solution is usually to find a way to express it that doesn't require the winapifamily.h macros:

    #if (_WIN32_WINNT >= 0x0602)
     ScopedHandle hFile( safe_handle(
     CreateFile2( szFile, GENERIC_READ, FILE_SHARE_READ,
     OPEN_EXISTING, nullptr ) ) );
    #else
     ScopedHandle hFile( safe_handle(
     CreateFile( szFile, GENERIC_READ, FILE_SHARE_READ, nullptr,
     OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, nullptr ) ) );
    #endif
     if ( !hFile )
     {
        return HRESULT_FROM_WIN32( GetLastError() );
     }


    Monday, April 08, 2013 6:55 PM

All replies

  • Hi John Rick,

    In Windows 8 SDK which will be available after installing VS2012, MS has included a new file named winapifamily.h in windows.h header in order to partitioning API family. You can see this file for getting the scenario. Actually which code will be available to the program's target run-time platform is finally decided by the value defined in constant WINAPI_FAMILY.

    When ones open a Windows Store Apps by Visual Studio 2012 the WINAPI_FAMILY is defined by WINAPI_FAMILY_APP. Here is the summary.

    #define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP
           //define for typical Desktop Apps.
    #define WINAPI_FAMILY WINAPI_FAMILY_APP   
           //define for Windows Store Apps.  
    
            #if WINAPI_FAMILY_ONE_PARTITION( WINAPI_FAMILY, WINAPI_PARTITION_APP )
                    //Available only only to Metro programs.             
            #endif
    
            #if WINAPI_FAMILY_ONE_PARTITION( WINAPI_FAMILY, WINAPI_PARTITION_DESKTOP )
                    //Available only onlyto Desktop programs.
            #endif
    
            #if WINAPI_FAMILY_ONE_PARTITION( WINAPI_FAMILY_DESKTOP_APP, WINAPI_PARTITION_APP )
                    //Available to Metro, but not Desktop programs.
            #endif
    
            #if WINAPI_FAMILY_PARTITION( WINAPI_PARTITION_APP )
                    //Available to Metro and Desktop programs.
            #endif
    
            #if WINAPI_FAMILY_PARTITION( WINAPI_PARTITION_DESKTOP )
                    //Available to Desktop program.
            #endif



    Monday, April 08, 2013 3:09 PM
  • I would like to give a suggestion in this respect, you can define a new macro like WINRT in order to your conditional compilation by setting on the Preprocessor Definition of compiler options which may ease of your coding. By the way WIN32 is still found for both types of apps.
    Monday, April 08, 2013 3:39 PM
  • (1) I cover this in  part 3 of my Dual-use Coding Techniques for Games blog series. In summary

    • Prefer use of the standard _WIN32_WINNT Windows Defines for selecting the correct Win32 API (i.e. many Win32 APIs required for use in Windows Store apps are the Vista (0x0600), Windows 7 (0x0601), or Windows 8 (0x0602) version.
    • You can make use of the winapifamily.h macros, but you should be careful with exactly how you use them. Adding your own preprocessor define is probably safer in the long run. The main reason to use winapifamily.h macros would be when writing your own shared APIs.
    • __cplusplus_winrt can be useful in some cases but remember that technically you can still be writing code for a Windows Store app without the /ZW switch.

    (2) I'd recommend sticking with the standard platform control defines here:

    • _M_ARM indicates an ARM target
    • _M_IX86 indicates a 32-bit X86 target
    • _M_X64 indicates a 64-bit native X64 target



    Monday, April 08, 2013 6:52 PM
  • I'd really recommend against the pattern mentioned above.

    You can usually do stuff like:

    #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
     // This code is for Win32 desktop apps
     #else
     // This code is for WinRT Windows Store apps
     #endif

    In the cases where you need to know if you are building for Windows phone 8, you can use something like:

    #if defined(WINAPI_FAMILY) && WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
     /// This code is for Windows phone 8
    #endif

    Of course, a better solution is usually to find a way to express it that doesn't require the winapifamily.h macros:

    #if (_WIN32_WINNT >= 0x0602)
     ScopedHandle hFile( safe_handle(
     CreateFile2( szFile, GENERIC_READ, FILE_SHARE_READ,
     OPEN_EXISTING, nullptr ) ) );
    #else
     ScopedHandle hFile( safe_handle(
     CreateFile( szFile, GENERIC_READ, FILE_SHARE_READ, nullptr,
     OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, nullptr ) ) );
    #endif
     if ( !hFile )
     {
        return HRESULT_FROM_WIN32( GetLastError() );
     }


    Monday, April 08, 2013 6:55 PM
  • I'd really recommend against the pattern mentioned above.

    You can usually do stuff like:

    #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
     // This code is for Win32 desktop apps
     #else
     // This code is for WinRT Windows Store apps
     #endif

    In the cases where you need to know if you are building for Windows phone 8, you can use something like:

    #if defined(WINAPI_FAMILY) && WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
     /// This code is for Windows phone 8
    #endif
    ...

    When building from the command line using nmake, what should we be defining? Without defining something, Windows Store Apps fail to compile from the command line.

    Open question at Command Line: Which API Family defines for Windows Phone 8 and Windows RT?.

    Sunday, August 11, 2013 5:30 PM