无法使用本机消息接收10字节的消息

时间:2014-07-02 09:50:13

标签: c++ google-chrome google-chrome-extension chrome-native-messaging

我创建了一个简单的chrome扩展,以便通过chrome本机消息传递与Windows本机应用程序进行交互。

我能够在浏览器和应用程序之间发送和接收消息。但是,如果从本机应用程序发送的消息长度为10或2560,则在扩展的onmessage事件侦听器中不会收到该消息。

原生应用代码

char *test = "{\"tes\":\"\"}";
unsigned int tLen = strlen(test);
cout<< char(((tLen>>0) & 0xFF))
    << char(((tLen>>8) & 0xFF))
    << char(((tLen>>16) & 0xFF))
    << char(((tLen>>24) & 0xFF));
cout << test << flush;

如果我指定char *test = "{\"test\":\"\"}"(带额外的t),它的工作正常。

我不知道这个问题的原因是什么。

请帮忙!

谢谢!

1 个答案:

答案 0 :(得分:1)

传递数据长度的方式不正确,长消息也会失败。相反,试试这个:

char *test = "{\"tes\":\"\"}";
unsigned int tLen = strlen(test);
cout.write((char*)&tLen , 4);
cout << test << flush;