title: Getting "Error when communicating with the native messaging host." when sending message from native host to browser extension (Windows)
body: <p>I'm getting an "Inside onDisconnected(): Error when communicating with the native messaging host." when sending a message from my native host app to the browser extension.</p>
  
  <p>There are several things that can cause this:</p>
  
  <p>1) Incorrect length sent at beginning of message, or improper order of length bytes sent (endianness).</p>
  
  <p>2) Not putting stdout in binary mode before writing to it (if stdout is in text mode extra bytes can be added apart from your code).</p>
  
  <p>3) Not sending the message as valid json in UTF-8. Actually I'm not certain that invalid json would be rejected, but the docs say the message should be json.</p>
  
  <p>The code is:</p>
  
  <pre><code>int nStdOutDescriptor = _fileno(stdout);
  int result = _setmode( nStdOutDescriptor, _O_BINARY );
  
  if( result == -1 )
  {
      OutputDebugStringA ("Failed attempting to set stdout to binary mode\r\n");
      return;
  }
  
  HANDLE hStdOut = (HANDLE) _get_osfhandle(nStdOutDescriptor);
  
  if (INVALID_HANDLE_VALUE != hStdOut)
  {
      char *results = "{\"results\":\"0000\"}";
      std::string sMsg(results);
  
      int nMsgLen = sMsg.length();  
  
      unsigned char first = (unsigned char)(nMsgLen &amp; 0x000000ff);
      unsigned char second = (unsigned char)((nMsgLen &gt;&gt; 8) &amp; 0x000000ff);
      unsigned char third = (unsigned char)((nMsgLen &gt;&gt; 16) &amp; 0x000000ff);
      unsigned char fourth = (unsigned char)((nMsgLen &gt;&gt; 24) &amp; 0x000000ff);
  
      char *bufMsg = new char[4 + nMsgLen]; // allocate message buffer
      const char *pMessageBytes = sMsg.c_str();
      memcpy( &amp;bufMsg[4], &amp;pMessageBytes[0], nMsgLen);
      bufMsg[0] = first;
      bufMsg[1] = second;
      bufMsg[2] = third;
      bufMsg[3] = fourth;
  
      DWORD dwNumBytesToWrite = nMsgLen + 4;
      DWORD dwNumBytesWritten;
      if (TRUE == WriteFile(hStdOut, (LPVOID)pMessageBytes, dwNumBytesToWrite, &amp;dwNumBytesWritten, NULL))
      {
          BTrace (L"WriteFile() succeeded. Returned TRUE. %lu bytes written", dwNumBytesWritten );
      }
  
      _close(nStdOutDescriptor);
  }
  </code></pre>
  
  <p>All three of the likely causes do not seem to be occurring.  But I cannot discover any detailed info (even from looking at the source that Google provides) about what is causing my particular error message.  WriteFile() succeeds and the number of bytes written is 22 bytes.  There are 22 total bytes written, 4 of which are the length bytes.  I've verified that the first 4 bytes are (in decimal, not hex): 18,0,0,0 which in little-endian fashion indicates how many bytes follow to make up the json message.  When I use DebugView to peek at my json message it is always:  {"results":"0000"}.  That's 18 bytes/chars.  I've even experimented with sending escaped double-quotes in case that is preferred.  In the browser extension's background page my onDisconnected() event is called which reports the last chrome runtime error message (that's where I'm getting the error msg defining this question). This means the connection between the extension and the native host app is being shut down. Help would be greatly appreciated.</p>