在 MFC 等环境下调用 system 函数时,会短暂闪烁一个控制台窗口。解决方法是在创建进程时将窗口设置为隐藏。
void Utils::system_hide_cmd(const string &cmd)
{
WCHAR path[MAX_PATH];
GetSystemDirectoryW(path, MAX_PATH);
string x = Utils::toAscii(path);
x += "\\cmd.exe";
SHELLEXECUTEINFOW ShExecInfo = { 0 };
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = Utils::toUnicode(x.c_str());
ShExecInfo.lpParameters = Utils::toUnicode(cmd.c_str());
ShExecInfo.nShow = SW_HIDE;
ShExecInfo.hInstApp = NULL;
ShellExecuteExW(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess, INFINITE);////wait for exit
}