Why close the handle to a pipe after writing?

This is a discussion on Why close the handle to a pipe after writing? within the Windows Programming forums, part of the Platform Specific Boards category; I was wondering in this bit of code: Code: void Pconnect::WriteToPipe(string s) { DWORD dwWritten; CHAR *chBuf; s += "\r\n"; ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    89

    Why close the handle to a pipe after writing?

    I was wondering in this bit of code:

    Code:
    void Pconnect::WriteToPipe(string s) 
    { 
       DWORD dwWritten; 
       CHAR *chBuf; 
    	s += "\r\n";
     
    // Read from a file and write its contents to a pipe. 
    
          if (! WriteFile(hChildStdinWr, s.c_str(), s.size(), 
             &dwWritten, NULL))
    		 std::cerr << "Write to pipe failed.\n";
    // Close the pipe handle so the child process stops reading. 
    
    
       if (! CloseHandle(hChildStdinWr)) 
    	  std::cerr << "Close pipe failed.\n";
    }
    Why is it necessary to close the handle after writing? My child process is a Telnet program and it reads the handle closing as an 'End of File' file command which it sends to the server. The server then stops listening and I can't write anymore. I fixed it by commenting out the last if statement for closing the pipe and it seems to work fine and the pipe still gets closed when my program ends. So is there any reason I should avoid doing this? If so do you have any ideas how?
    thanks

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Perhaps that was intended to be nested, so that it would close if the write failed? I don't think this is a correct way to handle such an error, but it's possible.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  2. a simple C question...
    By DramaKing in forum C Programming
    Replies: 10
    Last Post: 07-28-2002, 02:04 PM
  3. API Reading Files, handle is always -1
    By Xei in forum C++ Programming
    Replies: 13
    Last Post: 05-06-2002, 10:16 PM
  4. closing handle
    By swordfish in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2001, 06:47 AM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM

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