I got this from chromium-extension group and it WORKS for me.
Pasting the exact same:
The problem is likely in the header that contains the four-byte length of the message. If it's weird, Chrome will break the connection. Since stdout is in text mode by default, certain ASCII characters may be getting converted to different ones on Windows, such as \n becomes \r\n. You end up with more bytes in the header than you need, so the Chrome extension will think that you're sending millions of bytes of data, gets confused, and breaks the connection, and some of the header bytes bleed into the message, causing the JSON parser to encounter unexpected characters.
Try setting stdout into binary mode:
_setmode(_fileno(stdout), _O_BINARY);
If that doesn't help, you can additionally try this alternate method of writing to stdout:
unsigned int len = final_msg.length();
fwrite(&len, 4, 1, stdout);
printf("%s", final_msg.c_str());
fflush(stdout);
You may need to add some includes: fcntl.h io.h