1
votes

I am working on Chrome Extension with Native Host messaging. I am not able to use message text in my host application. Everything working fine from the establishing connection to get response in extension. I need to use Message text in my application for further use/execution in simple text datatype (string/char). I know message is in UTF8 encoded form i tried to decode but still getting problem, can any one help me out?

When i decode message chrome extension console show Error: "Error when communicating with the native messaging host." and if i use that message text after "cout" same error is there "Error when communicating with the native messaging host". Direct sending and receiving message works fine for me.

Code is something like this :

 std::string mycode(std::string data){
    data= data+"abc"; //changing text to any thing.
    cout<< data;
    anotherFunction(data);//killing processes using string data
}
int main(int argc, char* argv[])
{
    std::cout.setf( std::ios_base::unitbuf );
    while (true)
    {
        unsigned int ch, inMsgLen = 0, outMsgLen = 0;
        std::string input = "", response = "";
        std::cin.read((char*)&inMsgLen, 4);
        if (inMsgLen == 0)
        {
            break;
        }
        else
        {
            for (int i=0; i < inMsgLen; i++)
            {
                ch = getchar();
                input += ch;
            }
        }
        response.append("{\"echo\":").append(input).append("}");
        outMsgLen = response.length();
        std::cout.write((char*)&outMsgLen, 4);
        std::cout << response;
        cout<< input;
        //using "input" variable for further user 
        mycode(input);
    }
    return 0;
}
1
What do you mean with message is in UTF8 ? The message passed to the native messaging host is in binary mode, you need to set the stdin to binary, read the first 4 bytes to know the size of the message and then read the message. - João Augusto
I am doing same thing, reading the first 4 bytes to know the size of the message and then read the message. But when i use message text for further use then chrome extension log showing error "Error when communicating with the native messaging host". - Himanshu kukreja
post your communication c++ code. - João Augusto
Please check i shared the code below. @JoãoAugusto - Himanshu kukreja

1 Answers

1
votes

That's wrong, did you read the docs ? Try something like this...

_setmode( _fileno( stdin ), _O_BINARY ); 
    _setmode( _fileno( stdout ), _O_BINARY ); 

    char cBuffer[65536] = {0}; 
    while(true)
    {       
        unsigned int uiSize = 0;                        
        std::cin.read((char*)&uiSize, sizeof(unsigned int));

        if(uiSize != 0 && uiSize < 65536)
        {                       
            memset(cBuffer, 0, 65536);
            std::cin.read(cBuffer, uiSize);

            std::string strIn(cBuffer);

            std::string strOut = "{\"result\":\"This is a Test\"}";                 
            uiSize = strOut.length();

            std::cout << char(((uiSize>>0) & 0xFF));
            std::cout << char(((uiSize>>8) & 0xFF));
            std::cout << char(((uiSize>>16) & 0xFF));
            std::cout << char(((uiSize>>24) & 0xFF));   
            std::cout << strOut.c_str();                
        }
        else
            break;
    }

You need to set the io to binary, otherwise things like this could happen... if a byte with the value 00011010 (CTRL ALT Z = 26) is present it will be treated as a EOF and end the communication. :)