C++ 中的系统调用打开一个新终端。 windows
system call in c++ opens a new terminal . windows
我正在尝试用 c++ 编写系统命令。
std::string cmd = " DIR > d:\a.txt" ;
int isystemOut2 = system (cmd.c_str());
当我 运行 它时,我看到 d:\a.txt 有正确的输出,但我看到命令提示符立即打开和关闭。有没有办法阻止打开新的命令提示符。
您可以使用 windows API 函数。例如 CreateProcess.
STARTUPINFOW si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (CreateProcessW(cmd, arg, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
{
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
我正在尝试用 c++ 编写系统命令。
std::string cmd = " DIR > d:\a.txt" ;
int isystemOut2 = system (cmd.c_str());
当我 运行 它时,我看到 d:\a.txt 有正确的输出,但我看到命令提示符立即打开和关闭。有没有办法阻止打开新的命令提示符。
您可以使用 windows API 函数。例如 CreateProcess.
STARTUPINFOW si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (CreateProcessW(cmd, arg, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
{
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}