1

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?

Yuchen
  • 30,852
  • 26
  • 164
  • 234

2 Answers2

3

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
} 
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Hello Remy, thanks for the example! I hope that I can get an equivalent function in C++ rather than CX/C++. So that I can simply patch the third-party library which was originally written for old Win32 API; and compile the library to a Windows Phone or Store App DLL and then it would be used in a Windows Runtime Component eventually. But it seems that this is not an option as for now. I will give it a couple more hours see if other have better solutions. If not, this would be the best answer. – Yuchen Aug 19 '15 at 18:33
  • WinRT APIs are exposed to C++ as COM objects. Maybe you can use the [Windows Runtime C++ Template Library (WRL)](https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh438466.aspx) to interact with them. – Remy Lebeau Aug 19 '15 at 19:15
  • The preprocessor directive should probably be `#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)` instead. @Yuchen: C++/CX is C++, with language projections to ease access of the Windows Runtime. Although it borrows syntax from C++/CLI, it compiles to native code, and the non platform-specific syntax is compatible with C++11, so you can compile regular C++ code as C++/CX. – IInspectable Aug 20 '15 at 11:06
  • 1
    @Yuchen: You may want to check out [C++/CX Part 0 of \[n\]: An Introduction](http://blogs.msdn.com/b/vcblog/archive/2012/08/29/cxxcxpart00anintroduction.aspx): *"When compiling C++/CX code, the Visual C++ compiler transforms most C++/CX constructs into equivalent C++ code. [...] There is a top-secret compiler option, **/d1ZWtokens**, which causes the compiler to print the generated C++ code that it generated from your C++/CX source."* – IInspectable Aug 20 '15 at 11:09
  • @IInspectable: if you read the blog article I linked to,it says: "*The system headers make extensive use of the WINAPI_FAMILY_PARTITION macro available in , however, as the exact make-up of partitions is subject to change with the introduction of new families over time, the recommendation is to only take dependencies on the FAMILY macros.*" – Remy Lebeau Aug 20 '15 at 11:14
  • @RemyLebeau: The recommendation comes without (convincing) rationale. Surely, the partitions are combined to encompass compatible families, and this won't break with future additions. Taking a dependency on a family rather than a partition means, that you cannot compile code for future families without changing your code (even if you have code for all other families of that new family's partition). In fact, the `#else` branch causes the very breakage, that the recommendation meant to prevent. – IInspectable Aug 20 '15 at 11:33
  • @IInspectable, thanks for the tips!! But I don't quite agree using `#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)` is a good idea though, because it is not compatible with other platforms. I made a patch to a 3rd party library a while ago and uses this `#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)` extensively! I ended up having to go back and revise all my changes to something dummy like `#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)` >_ – Yuchen Aug 20 '15 at 19:55
  • @IInspectable top-secret compiler option **/d1ZWtokens** is actually very interesting! I will give it a try ... – Yuchen Aug 20 '15 at 19:58
1

Use ApplicationData.TemporaryFolder.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Do you know if there is an equivalent function in C++ rather than CX/C++? – Yuchen Aug 19 '15 at 15:41
  • 1
    Er, I don't know that. The doc I linked to mentions this though: *You can access files in the temporary app data store using the "ms-appdata:///temp/" protocol.* – David Heffernan Aug 19 '15 at 15:55