正确的方法1
cmd.exe /c D:\working\sdp\developing\SDPCheckTool\SDP_Debug\example.exe > D:\working\sdp\developing\SDPCheckTool\SDP_Debug\wintunlog.txt 2>&1
正确的方法2
cmd.exe /c ""D:\working\sdp\developing\SDPCheckTool\SDP_Debug\example.exe" > "D:\working\sdp\developing\SDPCheckTool\SDP_Debug\wintunlog.txt" 2>&1"
错误方法 ,方法将命令行复制到cmd窗口可以正常运行,但是放到程序中用ShellExecute调用就会出问题,按下win+r 的运行窗口也会出问题,主要是因为少了一对引号
cmd.exe /c "D:\working\sdp\developing\SDPCheckTool\SDP_Debug\example.exe" > "D:\working\sdp\developing\SDPCheckTool\SDP_Debug\wintunlog.txt" 2>&1
c++ 调用 createfile的实现
wstring exePath = L"\""+CUtils::GetDir() + L"\\example.exe"+L"\"" ;
// 要传递给可执行文件的参数
wstring args = L" > \""+ CUtils::GetDir() + L"\\driverlog.txt\" 2>&1";
wstring commandLine = L"cmd.exe /c \"" + exePath + args +L"\"";
// 创建一个STARTUPINFO结构体
STARTUPINFO si = { sizeof(STARTUPINFO) };
// 设置STARTF_USESHOWWINDOW标志以隐藏命令行窗口
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
// 创建一个PROCESS_INFORMATION结构体
PROCESS_INFORMATION pi = {};
// 执行命令行并隐藏命令行窗口
BOOL Ret = FALSE;
Ret = CreateProcess(NULL,
const_cast<LPWSTR>(commandLine.c_str()),
NULL,
NULL,
FALSE,
CREATE_NO_WINDOW,
NULL,
CUtils::GetDir().c_str(), &si, &pi);
if (Ret)
{
// 等待进程完成并获取返回值
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exitCode = 0;
GetExitCodeProcess(pi.hProcess, &exitCode);
// 关闭进程句柄
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
// 打开日志文件
wstring logFilePath = L"\"" + CUtils::GetDir() + L"\\driverlog.txt" + L"\"";
HINSTANCE hInstance2 = ShellExecute(NULL, L"open", logFilePath.c_str(), NULL, CUtils::GetDir().c_str(), SW_SHOWDEFAULT);
}
c++使用system()来实现,这种方法用错误的命令行也可以执行,导致我前面的createfile一直有问题,这个方法无法隐藏cmd窗口
// 要运行的可执行文件的路径
wstring exePath = L"\""+CUtils::GetDir() + L"\\example.exe"+L"\"" ;
// 要传递给可执行文件的参数
wstring args = L" > \""+ CUtils::GetDir() + L"\\driverlog.txt\" 2>&1";
//下面的命令可以执行,但是不推荐这样写,放到createprocess,或者shellexeclude中就不好用了
// wstring commandLine = L"cmd.exe /c " + exePath + args;
//下面的方法才是正确的
wstring commandLine = L"cmd.exe /c \"" + exePath + args + L"\"";
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::string narrowCommand = converter.to_bytes(commandLine);
int returnCode = system(narrowCommand.c_str());
wstring logFilePath = L"\"" + CUtils::GetDir() + L"\\driverlog.txt" + L"\"";
HINSTANCE hInstance2 = ShellExecute(NULL, L"open", logFilePath.c_str(), NULL, CUtils::GetDir().c_str(), SW_SHOWDEFAULT);
最后shellexeclude实现的方法
wstring exePath = L"\""+CUtils::GetDir() + L"\\example.exe"+L"\"" ;
// 要传递给可执行文件的参数
wstring args = L" > \""+ CUtils::GetDir() + L"\\driverlog.txt\" 2>&1";
wstring commandLine = L" /c \"" + exePath + args +L"\"";
SHELLEXECUTEINFO sei = { 0 };
sei.cbSize = sizeof(sei);
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = L"open";
sei.lpFile = L"cmd";
sei.lpParameters = commandLine.c_str();
sei.lpDirectory = CUtils::GetDir().c_str();
sei.nShow = SW_SHOW;
if (ShellExecuteEx(&sei))
{
// Wait for the process to exit
WaitForSingleObject(sei.hProcess, INFINITE);
// Close the process handle
CloseHandle(sei.hProcess);
wstring logFilePath = L"\"" + CUtils::GetDir() + L"\\driverlog.txt" + L"\"";
HINSTANCE hInstance2 = ShellExecute(NULL, L"open", logFilePath.c_str(), NULL, NULL, SW_SHOWDEFAULT);
}