0

In the past I wrote a game using the C programming language. Recently I was exploring the idea of porting it to the Universal Windows Platform, and potentially to the Windows Store.

After reading the docs, and spending some time on it, I managed to properly package it, and successfully install it locally as a UWP app.

I realized however, that up until now, the game was distributed as one folder, that when launched will look for the game's files locally. This means that if the game is run from a different folder (like when using a shortcut) it won't be able to find the local files to read. This is also a problem when it becomes a UWP app, since it is installed in the system, and can be launched from anywhere.

I've read in the docs this page: File access permissions, which describes how to get the install directory and read files from it. However the examples given are in C#.

How can I implement a similar functionality in C? The ideal solution would let me read files that are part of the package when it's installed.

8
  • @pqans My only concern is, would this be true all the time? I think I saw somewhere that it's possible to install apps on a different drive (other than C:). Although it does seem promising, at least for a short-term solution. Commented May 11, 2021 at 14:39
  • FWIW: all those APIs (from the linked documentation) are also available in C++ (you can switch the language in the API documentation in the top-right) Commented Aug 11, 2021 at 15:16
  • @UnholySheep Thanks for pointing out, I can't believe I missed that. This could potentially solve the problem, assuming there is no C solution to this. Commented Aug 11, 2021 at 15:19
  • AFAIK there is no C API available for this (though I might be wrong, my experience with UWP is limited to C++ projects), but creating a custom abstraction with an extern "C" header is the approach I've seen in a handful of open-source projects Commented Aug 11, 2021 at 15:26
  • @UnholySheep That sounds like a good idea. Do you happen to have any links of a project like that? I'd love to take a look at someone else's implementation. Also this information helps a lot, feel free to post it as an answer. I'll give it some time before I accept a solution, but unless someone knows a way to do this in C, this solution seems quite good :) Commented Aug 11, 2021 at 15:30

1 Answer 1

1
+100

As discussed in the comments, the UWP API is also available in C++ (either C++/CX or C++/WinRT). As far as I'm aware there is no pure C API available, but an option is to create a custom abstraction using an extern "C" header.

An example of this could look something like this (untested, and heavily leaning on the file reading code found in the open-source project Kinc: https://github.com/Kode/Kinc/blob/master/Sources/kinc/io/filereader.winrt.cpp#L159)

Header

#ifdef _cplusplus
extern "C"
#endif
const char* getAppLocation(void);

Implementation (C++/CX)

#include <Windows.h>

extern "C" const char* getAppLocation(void) {
    static char filepath[1001];
    Platform::String ^ locationString = Windows::ApplicationModel::Package::Current->InstalledLocation->Path;
    WideCharToMultiByte(CP_UTF8, 0, locationString->Begin(), -1, filepath, 1000, nullptr, nullptr);
    return filepath;
}
Sign up to request clarification or add additional context in comments.

3 Comments

What on earth is Platform::String ^ locationString = ... now? I'm confused by that ^, what does it stand for?
@MarcoBonelli that is C++/CX syntax used for handles to runtime managed objects (which is why I annotated that the implementation is using that language extension)
Oh that's an extension, I see, makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.