Q:

Getting "Error when communicating with the native messaging host." when sending message from native host to browser extension (Windows)

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.
There are several things that can cause this:
1) Incorrect length sent at beginning of message, or improper order of length bytes sent (endianness).
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).
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.
The code is:
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 & 0x000000ff);
    unsigned char second = (unsigned char)((nMsgLen >> 8) & 0x000000ff);
    unsigned char third = (unsigned char)((nMsgLen >> 16) & 0x000000ff);
    unsigned char fourth = (unsigned char)((nMsgLen >> 24) & 0x000000ff);

    char *bufMsg = new char[4 + nMsgLen]; // allocate message buffer
    const char *pMessageBytes = sMsg.c_str();
    memcpy( &bufMsg[4], &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, &dwNumBytesWritten, NULL))
    {
        BTrace (L"WriteFile() succeeded. Returned TRUE. %lu bytes written", dwNumBytesWritten );
    }

    _close(nStdOutDescriptor);
}

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.

A:

You should be using bufMsg with WriteFile.  You are using pMessageBytes, which is just sending the string and not the length prefix.
You should also consider using FlushFileBuffers after the WriteFile call since as a general rule you will want your native app communication to be sent immediately.

