利用管道获取控制台程序的标准输出

本文介绍了一个C++程序如何通过创建管道来捕获并显示另一个控制台程序的标准输出。示例中使用了hello.exe作为被调用的程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用管道获取控制台程序的标准输出


标签: c++管道
1000人阅读 评论(0) 收藏 举报
本文章已收录于:


分类:


1.该程序调用控制台程序hello.exe,通过管道获取到hello.exe的标准输出数据,并打印到当前程序的标准输出。

  1. #include <Windows.h>  
  2. #include <iostream>  
  3. #include <string>  
  4.   
  5. using namespace std;  
  6.   
  7. void invoke(string exe);  
  8.   
  9. int main(int argc, char* argv[])  
  10. {  
  11.     string exe = "hello.exe";  
  12.     invoke(exe);  
  13.     return 0;  
  14. }  
  15.   
  16.   
  17. void invoke(string exe)  
  18. {  
  19.     SECURITY_ATTRIBUTES saPipe;   
  20.         saPipe.nLength = sizeof(SECURITY_ATTRIBUTES);  
  21.         saPipe.lpSecurityDescriptor = NULL;  
  22.         saPipe.bInheritHandle = TRUE;  
  23.       
  24.     HANDLE hReadPipe, hWritePipe;  
  25.     BOOL bSuccess = CreatePipe(&hReadPipe,   
  26.         &hWritePipe,   
  27.         &saPipe,   
  28.         0);    
  29.     if(!bSuccess)   
  30.         return ;  
  31.       
  32.     PROCESS_INFORMATION pi;   
  33.     STARTUPINFO si;   
  34.         memset(&si,0,sizeof(si));   
  35.         si.hStdInput=hReadPipe;  
  36.         si.hStdOutput=hWritePipe;  
  37.         si.dwFlags=STARTF_USESTDHANDLES;  
  38.         si.cb=sizeof(si);   
  39.       
  40.     if(CreateProcess(NULL,(char*)exe.c_str(),NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))  
  41.     {  
  42.         CloseHandle(pi.hThread);      
  43.         const int max = 500;  
  44.         char buf[max] = {0};  
  45.         DWORD dw;  
  46.           
  47.         if(ReadFile(hReadPipe,buf,max-1,&dw,NULL))  
  48.         {  
  49.             cout<<buf<<endl;  
  50. //          ZeroMemory(buf,max);  
  51.         }  
  52.           
  53.         CloseHandle(pi.hProcess);     
  54.     }  
  55.       
  56.     CloseHandle(hReadPipe);  
  57.     CloseHandle(hWritePipe);  
  58. }  
#include <Windows.h>
#include <iostream>
#include <string>

using namespace std;

void invoke(string exe);

int main(int argc, char* argv[])
{
	string exe = "hello.exe";
	invoke(exe);
	return 0;
}


void invoke(string exe)
{
	SECURITY_ATTRIBUTES saPipe; 
        saPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
        saPipe.lpSecurityDescriptor = NULL;
        saPipe.bInheritHandle = TRUE;
	
	HANDLE hReadPipe, hWritePipe;
	BOOL bSuccess = CreatePipe(&hReadPipe, 
		&hWritePipe, 
		&saPipe, 
		0);  
	if(!bSuccess) 
		return ;
	
	PROCESS_INFORMATION pi; 
	STARTUPINFO si; 
        memset(&si,0,sizeof(si)); 
        si.hStdInput=hReadPipe;
        si.hStdOutput=hWritePipe;
        si.dwFlags=STARTF_USESTDHANDLES;
        si.cb=sizeof(si); 
	
	if(CreateProcess(NULL,(char*)exe.c_str(),NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))
	{
		CloseHandle(pi.hThread);   	
		const int max = 500;
		char buf[max] = {0};
		DWORD dw;
		
		if(ReadFile(hReadPipe,buf,max-1,&dw,NULL))
		{
			cout<<buf<<endl;
//			ZeroMemory(buf,max);
		}
		
		CloseHandle(pi.hProcess);   
	}
	
	CloseHandle(hReadPipe);
	CloseHandle(hWritePipe);
}
输出结果为:


2. hello.exe的主代码

  1. #include <stdio.h>  
  2.   
  3. void main()  
  4. {  
  5.     printf("hello!\n");  
  6. }  
#include <stdio.h>

void main()
{
	printf("hello!\n");
}



0
0
 
 




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值