I am trying to use Popen() to open the videogame System Shock 2 and have it stay open, the process opens and then closes after about 2 seconds due to a an access violation. No window is opened. Python gives no errors running the code. Nothing is passed through stdout or stderr. It has the return code 3221225477 which is an access violation error code. The game opens normally in all other cases I have tested. Here is the code I am running.
from subprocess import Popen
SS2 = Popen(args="C:\Program Files (x86)\Steam\steamapps\common\SS2\ss2.exe")
I have attempted to use several windows debugging tools to see why it is closing, here are some snippets from them right before it closes.
Process Monitor shows this as the last things it did before it starts closing down.
2:50:52.0748437 AM ss2.exe 16848 RegCloseKey HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\{5bf2a6f1-f431-4cbe-8692-8313eea088c3}\Properties SUCCESS
2:50:52.0752768 AM ss2.exe 16848 CreateFile C:\Users\User\AppData\Local\Programs\Python\Python311 SUCCESS Desired Access: Read Data/List Directory, Synchronize, Disposition: Open, Options: Directory, Synchronous IO Non-Alert, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened
2:50:52.0753113 AM ss2.exe 16848 QueryDirectory C:\Users\User\AppData\Local\Programs\Python\Python311\iface NO SUCH FILE FileInformationClass: FileBothDirectoryInformation, Filter: iface
2:50:52.0753277 AM ss2.exe 16848 CloseFile C:\Users\User\AppData\Local\Programs\Python\Python311 SUCCESS
2:50:52.0754329 AM ss2.exe 16848 CreateFile C:\Users\User\AppData\Local\Programs\Python\Python311 SUCCESS Desired Access: Read Data/List Directory, Synchronize, Disposition: Open, Options: Directory, Synchronous IO Non-Alert, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened
2:50:52.0754611 AM ss2.exe 16848 QueryDirectory C:\Users\User\AppData\Local\Programs\Python\Python311\iface.zip NO SUCH FILE FileInformationClass: FileBothDirectoryInformation, Filter: iface.zip
2:50:52.0754755 AM ss2.exe 16848 CloseFile C:\Users\User\AppData\Local\Programs\Python\Python311 SUCCESS
2:50:52.0755746 AM ss2.exe 16848 CreateFile C:\Users\User\AppData\Local\Programs\Python\Python311 SUCCESS Desired Access: Read Data/List Directory, Synchronize, Disposition: Open, Options: Directory, Synchronous IO Non-Alert, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened
2:50:52.0756017 AM ss2.exe 16848 QueryDirectory C:\Users\User\AppData\Local\Programs\Python\Python311\iface.crf NO SUCH FILE FileInformationClass: FileBothDirectoryInformation, Filter: iface.crf
2:50:52.0756156 AM ss2.exe 16848 CloseFile C:\Users\User\AppData\Local\Programs\Python\Python311 SUCCESS
2:50:52.0756812 AM ss2.exe 16848 QueryNameInformationFile C:\Program Files (x86)\Steam\steamapps\common\SS2\ss2.exe SUCCESS Name: \Program Files (x86)\Steam\steamapps\common\SS2\ss2.exe
2:50:52.2105046 AM ss2.exe 16848 Process Create C:\WINDOWS\SysWOW64\WerFault.exe SUCCESS PID: 16836, Command line: C:\WINDOWS\SysWOW64\WerFault.exe -u -p 16848 -s 2272
2:50:55.9768621 AM ss2.exe 16848 Thread Exit SUCCESS Thread ID: 16328, User Time: 0.0156250, Kernel Time: 0.0312500
And the last things the VS JIT debugger saw was this.
'ss2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\crypt32.dll'.
'ss2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msasn1.dll'.
'ss2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\MMDevAPI.dll'.
'ss2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\AudioSes.dll'.
'ss2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ResourcePolicyClient.dll'.
The thread 0x2234 has exited with code 0 (0x0).
The thread 0x2ce8 has exited with code 3221225477 (0xc0000005).
The thread 0x4fdc has exited with code 3221225477 (0xc0000005).
The thread 0x499c has exited with code 3221225477 (0xc0000005).
The thread 0x6a8 has exited with code 3221225477 (0xc0000005).
The thread 0x1bbc has exited with code 3221225477 (0xc0000005).
The thread 0x3630 has exited with code 3221225477 (0xc0000005).
The thread 0x4d04 has exited with code 3221225477 (0xc0000005).
The program '[8692] ss2.exe' has exited with code 3221225477 (0xc0000005) 'Access violation'.
Things I have tried: running the code in non-elevated and elevated scripts and python terminals. Setting ss2.exe to run as admin in its properties. Moving the System Shock 2 directory to somewhere else. I have made sure Popen() works on other steam games. Tried on 2 other computers. Updated GPU drivers. Tried on unmodded freshly downloaded copies of System Shock 2 and ones that have been modded with something called SS2Tool. Running with shell=True. Tried asyncio.create_subprocess_exec and got the same exact behavior.
I needed to add the working directory of the ss2 folder to Popen() by adding the argument cwd=("ss2.exe parent dir"). Now it works as expected.