在 cpp 中重定向标准输出
Redirecting stdout output in cpp
几天来我一直在寻找有关这个问题的答案,希望你们能够帮助我。(我已经搜索并找到了一些解决方案,但每个都有自己的问题......
事情是这样的:我正在编写一个自动化的工作,它负责启动我的同事编写的代码的外部".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 );
}
相关文章:
- 将标准输出重定向到ostream
- 在程序执行期间从标准输出重定向到自定义流
- 将 Tcl 中的命令输出重定向到文件时,文件以 DOS 格式生成
- 将 Linux system() 调用命令的输出重定向到仅可变的
- 将 ffmpeg 控制台输出重定向到C++中的字符串或文件
- Win32 GUI C(++) 应用将标准输出和标准输出重定向到磁盘上的同一文件
- 将线程之间的标准输入/输出重定向
- GDB Cygwin 不会将输出重定向到文件
- 如何将 printf 流输出重定向到.csv文件
- 将PowerShell.exe的输入和输出重定向到C 中的管道
- Windows Batch - 如何在将标准输出重定向到文件时添加时间戳
- 将一个进程标准输出重定向到管道将产生不同的运行结果
- 如何将我的应用程序生成的标准 Mac OS X 日志输出重定向到文件
- 正在将控制台输出重定向到同一解决方案中的MFC屏幕输出
- 函数 system() 中的输出重定向似乎不起作用
- CreateProcess:将子标准输出重定向到C++/Windows中的父标准输出
- 将标准输出重定向为整数
- ReadFile, CreateProcess, Pipe标准输出重定向,双倍输出
- 将标准输出重定向到缓冲区,不使用>
- 标准输出重定向是如何工作的