title: multiple writefile and readfile in named-pipe
body: <p>I need to pass some string by pipe. we can't pass a pointer by pipe and we must pass data. for passing a string we can send an array of chars. but I don't want to use array. I need a way to send strings in variable size.</p>
  
  <p>I used msdn samples for creating pipe server and pipe client:</p>
  
  <p>but inestead of only one writeFile and readFile function in pipe client and pipe server I used them three time in this way: </p>
  
  <p>I used a structure for saving my strings size. first of all this structure is sent. then my two string will be sent. so in pipe server at first, the size of strings will be read and after that the two strings will be received.</p>
  
  <p>i defined a structure like this in both client and server programs:</p>
  
  <pre><code>typedef struct 
  {
      int fileNameLen;
      int commandArgLen;
  }pipeData,*PpipeData;
  
     pipeData dataToWrite;
     pipeData *pdataToWrite = &amp;dataToWrite;
  </code></pre>
  
  <p>in pipe client I want to send this strings:</p>
  
  <pre><code>   LPTSTR s1 = TEXT("file1");
     LPTSTR s2 = TEXT("startCmd");
  
     dataToWrite.commandArgLen = sizeof(s1);
     dataToWrite.fileNameLen = sizeof(s2);
  </code></pre>
  
  <p>I sent the structure by pipe client in this way. </p>
  
  <pre><code>   fSuccess = WriteFile( 
        hPipe,                  // pipe handle 
        pdataToWrite,             // message 
        sizeof(dataToWrite),              // message length 
        &amp;cbWritten,             // bytes written 
        NULL);                  // not overlapped 
  
     if ( ! fSuccess) 
     {
        _tprintf( TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError() ); 
        return -1;
     }
  
     fSuccess = WriteFile( 
        hPipe,                  // pipe handle 
        s1,             // message 
        sizeof(s1),     // message length 
        &amp;cbWritten,             // bytes written 
        NULL);                  // not overlapped 
  
     if ( ! fSuccess) 
     {
        _tprintf( TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError() ); 
        return -1;
     }
  
  
     fSuccess = WriteFile( 
        hPipe,                  // pipe handle 
        s2,             // message 
        sizeof(s2),     // message length 
        &amp;cbWritten,             // bytes written 
        NULL);                  // not overlapped 
  
     if ( ! fSuccess) 
     {
        _tprintf( TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError() ); 
        return -1;
     }
  </code></pre>
  
  <p>in pipe server for read the pipe i use 3 readFile like this:</p>
  
  <pre><code>      fSuccess = ReadFile( 
           hPipe,        // handle to pipe 
           pdataToWrite,    // buffer to receive data 
           sizeof(pdataToWrite), // size of buffer 
           &amp;cbBytesRead, // number of bytes read 
           NULL);        // not overlapped I/O 
  
        if (!fSuccess || cbBytesRead == 0)
        {   
            if (GetLastError() == ERROR_BROKEN_PIPE)
            {
                _tprintf(TEXT("InstanceThread: client disconnected.\n"), GetLastError());
                break;
            }
  
            else
            {
                _tprintf(TEXT("InstanceThread ReadFile failed, GLE=%d.\n"), GetLastError());
                break;
            }
  
        }
  
     // Process the incoming message.
        GetAnswerToRequest(TEXT("structure recieved"), pchReply, &amp;cbReplyBytes); 
  
  
  
        fSuccess = ReadFile( 
           hPipe,        // handle to pipe 
           s1,    // buffer to receive data 
           dataToWrite.commandArgLen, // size of buffer 
           &amp;cbBytesRead, // number of bytes read 
           NULL);        // not overlapped I/O 
  
        if (!fSuccess || cbBytesRead == 0)
        {   
            if (GetLastError() == ERROR_BROKEN_PIPE)
            {
                _tprintf(TEXT("InstanceThread: client disconnected.\n"), GetLastError()); 
            }
  
            else
            {
                _tprintf(TEXT("InstanceThread ReadFile failed, GLE=%d.\n"), GetLastError()); 
            }
            break;
        }    
        GetAnswerToRequest(s1, pchReply, &amp;cbReplyBytes); 
  
  
        fSuccess = ReadFile( 
           hPipe,        // handle to pipe 
           s2,    // buffer to receive data 
           dataToWrite.fileNameLen, // size of buffer 
           &amp;cbBytesRead, // number of bytes read 
           NULL);        // not overlapped I/O 
  
        if (!fSuccess || cbBytesRead == 0)
        {   
            if (GetLastError() == ERROR_BROKEN_PIPE)
            {
                _tprintf(TEXT("InstanceThread: client disconnected.\n"), GetLastError()); 
            }
  
            else
            {
                _tprintf(TEXT("InstanceThread ReadFile failed, GLE=%d.\n"), GetLastError()); 
            }
            break;
        }
  
  
  
        GetAnswerToRequest(s2, pchReply, &amp;cbReplyBytes); 
  </code></pre>
  
  <p>this way doesn't work properly. when pipe server read data in first readFile, it may return this error : ERROR_MORE_DATA (if I used PIPE_TYPE_MESSAGE in createNamedPipe)</p>
  
  <p>I don't know how can i use multiple writeFile and readFile in pipe client and server.</p>