2016-07-04 11 views
-1

私はこのファイルを指し示すjsonファイルを通してNativeHostMessagingを使って実行可能なC++ファイルを開始するChrome拡張機能を持っています。C++プログラムのChrome拡張機能からメッセージを受信

この.exeファイルは、内線番号から送信されたメッセージを読み取ろうとします。まず、長さを表す4バイトを読み込み、長さに等しいバイト数を読み込みます。このファイルが最初に行うことは、拡張機能からのメッセージを待つことです。

ただし、実行可能ファイルは拡張子から何も受信せず、待機し続けます。 STDIN(0)には何も存在しません。

これはセキュリティの問題ですか、何か間違っていますか?私は間違った順序で読み書きシーケンスを持っていましたか?

+1

は質問が明確ではないですか? – user3856170

+0

質問を編集して質問を理解しやすくしました。あなたの意図する質問が届かない場合は、私に知らせて、それを自由に変更してください。 – Sometowngeek

+0

質問が投票されたのはなぜですか? – user3856170

答えて

0

このコードは、OSX

ここ
#include <stdio.h> 
#include <iostream> 
#include<stdlib.h> 
#include <string.h> 
#include<unistd.h> 
int main(){ 

    char cBuffer[65536] = {0}; 
    FILE *fh; 
    fh=fopen("/Users/mycode.text","ab+"); 
    std::string msg="\"This is a Test\"}"; 
    std::string hello="itsme"; 
    int ii=4; 
    while(ii) 
    { 
     unsigned int uiSize = 0; 
     std::string strOut = "{\"type\":"; 
     strOut=strOut + msg; 

     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(); 

     std::cin.read((char*)&uiSize, sizeof(unsigned int)); 
     if(uiSize > 0 && uiSize < 65536) 
     { 
      memset(cBuffer, 0, 65536); 
      std::cin.read(cBuffer, uiSize); 
      fwrite(cBuffer,sizeof(char),uiSize,fh); 
      std::string strIn(cBuffer); 
      msg=strIn+"}"; 
      if(!strIn.compare(hello)) 
       exit(0); 
      ii--; 

     } 
     else 
      break; 
    } 


    return 0; 
} 
1

に私のために働いている鉱山は、Windows用のように見える方法:

#include "stdafx.h" 
#include "json\json.h" 

int _tmain(int argc, _TCHAR* argv[]) 
{ 

    //save in a file so we know it was 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 the first four bytes (=> Length) 
    for (int i = 0; i < 4; i++) 
    { 
     int read_char = getchar(); 
     uiSize += read_char * (int) pow(2.0, i * 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 the text 
    for (unsigned int i = 0; i < uiSize; i++) 
    { 
     inputStr += getchar(); 
    } 

    //debug record json message 
    msg = inputStr; 
    fwrite(msg.c_str(), sizeof(char),msg.size(),fh_debug); 

    //get the 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; 
}