C++ 如何使用 CreateProcess 将输出重定向到文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7018228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:22:16  来源:igfitidea点击:

How do I redirect output to a file with CreateProcess?

c++cwinapiio-redirection

提问by Paul Manta

I tried using CreateProcess to run a simple command like hg > test.txt. I tried running the string as a whole (as opposed to separating it into an application name and its parameters). Why does CreateProcess(0, "notepad.exe test.txt", ...)work but CreateProcess(0, "hg > test.txt", ...)does not?

我尝试使用 CreateProcess 运行一个简单的命令,如hg > test.txt. 我尝试将字符串作为一个整体运行(而不是将其分成应用程序名称及其参数)。为什么CreateProcess(0, "notepad.exe test.txt", ...)有效但CreateProcess(0, "hg > test.txt", ...)无效?

采纳答案by David Heffernan

You can't use stdout redirection in the command line passed to CreateProcess. To redirect stdout you need to specify a file handle for the output in the STARTUPINFOstructure.

您不能在传递给CreateProcess. 要重定向标准输出,您需要为STARTUPINFO结构中的输出指定一个文件句柄。

You are also making another, more subtle, mistake. The second parameter, lpCommandLinemust point to writeable memory because CreateProcessoverwrites the buffer. If you happen to be using the ANSI version of the function then you will get away with this, but not for the Unicode version.

你也在犯另一个更微妙的错误。第二个参数,lpCommandLine必须指向可写内存,因为会CreateProcess覆盖缓冲区。如果您碰巧使用了该函数的 ANSI 版本,那么您可以避免使用此功能,但不适用于 Unicode 版本。

The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a constvariable or a literal string). If this parameter is a constant string, the function may cause an access violation.

此函数的 Unicode 版本CreateProcessW可以修改此字符串的内容。因此,此参数不能是指向只读内存的指针(例如const变量或文字字符串)。如果此参数是一个常量字符串,该函数可能会导致访问冲突。

回答by Lukas Koblovsky

The code below creates a console-less process with stdout and stderr redirected to the specified file.

下面的代码创建一个无控制台进程,stdout 和 stderr 重定向到指定的文件。

#include <windows.h>


int _tmain(int argc, _TCHAR* argv[])
{
    SECURITY_ATTRIBUTES sa;
    sa.nLength = sizeof(sa);
    sa.lpSecurityDescriptor = NULL;
    sa.bInheritHandle = TRUE;       

    HANDLE h = CreateFile(_T("out.log"),
        FILE_APPEND_DATA,
        FILE_SHARE_WRITE | FILE_SHARE_READ,
        &sa,
        OPEN_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL );

    PROCESS_INFORMATION pi; 
    STARTUPINFO si;
    BOOL ret = FALSE; 
    DWORD flags = CREATE_NO_WINDOW;

    ZeroMemory( &pi, sizeof(PROCESS_INFORMATION) );
    ZeroMemory( &si, sizeof(STARTUPINFO) );
    si.cb = sizeof(STARTUPINFO); 
    si.dwFlags |= STARTF_USESTDHANDLES;
    si.hStdInput = NULL;
    si.hStdError = h;
    si.hStdOutput = h;

    TCHAR cmd[]= TEXT("Test.exe 30");
    ret = CreateProcess(NULL, cmd, NULL, NULL, TRUE, flags, NULL, NULL, &si, &pi);

    if ( ret ) 
    {
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
        return 0;
    }

    return -1;
}

回答by Tamas Demjen

Microsoft has an example how to redirect the standard output: http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx.

Microsoft 有一个如何重定向标准输出的示例:http: //msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx

回答by MK.

CreateProcess() launches processes, it is not a command line itnerpreter. It doesn't know what ">" is and won't do the stream redirection for you. You need to open the file test.txt yourself and pass the handle to it to CreateProcess inside the STARTUPINFO structure: CreateProcessSTARTUPINFO

CreateProcess() 启动进程,它不是命令行解释器。它不知道 ">" 是什么,也不会为您进行流重定向。您需要自己打开文件 test.txt 并将其句柄传递给 STARTUPINFO 结构内的CreateProcessCreateProcess STARTUPINFO

回答by Boris Yaroslav

you should run process cmd.exe with params "/c command line". This will redirect the output to a file or to organize a pipeline through CreateProcess.

您应该使用参数“/c 命令行”运行进程 cmd.exe。这会将输出重定向到文件或通过 CreateProcess 组织管道。