0

我有一个函数,它读取一个注册表项,在那里获取一个程序的路径,然后用这个路径作为第二个参数调用一个 CreateProcessA。调试应用程序,它失败说找不到文件。

a) 是的,该文件存在 b) 是的,我有权执行该文件

问题:实际读取 reg 键并给出 CreateProcessA 路径的函数不会转义路径:这意味着 CreateProcessA 接收到一个类似于“C:\Program Files\prog.exe”的字符串,而不是“C:\ \程序文件\\prog.exe”。那是问题吗?是否存在任何 Windows 函数来自动转义所有反斜杠?

4

1 回答 1

0

常见错误包括未将可执行文件的路径指定为 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.

于 2012-06-19T08:15:17.773 回答