常见错误包括未将可执行文件的路径指定为 CreateProcess 的第一个参数,以及未在第二个参数中引用可执行文件的路径
CreateProcess( <exe path goes here> , <quoted exe path plus parameters goes here>, ... );
像这样:
std::wstring executable_string(_T("c:\\program files\\myprogram\\executable.exe"));
std::wstring parameters(_T("-param1 -param2"));
wchar_t path[MAX_PATH+3];
PathCanonicalize(path, executable_string.c_str());
path[sizeof(path)/sizeof(path[0]) - 1] = _T('\0');
// Make sure that the exe is specified without quotes.
PathUnquoteSpaces(path);
const std::wstring exe = path;
// Make sure that the cmdLine specifies the path to the executable using
// quotes if necessary.
PathQuoteSpaces(path);
std::wstring cmdLine = path + std::wstring(_T(" ")) + parameters;
BOOL res = CreateProcess(
exe.c_str(),
const_cast<wchar_t *>(cmdLine.c_str()),
...);
I just copied and adapted some, so there might be some errors in the above, but the idea is there. Make sure to use path without quotes in the first parameter and path with quotes in the second and you should be fine.