Add File Redirections in MSIX using PSF

MSIX is the latest packaging technology introduced by Microsoft in 2018. This article does not focus on the basics of MSIX.

To find out what MSIX is, i recommend having a look over this 24 chapter introduction guide.

Also, in this tutorial, we make use of PSF (Package Support Framework). Again, i will not go in full details of what PSF is, but to keep it short, PSF is an open source project that helps you create simple fixes for applications to which the source code is not accessible.

The default provided fixes are:

  • File redirections (when the application must write in a location which is not accessible)
  • Add arguments to the applications (or shortcuts as previously known)
  • Run Powershell scripts during the lauch of the applications

Being open source, everybody can contribute to the code with additional fix-ups. For example, Advanced Installer added a few of their own fixes to the source code. Tim Mangan also created a PSFTooling to help add these fix-ups easier.Of course, you can write your own fix-ups if you master C++.

More details about PSF can also be found here.

What’s the problem?

Enough of the intro and let’s get to the actual problem and fix-up.

I created a simple application that creates a file near its location. This is a common scenario for per-user installed applications. Usually these create files in which they store some sort of configuration or preferences.

Now, if i get to press the “Create File” button, my application will crash.

Why does this happen? MSIX packages are installed, by default, under:

%ProgramFiles%\WindowsApps\PublisherName.AppName_AppVersion_architecture_hash

Unfortunately, applications don’t have permissions to write in the installation folder, and as mentioned before, we assume that we don’t have access to the source code to modify the application behavior.

 

What’s the fix-up?

If we open up the MSIX Package with MSIX Packaging Tool from Microsoft, we can see that it has a simple structure.Inside the VFS, we have our Sample Redirect.exe and that’s pretty much it.

Now the PSF comes into play. We have to add the PSF files, edit the AppxManifest.xml to point to the PSFLauncher.exe and create the config.json file to create the redirection.

Have i lost you? Let’s take it step by step.

 

Step 1 – Download PSF binaries

The simplest way (from my point of view) to get the PSF is to go through Nuget.

Navigate to this link and select “Direct Download”. Once Downloaded, extract the .nupkg with 7Zip or other archive tool.

That’s it, all the required files can be found in the bin folder.

 

Step 2 – Add PSF files into the MSIX package

With all the PSF files downloaded, open up the package with MSIX Packaging tool.

The following files must be added in the ROOT of the package:

  • FileRedirectionFixup32.dll
  • FileRedirectionFixup64.dll
  • PsfLauncher32.exe
  • PsfLauncher64.exe
  • PsfRunDll32.exe
  • PsfRunDll64.exe
  • PsfRuntime32.dll
  • PsfRuntime64.dll

 

At the end, the package should look something like this:

Step 3 – Modify MSIX Manifest File

In the MSIX Packaging Tool from Microsoft, in the Package Information Tab (on the left) scroll down until you see “Manifest File” and click “Open File”.

This will open up the MSIX manifest file with Notepad.

In here, navigate to your “Applications” element and look for your “Application”. In this example, we have:

<Application Id=”SAMPLEREDIRECT” Executable=”VFS\ProgramFilesX64\MyApp\Sample Redirect.exe” EntryPoint=”Windows.FullTrustApplication”>

 

This must be changed to open the PSFLauncher64.exe. We are using the 64 bit version of PSF Launcher because our application is 64 bit. In case you don’t know the application architecture, use the 32bit one and see if it works. If now, change it to the 64 bit.

In the end, it should look something like this:

<Application Id=”SAMPLEREDIRECT” Executable=”PsfLauncher64.exe” EntryPoint=”Windows.FullTrustApplication”>

Step 4 – Create and add the config.json file

We added the PSF binaries, we modified our application (shortcut) to start the PSF Launcher, now all we have to do is to create a config.json file which tells the PSF Launcher what to actually do.

A sample config.json file can be found on this extensive article from Microsoft regarding debugging and adding PSF Redirections.

In this example, the config json file looks like this:

 

Once the config.json is created, add it in the ROOT of the MSIX package again. Next save the package and install it.

 

Step 5 – Enjoy Success

 

With all the above changes to the MSIX package, now everything works as expected. The application launches and when the “Create File” button is pressed, the file is created and the application reads from it.

 

 

Wait, what the heck is going on? Let me explain:

 

“applications”: [
{
“id”: “SAMPLEREDIRECT”,
“executable”: “VFS/ProgramFilesX64/MyApp/Sample Redirect.exe”,
“workingDirectory”: “VFS/ProgramFilesX64/MyApp/”
}
],

 

If you noticed earlier in step 3, the Application ID from the manifest file was SAMPLEREDIRECT. We are now telling PSF Launcher to launch our Sample Redirect.exe for that particular ID.

If we have multiple applications in the package, PSF Launcher must know what executable to launch for each ID. This is a very important step!

Working Directory is optional, but this refers in which directory the application launches.

 

“processes”: [
{
“executable”: “.*”,
“fixups”: [
{
“dll”: “FileRedirectionFixup64.dll”,
“config”: {
“redirectedPaths”: {
“packageRelative”: [
{
“base”: “VFS/ProgramFilesX64/MyApp/”,
“patterns”: [
“.*\\.txt”

 

Next, the PSF Launcher must know what fix-up to use for the application. In our case, we use the File Redirection fixup, so the FileRedirectionFixup64.dll must be loaded.

Then, the PSF monitors if any type of file with .txt extension is getting created/modified in VFS/ProgramFilesX64/MyApp/ and redirects it to a writable location, from where the application can access it.

The location where the file is redirected is:

%localappdata%\Packages\RedirectPSF_r21n0w1rc5s2y\LocalCache\Local\Microsoft\WritablePackageRoot\VFS\ProgramFilesX64\MyApp

Sharing is caring!

Leave a comment

Your email address will not be published. Required fields are marked *

four × 5 =