Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows API
  4. Reading from a Pipe

Reading from a Pipe

Scheduled Pinned Locked Moved Windows API
helpc++securityquestion
2 Posts 2 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrDooDoo
    wrote on last edited by
    #1

    Hi friends, I am developing a program for windows 7 with C++ and I am using VS2010. I create a a pair of pipes for redirecting output of Cmd.exe to my program like:

    void CpipesDlg::OnBnClickedBtnredirstd()
    {
    char Cmd[]="dir *.*\r\n";
    char Buff[129];
    CString str;
    HANDLE hStdIn_Read, hStdIn_Write;
    HANDLE hStdOut_Read, hStdOut_Write;
    SECURITY_ATTRIBUTES sAttr;

    STARTUPINFOA StartInf;
    PROCESS\_INFORMATION procInf;
    
    DWORD dwBytesToWrite,dwBytesReadFrom;
    
    sAttr.nLength = sizeof(sAttr);
    sAttr.bInheritHandle = TRUE;
    sAttr.lpSecurityDescriptor = NULL;
    
    CreatePipe(&hStdIn\_Read,&hStdIn\_Write,&sAttr,0);
    CreatePipe(&hStdOut\_Read,&hStdOut\_Write,&sAttr,0);
    
    //SetHandleInformation(hStdIn\_Read, HANDLE\_FLAG\_INHERIT, 0);
    //SetHandleInformation(hStdIn\_Write, HANDLE\_FLAG\_INHERIT, 0);
    
    memset(&StartInf,0, sizeof(StartInf));
    memset(&procInf,0,sizeof(procInf));
    
    StartInf.cb = sizeof(StartInf);
    StartInf.dwFlags = STARTF\_USESTDHANDLES;
    StartInf.hStdError = hStdOut\_Write;
    StartInf.hStdOutput = hStdOut\_Write;
    StartInf.hStdInput = hStdIn\_Read;
    
    WriteFile(hStdIn\_Write,Cmd,sizeof(Cmd),&dwBytesToWrite,NULL);
    
    if(!CreateProcessA(NULL,"cmd.exe",NULL,NULL,TRUE,NORMAL\_PRIORITY\_CLASS | CREATE\_NO\_WINDOW ,NULL,NULL,&StartInf,&procInf))
    {
    	MessageBoxA(m\_hWnd, "Can't Create Process","Error",MB\_OK | MB\_ICONERROR);
    }
    	WriteFile(hStdIn\_Write,Cmd,sizeof(Cmd),&dwBytesToWrite,NULL);
    
    BOOL bSUCCESS =TRUE;
    Sleep(100);
    while(bSUCCESS)
    {
    
    	BOOL bResult  = ReadFile(hStdOut\_Read,Buff,70,&dwBytesReadFrom,NULL);
    	if(!bResult)
    	{
    		break;
    	}
    	Buff\[dwBytesReadFrom\]=0;
    	str+= Buff;
    	bSUCCESS = dwBytesReadFrom!=0;
    }
    m\_Disp = str;
    UpdateData(FALSE);
    CloseHandle(hStdIn\_Read);
    CloseHandle(hStdIn\_Write);
    CloseHandle(hStdOut\_Read);
    CloseHandle(hStdOut\_Write);
    

    }

    and pipe works but there is a problem when I try to read from pipe using ReadFile and the problem is that I don't know how many bytes I should read. The above code blocks when it gets last ReadFile call and there is nothing to read. Actually a deadlock is made. Do you have any Idea to solve the problem? Do you know how I can get the number of ready bytes for reading? Or can I change the behavior of ReadFile for returning immediately? I was wondering if you could help me! I am looking forward to hearing from you. :laugh:

    W 1 Reply Last reply
    0
    • M MrDooDoo

      Hi friends, I am developing a program for windows 7 with C++ and I am using VS2010. I create a a pair of pipes for redirecting output of Cmd.exe to my program like:

      void CpipesDlg::OnBnClickedBtnredirstd()
      {
      char Cmd[]="dir *.*\r\n";
      char Buff[129];
      CString str;
      HANDLE hStdIn_Read, hStdIn_Write;
      HANDLE hStdOut_Read, hStdOut_Write;
      SECURITY_ATTRIBUTES sAttr;

      STARTUPINFOA StartInf;
      PROCESS\_INFORMATION procInf;
      
      DWORD dwBytesToWrite,dwBytesReadFrom;
      
      sAttr.nLength = sizeof(sAttr);
      sAttr.bInheritHandle = TRUE;
      sAttr.lpSecurityDescriptor = NULL;
      
      CreatePipe(&hStdIn\_Read,&hStdIn\_Write,&sAttr,0);
      CreatePipe(&hStdOut\_Read,&hStdOut\_Write,&sAttr,0);
      
      //SetHandleInformation(hStdIn\_Read, HANDLE\_FLAG\_INHERIT, 0);
      //SetHandleInformation(hStdIn\_Write, HANDLE\_FLAG\_INHERIT, 0);
      
      memset(&StartInf,0, sizeof(StartInf));
      memset(&procInf,0,sizeof(procInf));
      
      StartInf.cb = sizeof(StartInf);
      StartInf.dwFlags = STARTF\_USESTDHANDLES;
      StartInf.hStdError = hStdOut\_Write;
      StartInf.hStdOutput = hStdOut\_Write;
      StartInf.hStdInput = hStdIn\_Read;
      
      WriteFile(hStdIn\_Write,Cmd,sizeof(Cmd),&dwBytesToWrite,NULL);
      
      if(!CreateProcessA(NULL,"cmd.exe",NULL,NULL,TRUE,NORMAL\_PRIORITY\_CLASS | CREATE\_NO\_WINDOW ,NULL,NULL,&StartInf,&procInf))
      {
      	MessageBoxA(m\_hWnd, "Can't Create Process","Error",MB\_OK | MB\_ICONERROR);
      }
      	WriteFile(hStdIn\_Write,Cmd,sizeof(Cmd),&dwBytesToWrite,NULL);
      
      BOOL bSUCCESS =TRUE;
      Sleep(100);
      while(bSUCCESS)
      {
      
      	BOOL bResult  = ReadFile(hStdOut\_Read,Buff,70,&dwBytesReadFrom,NULL);
      	if(!bResult)
      	{
      		break;
      	}
      	Buff\[dwBytesReadFrom\]=0;
      	str+= Buff;
      	bSUCCESS = dwBytesReadFrom!=0;
      }
      m\_Disp = str;
      UpdateData(FALSE);
      CloseHandle(hStdIn\_Read);
      CloseHandle(hStdIn\_Write);
      CloseHandle(hStdOut\_Read);
      CloseHandle(hStdOut\_Write);
      

      }

      and pipe works but there is a problem when I try to read from pipe using ReadFile and the problem is that I don't know how many bytes I should read. The above code blocks when it gets last ReadFile call and there is nothing to read. Actually a deadlock is made. Do you have any Idea to solve the problem? Do you know how I can get the number of ready bytes for reading? Or can I change the behavior of ReadFile for returning immediately? I was wondering if you could help me! I am looking forward to hearing from you. :laugh:

      W Offline
      W Offline
      WuRunZhe
      wrote on last edited by
      #2

      Well, I've compiled your code and invoke it. Whenever I invoke it, it didn't stop but I find that your code work well when during debug. About your problem, I think there is no answer. Because c++ stopped reading when It meets zero during read char arrays. So your code is right, I think. In my opinion, you could control your reading bytes which is set 70. :)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups