title: Strange behaviour when sending messages from c++ to chromium extension [Native Messaging] - No data being sent
body: <p>Hi and hope everyone is doing good!</p>
  <p>I started working on a chrome extension which talks to a cpp exe. At the moment I am just trying to read some data from the extension which works as expected using the following code</p>
  <pre><code>unsigned int length = 0;
  
  {
      unsigned int read_char = getchar();
      length = length | (read_char &lt;&lt; index*8);
  }
  
  std::string message = &quot;&quot;;
  for (int index = 0; index &lt; length; index++)
  {
      message += getchar();
  }
  </code></pre>
  <p>When trying to send the same information from the cpp -&gt; extension using the following code</p>
  <pre><code>unsigned int len = message.length();
  
  
  printf(&quot;%c%c%c%c&quot;,  (char) (len &amp; 0xff),
                      (char) (len &lt;&lt; 8 &amp; 0xff),
                      (char) (len &lt;&lt; 16 &amp; 0xff),
                      (char) (len &lt;&lt; 24 &amp; 0xff));
  
  
  printf(&quot;%s&quot;, message.c_str());
  </code></pre>
  <p>it works as expected
  <a href="https://i.stack.imgur.com/JIjaN.png" rel="nofollow noreferrer">Screenshot of the working scenario</a></p>
  <p>However, when i try to send back another string rather than the same string that we send it won't work.
  Example:</p>
  <pre><code>    unsigned int length = 0;
  
  
  for (int index = 0; index &lt; 4; index++)
  {
      unsigned int read_char = getchar();
      cerr &lt;&lt; read_char &lt;&lt; endl;
      length = length | (read_char &lt;&lt; index*8);
  }
  
  std::string message = &quot;&quot;;
  for (int index = 0; index &lt; length; index++)
  {
      message += getchar();
  }
  
  std::string outputMessage = &quot;output&quot;;
  
  unsigned int len = outputMessage.length();
  
  
  printf(&quot;%c%c%c%c&quot;,  (char) (len &amp; 0xff),
                      (char) (len &lt;&lt; 8 &amp; 0xff),
                      (char) (len &lt;&lt; 16 &amp; 0xff),
                      (char) (len &lt;&lt; 24 &amp; 0xff));
  
  
  printf(&quot;%s&quot;, outputMessage.c_str());
  
  
  
  return 0;
  </code></pre>
  <p>Here is also the screenshot of the scenario that <strong>is failing.</strong> <a href="https://i.stack.imgur.com/myrvE.png" rel="nofollow noreferrer">Failed scenario</a></p>
  <hr />
  <p>I am not sure what I missed here, consider that I am just creating a new string and also using it's length for information that the extension needs.</p>
  <p>Anyway, it would be great if anyone could look at this one. Thanks in advance :)</p>
  <p>UPDATE:
  After trying different ways of returning messages I found this on the web and it works as expected</p>
  <pre><code>message_t encode_message(json content) {
      string encoded_content = content.dump();
      message_t m;
      m.content = encoded_content;
      m.length = (uint32_t)encoded_content.length();
      return m;
  }
  
  // Send an encoded message to stdout.
  void send_message(message_t encoded_message) {
      char* raw_length = reinterpret_cast&lt;char*&gt;(&amp;encoded_message.length);
      fwrite(raw_length, 4, sizeof(char), stdout);
      fwrite(encoded_message.content.c_str(), encoded_message.length, sizeof(char), stdout);
      fflush(stdout);
  }
  </code></pre>