Windows c ++ ejecuta la línea de comando y espera su valor de retorno cmd /c

 manera correcta 1

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

 manera correcta 2

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

El método incorrecto, el método es copiar la línea de comando a la ventana de cmd y puede ejecutarse normalmente, pero si lo coloca en el programa y usa ShellExecute para llamarlo, habrá problemas. Presionar win+r también causará problemas en la ventana en ejecución, principalmente porque faltan un par de comillas

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

C++ llama a la implementación de 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 ++ usa system () para implementar. Este método también se puede ejecutar con la línea de comando incorrecta, lo que ha causado problemas con mi archivo de creación anterior. Este método no puede ocultar la ventana 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);

Finalmente, el método implementado por 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);
        }

Supongo que te gusta

Origin blog.csdn.net/babytiger/article/details/130252159
Recomendado
Clasificación