views:

60

answers:

2
    hPipe = CreateNamedPipe( 
          lpszPipename,             // pipe name 
          PIPE_ACCESS_DUPLEX,       // read/write access 
          PIPE_TYPE_MESSAGE |       // message type pipe 
          PIPE_READMODE_MESSAGE |   // message-read mode 
          PIPE_WAIT,                // blocking mode 
          PIPE_UNLIMITED_INSTANCES, // max. instances  
          BUFSIZE,                  // output buffer size 
          BUFSIZE,                  // input buffer size 
          0,       

I have two questions about this:

  1. what if the above code is run twice,how many pipes will get created,1 or 2?
  2. if 2,suppose one of the pipe get connected by A,then B tries to connect lpszPipename, is it guaranteed that B will connect to the one that no one has connected?
A: 

The second call to CreateNamedPipe with the same name fails, if FILE_FLAG_FIRST_PIPE_INSTANCE flag is used, or connects to the same pipe, if this flag is not used. In the case the second CreateNamedPipe call succeeds, it returns another handle to the same kernel object.

Alex Farber
A: 

In the fourth parameter of CreateNamedPipe function you can limit how many named pipe instances can be created. If you set it to PIPE_UNLIMITED_INSTANCE as in your example and call the CreateNamedPipe function twice with the same parameters, two instances will be created (they'll share the same pipe name) and two clients will be able to connect to your named pipe server (each of them to one named pipe instance).

For more information look at http://msdn.microsoft.com/en-us/library/aa365594%28v=VS.85%29.aspx

Ondra C.