Windows pipes: Write from C - read in Python
<p>I'd like to transmit a few bytes of data though a pipe to plot it from python.</p>

<p>I started with some snippets I found here but I cant get them working.</p>

<p>I've created the pipe like this:</p>

<pre><code>int main(void){

HANDLE hPipe;
char buffer[24];
DWORD dwRead;


hPipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\Pipe"),
                        PIPE_ACCESS_DUPLEX | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,   // FILE_FLAG_FIRST_PIPE_INSTANCE is not needed but forces CreateNamedPipe(..) to fail if the pipe already exists...
                        PIPE_WAIT,
                        1,
                        24 * 16,
                        24 * 16,
                        NMPWAIT_USE_DEFAULT_WAIT,
                        NULL);


while (hPipe != INVALID_HANDLE_VALUE)
{
    if (ConnectNamedPipe(hPipe, NULL) != FALSE)   // wait for someone to connect to the pipe
    {
        while (ReadFile(hPipe, buffer, sizeof(buffer) - 1, &amp;dwRead, NULL) != FALSE)
        {
            /* add terminating zero */
            buffer[dwRead] = '\0';

            /* do something with data in buffer */
            printf("%s", buffer);
        }
    }

    DisconnectNamedPipe(hPipe);
}

return 0;}
</code></pre>

<p>If I execute the following code it writes but the read part blocks:</p>

<pre><code>  import time
  import struct

  f = open(r'\\.\\pipe\\Pipe', 'r+b', 0)
  i = 1
  sss='ccccc'
  while True:

   s = sss.format(i)
   i += 1

   f.write(struct.pack('I', len(s)) + s)   # Write str length and str
   f.seek(0)                               # EDIT: This is also necessary
   print 'Wrote:', s

   n = struct.unpack('I', f.read(4))[0]    # Read str length
   s = f.read(n)                           # Read str
   f.seek(0)                               # Important!!!
   print 'Read:', s
   time.sleep(2)
</code></pre>

<p>I tried commenting the ReadFile part in the C code but It did not work. Is there any other way to achieve this? I want to write from C and read from python. I tried writing into the pipe with CreateFile (from C) and it worked as expected. I only need the read part with python.</p>