获取控制台程序(Console)的输出内容

VC++ 专栏收录该内容
134 篇文章 1 订阅
int GetConsoleExeOutPutCreatePipeAndCreateProcess( char * pstrExename_pstrCmdLine )
{
HANDLE hRead1, hWrite1;   //读句柄,写句柄
HANDLE hRead2, hWrite2;   //读句柄,写句柄
HANDLE hCmd;


int ret;


SECURITY_ATTRIBUTES sa;
sa.nLength              = 12;
sa.lpSecurityDescriptor = 0;
sa.bInheritHandle       = true;


ret = CreatePipe( &hRead1, &hWrite1, &sa, 0 );

if ( ret == 0 )
{
return -1;
}

ret = CreatePipe( &hRead2, &hWrite2, &sa, 0 );

if ( ret == 0 )
{
return -1;
}

//创建进程且重定向标准输入输出

//将其标准输入设置为hReadPipe2
//将其标准输出和错误输出设置为hWritePipe1
STARTUPINFOA si;
ZeroMemory(&si,sizeof(si));
si.dwFlags     = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.wShowWindow = SW_SHOW;
si.hStdInput   = hRead2;
si.hStdOutput  = si.hStdError = hWrite1;
char cmdLine[] = "L:\\ffmpeg.exe -i L:\\t.mp4 L:\\t.avi" ;

PROCESS_INFORMATION ProcessInformation;
ret=CreateProcessA( NULL,  cmdLine, NULL, NULL, 1, 0, NULL, NULL, &si, &ProcessInformation );
if (ret == 0)
{
return -1;
}

//
DWORD lBytesRead;
char buffer[1024] = {0};
bool bCovBegin = false;
while(true)
{
memset(buffer,0,1024);
ret=ReadFile(hRead1,buffer,1023,&lBytesRead,0);
if (ret == 0)
{
break;
}
else
{
AfxMessageBox( CString(buffer) );
}
}


//
CloseHandle(hRead1);
CloseHandle(hWrite1);
CloseHandle(hWrite2);
CloseHandle(hRead2);

// Close process and thread handles. 
CloseHandle( ProcessInformation.hProcess );
CloseHandle( ProcessInformation.hThread );


}



微软的一篇文章:https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx


用Java实现,更简单;

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值