在 cpp 中重定向标准输出

Redirecting stdout output in cpp

本文关键字:标准输出 重定向 cpp      更新时间:2023-10-16

几天来我一直在寻找有关这个问题的答案,希望你们能够帮助我。(我已经搜索并找到了一些解决方案,但每个都有自己的问题......

事情是这样的:我正在编写一个自动化的工作,它负责启动我的同事编写的代码的外部".exe"文件。由于他们编写的那些程序是提供给客户的,因此我不允许对他们的代码进行任何修改。这些程序一旦启动,就会等待特定的击键,并在收到合法击键时打印一条消息。

我的目标是:要编写将执行外部程序的程序,请向其发送击键,并从其标准输出接收输出。到目前为止,我已经能够从我的程序(使用 ShellExecute)运行该程序,并将某种键盘侦听器(使用 SendMessage)模拟到另一个程序。我可以看到它有效 - 我可以在测试程序的控制台中看到输出。

我正在尝试实时获取打印在测试程序 shell 上的消息(并在程序终止时获取大量数据),以便我可以在它发生时对其进行分析。

我尝试过的那些:

  • 将具有内联输出重定向的外部批处理文件写入文本文件。
  • 使用重新打开。
  • 执行"ShellExecute"时重定向输出。

您可以使用 stdin、stdout 和 stderr 的句柄。使用CreateProcess函数创建进程以获取该句柄。示例代码 - 对于您的情况不完整,但如何做到这一点的好例子:

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
    /*for test.exe
#include <iostream>
#include <string> */
void _tmain( int argc, TCHAR *argv[] )
{
    /*for test.exe
    std::cout << "test output" << std::endl;
    for (;;)
    {
        std::string line;
        std::getline(std::cin, line);
        std::cout << "line: " << line << std::endl;
    }
    return;*/
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        "test.exe",        // 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;
    }
   /*     HANDLE  hStdInput;
    HANDLE  hStdOutput;
    HANDLE  hStdError;*/
    HANDLE me_hStdInput = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE me_hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE proc_hStdInput = si.hStdInput;
    HANDLE proc_hStdOutput = si.hStdOutput;
                char buff[64];
                DWORD chars;
    while (!ReadConsole(me_hStdInput, buff, sizeof(buff), &chars, NULL))
    {
                    for (DWORD written = 0, writtenThisTime; written < chars; written += writtenThisTime)                   
                        if (!WriteConsole(proc_hStdOutput, buff + written, chars - written, &writtenThisTime, NULL))
                        {
                            //handle error - TODO
                        }
                }
                //possibly handle error for ReadConsole - TODO  

    // Wait until child process exits.
    //WaitForSingleObject( pi.hProcess, INFINITE );
    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}