title: C++ and C# Communication using Named Pipe
body: <p>I'm trying to reverse enginering a dll injected into a process, that does hook winsock <code>send()</code> and send data over a <code>PipeStream</code>.</p>
  
  <p>This is the C# code that read the pipe stream:</p>
  
  <pre><code>    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
      public struct PipeHeader
      {
          [MarshalAs(UnmanagedType.I1)]
          public byte command;
          [MarshalAs(UnmanagedType.I4)]
          public int sockid;
          public int datasize;
      }
  
      public static object RawDeserializeEx(byte[] rawdatas, Type anytype)
      {
          int rawsize = Marshal.SizeOf(anytype);
          if (rawsize &gt; rawdatas.Length)
              return null;
          GCHandle handle = GCHandle.Alloc(rawdatas, GCHandleType.Pinned);
          IntPtr buffer = handle.AddrOfPinnedObject();
          object retobj = Marshal.PtrToStructure(buffer, anytype);
          handle.Free();
          return retobj;
      }
      private void PipeRead()
      {
          byte[] dbPipeMsgIn = new byte[9];
          byte[] zero = new byte[] { 0 };
  
          byte[] dbPipeMsgInData;
      PipeLoop:
          while (pipeIn.Read(dbPipeMsgIn, 0, 9) != 0)
          {
              strPipeMsgIn = (PipeHeader)RawDeserializeEx(dbPipeMsgIn, typeof(PipeHeader));
              if (strPipeMsgIn.datasize != 0)
              {
                  dbPipeMsgInData = new byte[strPipeMsgIn.datasize];
                  pipeIn.Read(dbPipeMsgInData, 0, dbPipeMsgInData.Length);
                  //do something with dbPipeMsgInData
              }
          }
          if (pipeIn.IsConnected) goto PipeLoop;
      }
  </code></pre>
  
  <p>So far i have hooked the <code>send()</code> func, connect and send messages trough the pipe.
  The problem is that the data received is not the data i expect, so probably I do sent in a wrong way. I need help because i have very little to no C++ knowledge.</p>
  
  <p>C++ code:</p>
  
  <pre><code>#pragma pack(1)
  typedef struct
  {
      byte command;
      int sockid;
      int datasize;
  } PipeHeader;
  #pragma pack(0)
  
  int WINAPI MySend(SOCKET s, const char* buf, int len, int flags)
  {
      PipeHeader ph;
      string p(buf);
      if(p.find("&lt;TalkMsg") == 0){
          byte cmd = 0;
          ph.command = cmd;
          ph.sockid = s;
          ph.datasize = len;
          char buffer[sizeof(ph)];
          memcpy(buffer, &amp;ph, sizeof(ph)); 
          if(SendPipeMessage(buffer, sizeof(buffer))){
              if(SendPipeMessage(buf, sizeof(buf))){
                  MessageBox(NULL,"Message Sent", NULL, NULL);
              }
          }
          fopen_s(&amp;pSendLogFile, "C:\\SendLog.txt", "a+");
          fprintf(pSendLogFile, "%s\n", buf);
          fclose(pSendLogFile);
      }
      return pSend(s, buf, len, flags);
  }
  
  BOOL SendPipeMessage(LPCVOID lpvMessage, DWORD ctToWrite){
      // Send a message to the pipe server. 
     cbToWrite = sizeof(lpvMessage);
  
     fSuccess = WriteFile( 
        hPipe,                  // pipe handle 
        lpvMessage,             // message 
        cbToWrite,              // message length 
        &amp;cbWritten,             // bytes written 
        NULL);                  // not overlapped 
  
     if ( ! fSuccess) 
     {
        return false;
     }else return true;
  }
  </code></pre>
  
  <p><strong>Edit, more info:</strong></p>
  
  <p>The goal is to send a message containing a 9 Bytes long <code>PipeHeader</code> Struct over the pipe, then send another message containing the winsock <code>send()</code> data, (the <code>buf</code> variable), read the pipe on the C# application and parse the first message to get the DataSize of the next incoming message (that's the <code>datasize</code> var of <code>PipeHeader</code>), then, using the <code>datasize</code> read again the pipe to get the send() buffer.
  I think that's working in that way. I do not know very well how Pipes work.</p>
  
  <p>Anyway the MAIN goal is to send the <code>send()</code> buffer from the C++ Dll into the C# application.</p>
  
  <p><strong>Update:</strong>
  Seems that i have to first serialize the <code>PipeHeader</code> struct in a way so i can Deserialize it using <code>RawDeserializeEx()</code> in the C# code.
  I tried by doing: </p>
  
  <pre><code>char buffer[sizeof(ph)];
  memcpy(buffer, &amp;ph, sizeof(ph)); 
  </code></pre>
  
  <p>The problem is that, in C++ <code>sizeof(ph)</code>, or <code>sizeof(buffer)</code> return 12 bytes.
  Instead in C# the unmanaged size (<code>Marshal.SizeOf()</code>) of the same Struct, return 9 bytes.</p>
  
  <p><strong>Update 2:</strong>
  Solved the size differences by changing the struct packing.
  But I'm still not getting the right values in C#.
  Code Updated.</p>