According to the CreateProcess docs, the executable name can be passed as the first argument or the command line as the second argument (from which the executable name will be extracted).
If you pass an executable name, docs PATH documents will not be found.
if you pass the command line instead, the first token is extracted for use as expected, the executable file name and PATH will be executed.
In my case, however, my call to CreateProcess --- only with the command line and with a modified environment --- does not find the executable file I am looking for. This is only successful if I precede the command line with cmd.exe /c (I understand why it works this way).
For completeness, I actually do not use the Windows API, but subprocess.Popen in Python, although I think I narrowed down the problem to the above circumstances. With shell = True correct environment is selected; with shell = False (my desired way to create a subprocess), the call cannot find my executable. The executable is a standalone exe, not a built-in cmd.exe command.
Can someone please tell me what I'm doing wrong here or where is my misunderstanding?
Code example:
from subprocess import Popen import os, sys exe = "wc.exe" # No other wc.exe on the PATH env = os.environ.copy() new_path = os.path.expandvars(r"%HOMEDRIVE%%HOMEPATH%\SmallApps\GnuWin32\bin;%PATH%") env["PATH"] = os.path.expandvars(new_path).encode(sys.getfilesystemencoding()) Popen( args=[exe, "*.*"], env=env, # shell=True # Works if you uncomment this line. )
source share