Search code examples
visual-studiowinapiwinrt-component

Equivalent of the GetTempPath on Windows Phone


I am compiling a third party library libkml for Windows Universal App. And I notice that the following Win32 API is not available on anything but WINAPI_PARTITION_DESKTOP.

The following is from fileapi.h:

#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)

WINBASEAPI
DWORD
WINAPI
GetTempPathW(
    _In_ DWORD nBufferLength,
    _Out_writes_to_opt_(nBufferLength, return + 1) LPWSTR lpBuffer
    );
... 
#endif

Does anyone know the equivalent function for this GetTempPath for Windows Store App and Windows Phone App?


Solution

  • Here is an example GetTemporaryDirectory() wrapper function taken from the following MSDN blog article about "Writing shared code for Windows Store and Win32 desktop apps":

    Dual-use Coding Techniques for Games, part 3.

    void GetTemporaryDirectory( wchar_t* dir, size_t maxsize )
    {
        if ( !maxsize ) return;
        *dir = 0;
        #if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
        DWORD nChars = GetTempPath( maxsize, dir );
        if ( nChars > 0 )
            dir[nChars-1] = '\0'; // Trim trialing '\'
        else
            *dir = 0;
        #else // Windows Store WinRT app
        auto folder = Windows::Storage::ApplicationData::Current->TemporaryFolder;
        wcscpy_s( dir, maxsize, folder->Path->Data() );
        #endif // WINAPI_FAMILY_PARTITION
    }