C++ - CreateProcess 失败代码 2
C++ - CreateProcess failed code 2
我正在尝试使用 CreateProcess() 函数来启动位于我的根目录(我的 VS 解决方案所在的目录)的文件夹中的 .exe 应用程序。看起来很简单吧?可能是,但我一生都无法注意到我做错了什么。每次我尝试启动 .exe 时,我都会收到错误消息 "CreateProcess failed code 2",这意味着找不到我尝试启动的 .exe 文件。
我的代码:
void HavNetProfiler::LaunchClumsy()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if (!CreateProcess((LPCTSTR)"Clumsy\clumsy.exe", // No module name (use command line)
NULL, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
return;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
我是不是用错了这个功能?我是否误解了它的工作原理,或者我只是错过了一些小细节?我在另一个文件夹中的文件中调用函数 LaunchClumsy()
(该文件夹存在于根文件夹中,就像 "Clumsy" 文件夹一样)。那会有什么不同吗?
谢谢!
代码中有 2 个直接错误:
LPCTSTR
转换错误。如果您的代码在没有该转换的情况下无法编译,则您传递了一个带有错误字符编码的参数。将其更改为 L"Clumsy\clumsy.exe"
以显式传递 wide-character 字符串。
- 使用相对路径很可能会失败。系统从当前工作目录开始搜索。那是一个 process-wide 属性,可以随时由任何线程更改。请改用绝对路径。
我正在尝试使用 CreateProcess() 函数来启动位于我的根目录(我的 VS 解决方案所在的目录)的文件夹中的 .exe 应用程序。看起来很简单吧?可能是,但我一生都无法注意到我做错了什么。每次我尝试启动 .exe 时,我都会收到错误消息 "CreateProcess failed code 2",这意味着找不到我尝试启动的 .exe 文件。
我的代码:
void HavNetProfiler::LaunchClumsy()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if (!CreateProcess((LPCTSTR)"Clumsy\clumsy.exe", // No module name (use command line)
NULL, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
return;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
我是不是用错了这个功能?我是否误解了它的工作原理,或者我只是错过了一些小细节?我在另一个文件夹中的文件中调用函数 LaunchClumsy()
(该文件夹存在于根文件夹中,就像 "Clumsy" 文件夹一样)。那会有什么不同吗?
谢谢!
代码中有 2 个直接错误:
LPCTSTR
转换错误。如果您的代码在没有该转换的情况下无法编译,则您传递了一个带有错误字符编码的参数。将其更改为L"Clumsy\clumsy.exe"
以显式传递 wide-character 字符串。- 使用相对路径很可能会失败。系统从当前工作目录开始搜索。那是一个 process-wide 属性,可以随时由任何线程更改。请改用绝对路径。