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);
}
vc中获得输出别的exe中打印到STDOUT的输出到控件中
最新推荐文章于 2021-05-23 13:14:42 发布