cmd.exe 使用 CreateProcess 调用后立即关闭
cmd.exe immediately closes after calling with CreateProcess
我正在尝试使用 CreateProcess 函数执行批处理文件,但是 cmd.exe 立即关闭而没有打开执行批处理文件。
还传递了一个参数(目录路径)。
C++ 代码是:
int main()
{
std::wstring cmdPath;
std::wstring batchFile;
batchFile = L"\"B:\code\Batch\all_files.bat\"";
std::wstring args = L"\"B:\code\Batch\"";
{
wchar_t cmdP[500] = L" ";
GetEnvironmentVariable(L"COMSPEC", cmdP, 500);
cmdPath = cmdP;
}
std::wstring CmdLine = L"/c " + batchFile + L" " + args;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (!
CreateProcess
(
cmdPath.c_str(),
const_cast<LPWSTR>(CmdLine.c_str()),
NULL, NULL, FALSE,
CREATE_NEW_CONSOLE,
NULL, NULL,
&si,
&pi
)
)
{
std::cout << "bad";
std::cin.get();
return 1;
}
std::cout << "yay......\n";
std::cin.get();
return 0;
}
批处理文件是
echo hello.
set directory=%~1
cd %directory%
dir > files.txt
pause
exit 0
输出
yay......
已获取,但已获取文件 files.txt 或 echo hello.
的输出。
答案似乎是需要用引号将整个命令行(\c 除外)括起来。
因此程序变为:
int main()
{
std::wstring cmdPath;
std::wstring batchFile;
batchFile = L"\"B:\Harith Source code\Batch\all_files.bat\"";
std::wstring args = L"\"B:\Harith Source code\Batch\"";
{
wchar_t cmdP[500] = L" ";
GetEnvironmentVariable(L"COMSPEC", cmdP, 500);
cmdPath = cmdP;
}
std::wstring CmdLine = L"/c " + std::wstring(L"\"") + batchFile + L" " + args + L"\"";
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (!
CreateProcess
(
cmdPath.c_str(),
const_cast<LPWSTR>(CmdLine.c_str()),
NULL, NULL, FALSE,
CREATE_NEW_CONSOLE,
NULL, NULL,
&si,
&pi
)
)
{
std::cout << "bad";
std::cin.get();
return 1;
}
std::cout << "yay......\n";
std::cin.get();
return 0;
}
编辑:对于最后的代码,我将 /k 改回 /c
我正在尝试使用 CreateProcess 函数执行批处理文件,但是 cmd.exe 立即关闭而没有打开执行批处理文件。
还传递了一个参数(目录路径)。
C++ 代码是:
int main()
{
std::wstring cmdPath;
std::wstring batchFile;
batchFile = L"\"B:\code\Batch\all_files.bat\"";
std::wstring args = L"\"B:\code\Batch\"";
{
wchar_t cmdP[500] = L" ";
GetEnvironmentVariable(L"COMSPEC", cmdP, 500);
cmdPath = cmdP;
}
std::wstring CmdLine = L"/c " + batchFile + L" " + args;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (!
CreateProcess
(
cmdPath.c_str(),
const_cast<LPWSTR>(CmdLine.c_str()),
NULL, NULL, FALSE,
CREATE_NEW_CONSOLE,
NULL, NULL,
&si,
&pi
)
)
{
std::cout << "bad";
std::cin.get();
return 1;
}
std::cout << "yay......\n";
std::cin.get();
return 0;
}
批处理文件是
echo hello.
set directory=%~1
cd %directory%
dir > files.txt
pause
exit 0
输出
yay......
已获取,但已获取文件 files.txt 或 echo hello.
的输出。
答案似乎是需要用引号将整个命令行(\c 除外)括起来。
因此程序变为:
int main()
{
std::wstring cmdPath;
std::wstring batchFile;
batchFile = L"\"B:\Harith Source code\Batch\all_files.bat\"";
std::wstring args = L"\"B:\Harith Source code\Batch\"";
{
wchar_t cmdP[500] = L" ";
GetEnvironmentVariable(L"COMSPEC", cmdP, 500);
cmdPath = cmdP;
}
std::wstring CmdLine = L"/c " + std::wstring(L"\"") + batchFile + L" " + args + L"\"";
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (!
CreateProcess
(
cmdPath.c_str(),
const_cast<LPWSTR>(CmdLine.c_str()),
NULL, NULL, FALSE,
CREATE_NEW_CONSOLE,
NULL, NULL,
&si,
&pi
)
)
{
std::cout << "bad";
std::cin.get();
return 1;
}
std::cout << "yay......\n";
std::cin.get();
return 0;
}
编辑:对于最后的代码,我将 /k 改回 /c