0

I want to execute and terminate Windows games from shell. The actual need is to launch games using Python upon receiving specific command. I have tried using the following code to launch game.

import subprocess

subprocess.Popen("D:/Entertainement/Games/NFS1/speed.exe",shell=False)

When I run this code, the game launches and stops without any errors. But the same game runs fine when executed from Windows Explorer. The game doesn't require administrative privileges and runs fine without that.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kishor V
  • 431
  • 4
  • 11
  • Where is the python script running? Inside a windows service or a web application or ...? – NineBerry Nov 27 '16 at 17:21
  • Yes. Inside a windows system. Runs as a websocket client – Kishor V Nov 27 '16 at 17:22
  • You can actually introspect what happened a little after `Popen` completes. See here - http://stackoverflow.com/questions/25079140/python-subprocess-popen-check-for-success-and-errors – Danish Nov 27 '16 at 17:28
  • @Danish This is what i get as a response after the program stops. `Traceback (most recent call last): File "C:\Users\devel\Desktop\temp.py", line 3, in proc = subprocess.check_output("D:/Entertainement/Games/NFS1/speed.exe", stderr=subprocess.STDOUT) File "C:\Python27\lib\subprocess.py", line 573, in check_output raise CalledProcessError(retcode, cmd, output=output) CalledProcessError: Command 'D:/Entertainement/Games/NFS1/speed.exe' returned non-zero exit status -1073741819` – Kishor V Nov 27 '16 at 17:45
  • -1073741819 is `STATUS_ACCESS_VIOLATION` (0xC0000005), which means the speed.exe process failed to read, write, or execute a memory address because the address is unallocated or protected. – Eryk Sun Nov 28 '16 at 00:40
  • @eryksun What do you think as a solution for this? The memory is not allocated or something like that? – Kishor V Nov 28 '16 at 05:15
  • Maybe it needs the working directory to be set as the application directory. Try the original `Popen` call, but with the following additional argument: `cwd='D:/Entertainement/Games/NFS1'`. – Eryk Sun Nov 28 '16 at 06:30
  • @eryksun That works too. Add it as answer. I will accept it. Thank you – Kishor V Nov 28 '16 at 06:37
  • This is a serious bug in speed.exe. Setting the working directory is just a hack to get around a bad code path. Report the bug to the developer(s) if you can. I'm not interested in writing an answer that amounts to a kludge to work around a bug. – Eryk Sun Nov 28 '16 at 07:49

1 Answers1

-1

(Posted on behalf of the OP).

I found a workaround. Instead of launching the executable directly, i created a shortcut and launched it with the following command.

import subprocess


p = subprocess.Popen("C:\Users\devel\Desktop\speed.exe.lnk",shell=True)
p.wait()
print("Program Exited with return code : "+str(p.returncode))

Now everything works fine and my Python program waits for the return code. Thank you all for replying.

halfer
  • 19,824
  • 17
  • 99
  • 186