Receive Messages from Chrome Extension in a C++ program -
i have chrome extension starts executable c++ file using nativehostmessaging
through json file points file.
this .exe file tries read messages send extension. first, reads 4 bytes depicts length, reads number of bytes equal length. first thing file wait message extension.
however, executable file did not receive extension , keeps on waiting. nothing exists on stdin(0)
.
is security issue or doing wrong? did have read/write sequence in wrong order?
here how mine looks windows:
#include "stdafx.h" #include "json\json.h" int _tmain(int argc, _tchar* argv[]) { //save in file know called file *fh_debug; int errorcode; errorcode =fopen_s(&fh_debug, "debug.txt","ab+"); std::string msg = "host called"; fwrite( msg.c_str(), sizeof(char),msg.size(),fh_debug); unsigned int uisize = 0; std::string inputstr; //read first 4 bytes (=> length) (int = 0; < 4; i++) { int read_char = getchar(); uisize += read_char * (int) pow(2.0, * 8); } //for debug "0000" = 808464432 if ( uisize == 808464432 ) { uisize = 0; inputstr = "{\"text\":\"tester\"}"; } //debug record size msg = std::to_string(uisize); fwrite( msg.c_str(), sizeof(char),msg.size(),fh_debug); //read text (unsigned int = 0; < uisize; i++) { inputstr += getchar(); } //debug record json message msg = inputstr; fwrite( msg.c_str(), sizeof(char),msg.size(),fh_debug); //get text std::string thetext; json::value jsonroot; json::reader jsonreader; bool ok = jsonreader.parse(inputstr, jsonroot); try { thetext = jsonroot.get("text", "no text element").asstring(); } catch (std::exception& e) { std::cout << e.what() << '\n'; throw e; } //debug record json message msg = thetext; fwrite( msg.c_str(), sizeof(char),msg.size(),fh_debug); std::string strout = "{\"text\":\"hello " + thetext + "\"}"; uisize = strout.length(); msg = strout; fwrite( msg.c_str(), sizeof(char),msg.size(),fh_debug); 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(); return 0; }
Comments
Post a Comment