Andreas Gajdosik
Andreas Gajdosik

Reputation: 190

How to Create-Process and wait for it to finish, when the executable is installed from MS Store in C:/Program Files/WindowsApps

I am working on a small background daemon implemented in Go. It needs to start a Blender to process some 3D models (.blend files) in background. For this I just create a new process in Go:

cmd := exec.Command(
  binaryPath,
  "--background",
  "--factory-startup", // disables user preferences, addons, etc.
  "-noaudio",
  blendPath,
  "--python", unpackScriptPath,
  "--",
  dataFile,
  addonModuleName, // Legacy has it as "blenderkit", extensions have it like bl_ext.user_default.blenderkit or anything else
  )

cmd.Env = append(os.Environ(), fmt.Sprintf("BLENDER_USER_SCRIPTS=%v", blenderUserScripts))

// Redirect both stdout and stderr to the buffer
var combinedOutput bytes.Buffer
cmd.Stdout = &combinedOutput
cmd.Stderr = &combinedOutput

err = cmd.Run() #wait for process to finish, later check the results

This works just fine on Linux, MacOS and on most Windows cases. The problem is however when Blender was installed via Microsoft Store. Then it is located in C:\Program Files\WindowsApps\... and this location is protected. Which leads to error:

command execution failed: fork/exec C:\Program 
Files\WindowsApps\BlenderFoundation.Blender_4.1.1.0_x64__ppwjx1n5r4v9t\Blender\blender.exe: Access is denied.

I have found that I can start UWP application using this PowerShell command:

Start-Process shell:$("AppsFolder\" + (Get-AppxPackage | ?{$_.PackageFamilyName -like '*Blender*'}).PackageFamilyName + "!Blender")

This works fine and seems like a proper way how to start the applications installed from Microsoft Store. But I need to wait for the process to finish. And here comes the problem.

The -Wait flag throws an error (even though Blender starts just fine):

PS C:\Users\andreas> Start-Process shell:$("AppsFolder\" + (Get-AppxPackage | ?{$_.PackageFamilyName -like '*Blender*'}).PackageFamilyName + "!Blender") -Wait
Start-Process : This command cannot be run completely because the system cannot find all the information required.
At line:1 char:1
+ Start-Process shell:$("AppsFolder\" + (Get-AppxPackage | ?{$_.Package ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

So I have tried to catch the PID using -PassThru flag, but this again throws an error and returns nothing (again Blender runs).

PS C:\Users\andreas> $p = Start-Process shell:$("AppsFolder\" + (Get-AppxPackage | ?{$_.PackageFamilyName -like '*Blender*'}).PackageFamilyName + "!Blender") -PassThru
Start-Process : This command cannot be run completely because the system cannot find all the information required.
At line:1 char:6
+ $p = Start-Process shell:$("AppsFolder\" + (Get-AppxPackage | ?{$_.Pa ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

My another idea was to redirect Stdout and Stderr and wait until Blender quit is present, but again it fails:

PS C:\Users\andreas> Start-Process shell:$("AppsFolder\" + (Get-AppxPackage | ?{$_.PackageFamilyName -like '*Blender*'}).PackageFamilyName + "!Blender") -RedirectStandardOutput "C:\Users\andreas\blenderkit_data\stdout.log" -RedirectStandardError "C:\Users\andreas\blenderkit_data\stderr.log"
Start-Process : This command cannot be run due to the error: The system cannot find the file specified.
At line:1 char:1
+ Start-Process shell:$("AppsFolder\" + (Get-AppxPackage | ?{$_.Package ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

Am I doing something wrong? Is there any other and better API? Or really the only way how to start a process installed from Microsoft Store is to use the shell:AppsFolder syntax and really this syntax does not support neither -Wait, -PassThru, -RedirectStandardOutput, -RedirectStandardError?

Upvotes: 4

Views: 82

Answers (0)

Related Questions