How does CreateProcess find the executable?

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. ) 
+4
source share
3 answers

You need to change the environment of the current process if you want CreateProcess to see it. Currently, a subshell (included in the command line or requested through shell=True ) is visible in your modified environment, but the direct call to CreateProcess not.

+3
source

If I read your question correctly, it looks like you have an application named wc.exe in the folder that %HOMEDRIVE%%HOMEPATH%\SmallApps\GnuWin32\bin is attached to. If so, you better install exe in the advanced version of %HOMEDRIVE%%HOMEPATH%\SmallApps\GnuWin32\bin\wc.exe . Since this path and the name of the executable file may contain spaces, this will not prevent you from wrapping it in quotation marks.

In short, don't rely on finding ways. It is not only error prone, but also a potential security hole.

0
source

Thought of checking MSDN? CreateProcess documentation .

To quote one part of it:

The directory from which the application was downloaded. The current directory for the parent process. 32-bit Windows system directory. Use the GetSystemDirectory function to get the path to this directory. Windows 16-bit system directory. There is no function that gets the path to this directory, but a search is performed. The name of this directory is System. Windows directory. Use the GetWindowsDirectory function to get the path to this directory. Directories listed in the PATH environment variable. Please note that this function does not look for the path to each application specified in the application registry key. To include this path for each application in the search sequence, use the ShellExecute function.

-1
source

All Articles