简体   繁体   English

如何在C ++中将执行的输出cmd行保存在文件中

[英]how to save executed output cmd line in file in c++

I have this code for execute CMD line in c++ 我有这段代码用于在C ++中执行CMD行

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

void _tmain( int argc, TCHAR *argv[] )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    if( argc != 2 )
    {
        printf("Usage: %s [cmdline]\n", argv[0]);
        return;
    }

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        argv[1],        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

I want save executed output in file.but how? 我想将执行的输出保存到file.but如何?

Either redirect stdout to a file with 可以使用以下命令将标准输出重定向到文件

freopen("file.txt", "w", stdout);

Or, pipe the output to a file with windows 或者,使用Windows将输出通过管道传输到文件

cmd> prg.exe > file.txt

In CreateProcess you pass a STARTUPINFO-structure. 在CreateProcess中,您传递一个STARTUPINFO结构。 You can set the STARTF_USESTDHANDLES in si.dwFlags and then fill out the hStdInput, hStdOutput, and hStdError-fields with valid file-descriptors, especially hStdOutput should be a handle to a previously opened file (returned by successful CreateFile) which will then receive the std-output of the started process. 您可以在si.dwFlags中设置STARTF_USESTDHANDLES,然后用有效的文件描述符填充hStdInput,hStdOutput和hStdError字段,尤其是hStdOutput应该是先前打开的文件(由成功的CreateFile返回)的句柄,然后该文件将接收std-启动进程的输出。

Edit: 编辑:

This was kind of mean answer, because it needs more work to make that thing work: You need to create that file with the right SECURITY_ATTRIBUTES and have to set the Set handle inheritance to TRUE in CreateProcess. 这是一种卑鄙的回答,因为要使该功能正常工作还需要更多的工作:您需要使用正确的SECURITY_ATTRIBUTES创建该文件,并且必须在CreateProcess中将Set handle inheritance为TRUE。 So it's also kind of purists nightmare to do it that way. 因此,这样做也是纯粹主义者的噩梦。

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

void _tmain( int argc, TCHAR *argv[] )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    si.dwFlags |=STARTF_USESTDHANDLES ;
    si.hStdInput=GetStdHandle(STD_INPUT_HANDLE);
    si.hStdError=GetStdHandle(STD_ERROR_HANDLE);
    SECURITY_ATTRIBUTES sa;
    ZeroMemory( &sa, sizeof(sa) );
    sa.nLength=sizeof(sa);
    sa.bInheritHandle=TRUE;
    si.hStdOutput=CreateFile ("log.txt", GENERIC_READ|GENERIC_WRITE, 0, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    ZeroMemory( &pi, sizeof(pi) );

    if( argc != 2 )
    {
        printf("Usage: %s [cmdline]\n", argv[0]);
        return;
    }
    // Start the child process.
    if( !CreateProcess( NULL,   // No module name (use command line)
        argv[1],        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        TRUE,           // Set handle inheritance to TRUE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    )
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }
    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles.
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    CloseHandle (si.hStdOutput);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM