Paths and CreateProcess

前端 未结 4 1999
小蘑菇
小蘑菇 2021-01-15 16:46

I have a question regarding a symptom of my misuse of CreateProcess. I\'m using the lpcommandline parameter to feed the path to my executable and parameters. My misuse is t

4条回答
  •  既然无缘
    2021-01-15 17:21

    I just struggled with the same problem for quite some time. So even if this question was raised a long time ago, just for the record, here is what the problem was in my case:

    If the command line is not quoted and contains spaces, CreateProcess will try to resolve the ambiguity as described in Simon's answer. If any of the tested parts up to a space character also resolves to an existing file with no or a .exe extension, this file will be used instead of the intended complete path.

    Example:

    char cmdline[] = "C:\Program Files\App One\bin\app.exe param1 param2";
    CreateProcess(NULL, cmdline, ...);
    

    Unfortunately there actually was an existing file called "C:\Program Files\App" (no extension) in my case. CreateProcess found this file, assumed it was an executable with no .exe extension and tried to execute it. Result: error 193 "%1 is not a valid Win32 application".

    Bottom line: use quotes or even better, the first argument to CreateProcess, or go looking for any other file which might match part of the offending path.

提交回复
热议问题