重定向接管DOS控制台: SECURITY_ATTRIBUTES lsa;
STARTUPINFO si;
PROCESS_INFORMATION pi, *lppi;
HANDLE hReadPipe, hWritePipe;

lsa.nLength = sizeof(SECURITY_ATTRIBUTES);
lsa.lpSecurityDescriptor = NULL;
lsa.bInheritHandle = TRUE; lppi = π
CWaitCursor waitCursor; if(!CreatePipe(&hReadPipe, &hWritePipe, &lsa, 0))
{
AfxMessageBox("Error");
return;
} memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
si.hStdOutput = hWritePipe; if(!CreateProcess(NULL, m_strFileName.GetBuffer(m_strFileName.GetLength()), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
{
AfxMessageBox("Error");
return;
} DWORD cchReadBuffer;
CString strText;
TCHAR ph[5000];
m_strOutput.Empty(); for(;;)
{
cchReadBuffer = 0;
if(!PeekNamedPipe(hReadPipe, ph, 1, &cchReadBuffer, NULL, NULL))
break; if(cchReadBuffer)
{
if(!ReadFile(hReadPipe, ph, 4096, &cchReadBuffer, NULL))
break;
ph[cchReadBuffer] = 0;
m_strOutput += ph;;
}
else
if(WaitForSingleObject(pi.hProcess, 0) == WAIT_OBJECT_0)
break;
Sleep(500);
} ph[cchReadBuffer] = 0;
m_strOutput += ph; CloseHandle(hReadPipe);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(hWritePipe);