vc中获得输出别的exe中打印到STDOUT的输出到控件中

void CConsoleTestDlg::ExecuteUserCommand(LPCTSTR lpszCmd)
{
	SECURITY_ATTRIBUTES sa; 
	HANDLE hRead,hWrite; 
	CString strCmdInfo; 
	char buffer[10240] = {0}; 
	DWORD bytesRead = 0; 
	DWORD dwRead = 0 ; 
	char szCmd[256]; 
	int nLen = 0; 
	strcpy(szCmd, "cmd.exe /a /s /c "); 
	strcat(szCmd,lpszCmd); 
	strCmdInfo.Format( ":> %s\r\n ",lpszCmd); 
	m_strCmdInfo += strCmdInfo;
	
	
	strCmdInfo.Empty(); 
	
	sa.nLength = sizeof(SECURITY_ATTRIBUTES); 
	sa.lpSecurityDescriptor = NULL; 
	sa.bInheritHandle = TRUE; 
	if (!CreatePipe(&hRead,&hWrite,&sa,0)) 
	{ 
		strCmdInfo = "\n创建通道失败... "; 
		m_strCmdInfo += strCmdInfo; 
		goto END_EXEC; 
	} 
	
	STARTUPINFO si; 
	PROCESS_INFORMATION pi; 
	si.cb = sizeof(STARTUPINFO); 
	GetStartupInfo(&si); 
	si.hStdError = hWrite; 
	si.hStdOutput = hWrite; 
	si.wShowWindow = SW_HIDE; 
	si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; 
	if (!CreateProcess(NULL,szCmd,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)) 
	{ 
		strCmdInfo.Format( "执行程序 %s 失败\r\n ",lpszCmd); 
		m_strCmdInfo += strCmdInfo; 
		goto END_EXEC; 
	} 
	CloseHandle(hWrite); 
	
	while (1) 
	{ 
		if( dwRead > 10239 ) 
			break; 
		Sleep(200); 
		if (ReadFile(hRead,&buffer[dwRead],10239-dwRead,&bytesRead,NULL) == NULL) 
			break; 
		dwRead += bytesRead; 
		bytesRead = 0 ; 
		strCmdInfo += buffer; 
		m_strCmdInfo += strCmdInfo; 
		m_ctrlCmdInfo.SetWindowText(m_strCmdInfo); 
		nLen = m_strCmdInfo.GetLength(); 
		m_ctrlCmdInfo.SetSel(nLen,nLen); 
	} 
	CloseHandle(hRead); 
	
END_EXEC: 
	m_ctrlCmdInfo.SetWindowText(m_strCmdInfo); 
	nLen = m_strCmdInfo.GetLength(); 
	m_ctrlCmdInfo.SetSel(nLen,nLen); 
	
	//m_strCmd.Empty(); 
	//SetDlgItemText(IDC_COMMAND,m_strCmd); 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值