Why can't use part of the win32 api in windows metro style program ? RRS feed

  • Question

  • Because windows metro style program the default preprocessor definitions is (WINAPI_FAMILY=2), so win32 library part code disable.

    Is windows metro style program can't well support win32 api function?

    such as, win32 library(<stdlib.h>) as part of the code:


    _Check_return_ _CRTIMP _CRT_INSECURE_DEPRECATE(_dupenv_s) char * __cdecl getenv(_In_z_ const char * _VarName);
    _Check_return_opt_ _CRTIMP errno_t __cdecl getenv_s(_Out_ size_t * _ReturnSize, _Out_writes_opt_z_(_DstSize) char * _DstBuf, _In_ rsize_t _DstSize, _In_z_ const char * _VarName);
    __DEFINE_CPP_OVERLOAD_SECURE_FUNC_1_1(errno_t, getenv_s, _Out_ size_t *, _ReturnSize, char, _Dest, _In_z_ const char *, _VarName)
    #if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
    #pragma push_macro("_dupenv_s")
    #undef _dupenv_s

    _Check_return_opt_ _CRTIMP errno_t __cdecl _dupenv_s(_Outptr_result_buffer_maybenull_(*_PBufferSizeInBytes) _Outptr_result_z_ char **_PBuffer, _Out_opt_ size_t * _PBufferSizeInBytes, _In_z_ const char * _VarName);

    #if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
    #pragma pop_macro("_dupenv_s")
    #endif /* _CRT_USE_TAILORED_APP */


    When WINAPI_FAMILY=2, the above code is not available.

    In the windows metro style program how use of it?

    Friday, January 6, 2012 8:23 AM


  • Hi Yangdebin,

    Metro style apps run in a sandbox and cannot access all Win32 API. In general there are restrictions on interacting with and altering system behavior, and in your particular case here the environment functions are unavailable. Since the underlying Win32 API needed to access environment variables cannot be called, the CRT doesn't allow the app to try to do so and fail.

    You can find the list of Win32 and COM API which can be called from Metro style apps are listed in the documentation at Win32 and COM for Metro style apps. That link also explains how you can look in the SDK header files to see if specific API are in the Desktop or Application families (Desktop family API are not available to Metro style apps). The Windows App Cert Kit will give you a definitive answer as to whether a given function will successfully validate.

    What are you trying to get from the Environment here? Perhaps there is another way to achieve the same goal that is compatible with Metro style apps.


    Friday, January 6, 2012 7:47 PM