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.

TomTsagk
  • 1,474
  • 9
  • 23
  • @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. – TomTsagk May 11 '21 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) – UnholySheep Aug 11 '21 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. – TomTsagk Aug 11 '21 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 – UnholySheep Aug 11 '21 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 :) – TomTsagk Aug 11 '21 at 15:30
  • @TomTsagk There is definitely a way to do this in pure C but I don't know if you'll be able to handle it. Reply me here and I may consider posting this - I've a code that opens a local "rasphone.pbk" file for my UWP VPN app with pure C and also it read writes strings from it. – AnArrayOfFunctions Aug 14 '21 at 13:15
  • @AnArrayOfFunctions Any information on the topic is highly useful. If you could provide details and possibly steps of how to approach this, I'd greatly appreciate it. Even better if you could share parts of a working project. – TomTsagk Aug 14 '21 at 18:41
  • @TomTsagk I do have the exact code that does what I told you but also look at the C backbone I've created for consuming WinRT (specifically UWP) components [Bluetooth UWP example](https://github.com/6a4h8/BTHBenchSimple). It's similar for any UWP component you want to consume (including `Windows.Storage`). Also look [here](https://stackoverflow.com/a/68363800/4031604). – AnArrayOfFunctions Aug 14 '21 at 18:45

1 Answers1

1

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;
}
UnholySheep
  • 3,967
  • 4
  • 19
  • 24