title: Writing Binary Data to Named Pipes in Python
body: <p>Several months ago, I implemented a named pipe server in a GUI using the example code from MSDN -> <a href="https://msdn.microsoft.com/en-us/library/bb546085" rel="nofollow">https://msdn.microsoft.com/en-us/library/bb546085</a>.</p>
  
  <p>The named pipe server seems to work well, and I've used it to control my GUI with other programs written in various Visual Studio languages.</p>
  
  <p>Today I have been trying to interact with the named pipe server with a Python script.  On the web, it was simple to find sample python code, using the ctypes library, that opened up the named pipe with the windll.kernel32.CreateFileA() method and wrote ASCII bytes to it with the windll.kernel32.WriteFile() method.</p>
  
  <p>The problem is that the MSDN example uses a format where the first two bytes written to/received from the pipe represent the size of a string that is to follow in the pipe.  While I can write strings to the pipe from Python with no problem, I can't seem to pass any binary data into the WriteFile() method.</p>
  
  <p>Questions about ways to solve this problem:
    1. Is there any way to write raw bytes of binary data to WriteFile()?  Is there another method I could be calling to handle this?
    2. Is there any way for me to encode the binary number into a string as raw bytes?  For example, if I knew the bytes I wanted to send ahead of time (0x1234 for example) then I could just send create_string_buffer("\x12\x34").  The bytes have to be determined at runtime, however, and I haven't found a way to create a string out of the raw bytes.</p>