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

stdout をファイルにリダイレクトするか、

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

または、ウィンドウを使用して出力をファイルにパイプします

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 に答える