4

我有这段代码用于在 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 );
}

我想将执行的输出保存在文件中。但是如何?

4

2 回答 2

3

将标准输出重定向到一个文件

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

或者,将输出通过管道传输到带有 windows 的文件

cmd> prg.exe > file.txt
于 2012-12-21T20:10:45.783 回答
3

在 CreateProcess 中,您传递一个 STARTUPINFO 结构。您可以在 si.dwFlags 中设置 STARTF_USESTDHANDLES,然后使用有效的文件描述符填写 hStdInput、hStdOutput 和 hStdError 字段,尤其是 hStdOutput 应该是以前打开的文件的句柄(由成功的 CreateFile 返回),然后它将接收已启动进程的标准输出。

编辑:

这是一个刻薄的答案,因为它需要做更多的工作才能使这件事起作用:您需要使用正确的 SECURITY_ATTRIBUTES 创建该文件,并且必须Set handle inheritance在 CreateProcess 中将其设置为 TRUE。因此,这样做也是纯粹主义者的噩梦。

#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);
}
于 2012-12-21T20:21:20.430 回答