Windows c++ executes the command line and waits for its return value cmd /c

 correct way 1

cmd.exe /c D:\working\sdp\developing\SDPCheckTool\SDP_Debug\example.exe  > D:\working\sdp\developing\SDPCheckTool\SDP_Debug\wintunlog.txt 2>&1

 correct way 2

cmd.exe /c ""D:\working\sdp\developing\SDPCheckTool\SDP_Debug\example.exe" > "D:\working\sdp\developing\SDPCheckTool\SDP_Debug\wintunlog.txt" 2>&1"

The wrong method, the method is to copy the command line to the cmd window and it can run normally, but if you put it in the program and use ShellExecute to call it, there will be problems. Pressing win+r will also cause problems in the running window, mainly because a pair of quotation marks is missing

cmd.exe /c "D:\working\sdp\developing\SDPCheckTool\SDP_Debug\example.exe" > "D:\working\sdp\developing\SDPCheckTool\SDP_Debug\wintunlog.txt" 2>&1

C++ calls the implementation of 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++ uses system() to implement. This method can also be executed with the wrong command line, which has caused problems with my previous createfile. This method cannot hide the cmd window.

  // 要运行的可执行文件的路径
        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);

Finally, the method implemented by 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);
        }

Guess you like

Origin blog.csdn.net/babytiger/article/details/130252159