CString str; assert(hReadPipe); DWORD BytesRead; char dest[100]; bool RdLoopDone = false; while (!RdLoopDone) { static int a=0; memset(dest, 0, 100); assert(ReadFile(hReadPipe, &dest, sizeof(dest), &BytesRead, NULL)); str+=dest; if (BytesRead < 100) RdLoopDone = true; if (a > 20) RdLoopDone = true; a++;//this while loop seems infinit if without any control. } CloseHandle(hReadPipe); CloseHandle(hWritePipe); CloseHandle(ProcInfo.hProcess); AfxMessageBox(str);//a big messagebox with long text appear!! return TRUE;
http://www.sqqt.cn/article.asp?id=6488 这里有个例子可以看看怎样重定向
HANDLE hReadPipe,hWritePipe;
CString cmd="command";
SECURITY_ATTRIBUTES sa;
SECURITY_DESCRIPTOR sd;
LPSECURITY_ATTRIBUTES lpsa = NULL;
InitializeSecurityDescriptor(&sd,SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&sd,TRUE,NULL,TRUE);
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle=TRUE;
sa.lpSecurityDescriptor=&sd;
lpsa = &sa;
cmd="cmd";
sa.nLength=sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle=TRUE;
if(!CreatePipe(&hReadPipe,&hWritePipe,&sa,0))
return FALSE;
STARTUPINFO si;
PROCESS_INFORMATION ProcInfo;
ZeroMemory(&si,sizeof(si));
si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.wShowWindow=SW_HIDE;
si.hStdInput=hReadPipe;
si.hStdOutput=si.hStdError=hWritePipe;
char* windir=getenv("windir");
if(!CreateProcess(NULL,cmd.LockBuffer(),NULL,NULL,1,CREATE_NEW_CONSOLE,NULL,
windir,&si,&ProcInfo))
return FALSE;
WaitForSingleObject(ProcInfo.hProcess,3000);
CString str;
assert(hReadPipe);
DWORD BytesRead;
char dest[100];
bool RdLoopDone = false;
while (!RdLoopDone) {
static int a=0;
memset(dest, 0, 100);
assert(ReadFile(hReadPipe, &dest, sizeof(dest), &BytesRead, NULL));
str+=dest;
if (BytesRead < 100) RdLoopDone = true;
if (a > 20) RdLoopDone = true;
a++;//this while loop seems infinit if without any control.
}
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
CloseHandle(ProcInfo.hProcess);
AfxMessageBox(str);//a big messagebox with long text appear!!
return TRUE;