[python-win32] win32process.CreateProcess: '%1 is not a valid Win32 application.'

Eryk Sun eryksun at gmail.com
Tue Dec 8 17:35:54 EST 2020


On 12/8/20, Niko Pasanen <niko at pasanen.me> wrote:
>
> path_to_exe = Path(r"C:\path to some\folder\some.exe")

FYI, the constructor of pathlib.Path supports forward slash as the
path separator, which avoids the need to use a raw string. A
WindowsPath instance uses backslash when converting back to a string.

> (h_process, _, dw_process_id, _) = win32process.CreateProcess(
>     None,  # module name
>     str(path_to_exe),  # command line

Since you just have an executable path, pass it in lpApplicationName
and leave lpCommandLine as None. This is simpler than manually quoting
the path.

If the string in lpCommandLine is an unquoted executable path, Windows
will consume spaces in it until it finds a non-directory match. For
example, if it's r"C:\path to\some folder\some.exe", it will use
r"C:\path to\some" or r"C:\path to\some.exe" if either exists and is
not a directory. If the matching file isn't a valid PE image, it does
not continue parsing the command line to consume the next space as
"some folder". It simply fails the call with ERROR_BAD_EXE_FORMAT.

> 1) The following DLLs on *both* machines with same path (mine & Person C)
> -  "python.exe"

ERROR_BAD_EXE_FORMAT occurs when mapping the executable image into the
process VM during the NtCreateUserProcess system call. At this stage,
no dependent DLLs are mapped into the process. That occurs after the
process has already been created, during process initialization. If it
were a dependent DLL problem, then CreateProcess would succeed, but
the child process would abnormally terminate with a status code such
as STATUS_INVALID_IMAGE_FORMAT (0xC000007B).


More information about the python-win32 mailing list