-
Pipes! HELPPPPPP!!!!!!
I am trying to read gdb's (v 5.1) output. The problem is that ReadFile hangs. If i am debugging in Visual C++ ,then in the first iteration of the while loop the contents are read fully in the buffer but it hangs in the 2nd iteration when it should return 0. That is still ok, cause i can check if the buffer is full or not and then decide whether to continue with iterations, BUT -
What is even more strange is that when i am running the program normally w/o debug mode (though the exe is still the debug one) then it doesnt read the full buffer at once. Say if the text to be read is 102 chars , it first reads 40 and then 62. Now that beomes very difficult as i dont know how many times to run the loop. Can anyone help me pls!
P.S.- I dont have any problems performing write operations.
Code:
HANDLE hIWrite,hIRead,hIWriteTmp,hOWrite,hORead,hOReadTmp,hErrWrite;
SECURITY_ATTRIBUTES sa;
sa.nLength =sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle=true;
sa.lpSecurityDescriptor=NULL;
// Create the child output pipe
CreatePipe(&hOReadTmp,&hOWrite,&sa,NULL);
DuplicateHandle(GetCurrentProcess(), hOWrite,GetCurrentProcess(), &hErrWrite, 0,
TRUE, DUPLICATE_SAME_ACCESS);
// Create the child input pipe
CreatePipe(&hIRead,&hIWriteTmp,&sa,NULL);
DuplicateHandle(GetCurrentProcess(), hOReadTmp,GetCurrentProcess(), &hORead, 0,
FALSE, DUPLICATE_SAME_ACCESS);
DuplicateHandle(GetCurrentProcess(), hIWriteTmp,GetCurrentProcess(), &hIWrite, 0,
FALSE, DUPLICATE_SAME_ACCESS);
CloseHandle(hOReadTmp);
CloseHandle(hIWriteTmp);
STARTUPINFO si;
memset(&si,0,sizeof(STARTUPINFO));
si.cb=sizeof(STARTUPINFO);
si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.hStdOutput=hOWrite;
si.hStdInput=hIRead;
si.hStdError=hErrWrite;
si.wShowWindow=SW_HIDE;
char app[] ="gdb --annotate=2 -silent";
PROCESS_INFORMATION pi={0};
CreateProcess(NULL,&app[0],NULL,NULL
,true,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
HANDLE hPid=pi.hProcess;
CloseHandle(pi.hThread);
CloseHandle(hOWrite);
CloseHandle(hIRead);
CloseHandle(hErrWrite);
char buf[1024]={0};
DWORD cbRead=0,cbWrite=0;
char *b="help\n";
WriteFile(hIWrite,b,strlen(b),&cbWrite,NULL);
while(1)
{
RtlZeroMemory(&buf[0],1024);SetLastError(0);
if(!ReadFile(hORead,&buf,1023,&cbRead,NULL)||cbRead==0)
break;
SendMessage(m_hWndEdit,EM_SETSEL,-1,0);
SendMessage(m_hWndEdit,EM_REPLACESEL,FALSE,(LPARAM)(LPCTSTR)&buf[0]);
}
-
Like Hellooo!!!!
Can anyone help me pls???
So many views but still no replies?
-
the pipe will not return an EOF unless it is explicitly closed.
-fugi
-
but i have in many examples it returning with an error, usually - ERROR_BROKEN_PIPE.
Also going by your answer how will i know when my output has finished?
-
Also one more thing, if i start another thread to read the output and pass hOread as the arg then readfile returns with error "invalid access to memory region"