Pipes! HELPPPPPP!!!!!!

This is a discussion on Pipes! HELPPPPPP!!!!!! within the Windows Programming forums, part of the Platform Specific Boards category; I am trying to read gdb's (v 5.1) output. The problem is that ReadFile hangs. If i am debugging in ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    46

    Unhappy 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]);
    }
    Last edited by MovingFulcrum; 05-23-2002 at 02:26 AM.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    46

    Unhappy

    Like Hellooo!!!!
    Can anyone help me pls???
    So many views but still no replies?

  3. #3
    fugi
    Guest
    the pipe will not return an EOF unless it is explicitly closed.

    -fugi

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    46
    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?

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    46
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipes sometimes fail
    By EVOEx in forum Linux Programming
    Replies: 2
    Last Post: 05-02-2009, 02:47 PM
  2. recieving pipes from bash
    By Drogin in forum Linux Programming
    Replies: 6
    Last Post: 04-12-2009, 05:09 PM
  3. Pipes
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 03-31-2009, 04:09 AM
  4. pipes in c
    By ajal1 in forum C Programming
    Replies: 6
    Last Post: 10-29-2005, 04:29 PM
  5. Services and Pipes
    By nickname_changed in forum Windows Programming
    Replies: 0
    Last Post: 07-16-2003, 07:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21