native messaging example in c++ for windows

2,090 views
Skip to first unread message

Rajesh Katalkar

unread,
Dec 3, 2013, 12:20:13 PM12/3/13
to chromi...@chromium.org
I am making a native messaging host using vc++ 2010 console app ...It is working but does not take read big json strings...
If i pass the same value to sample made in python ...it works there.....

following is roughly how i read

while(1)
{
   cnt = 0;
   zeromem len;;
   cin.read(len,4);
   for(i=0;i<4;i++)
    cnt += len[i];

  cin,read(buff,cnt);

//also below is how i write
    unsigned int len = response.length();
        cout << char(((len>>0) & 0xFF))
            << char(((len>>8) & 0xFF))
            << char(((len>>16) & 0xFF))
            << char(((len>>24) & 0xFF));

        cout<<response<<flush;   
}

I could not find a working eg of c++ anywhere



   

Lambros Lambrou

unread,
Dec 3, 2013, 1:56:11 PM12/3/13
to Rajesh Katalkar, Chromium Apps
I think you're missing a line to shift "cnt" 8 bits before adding len[i] inside the loop:

cnt << 8;
cnt += len[i];

Lambros



--
You received this message because you are subscribed to the Google Groups "Chromium Apps" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-app...@chromium.org.
To post to this group, send email to chromi...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-apps/.
For more options, visit https://groups.google.com/a/chromium.org/groups/opt_out.

Rajesh Katalkar

unread,
Dec 3, 2013, 2:25:16 PM12/3/13
to chromi...@chromium.org, Rajesh Katalkar
message ={"text":"asasdasdasdasdasdasd","text1":"asasdasdasdasdasdasd","text2":"asasdasdasdasdasdasd","text3":"asasdasdasdasdasdasd","text4":"asasdasdasdasdasdasd"};

are you saying cnt = cnt<<8;
i tried it no use...
additionally i get a negative value of length in buff read

Lambros Lambrou

unread,
Dec 3, 2013, 2:39:36 PM12/3/13
to Rajesh Katalkar, Chromium Apps
Yes you're right, I did mean cnt <<= 8;
Also, on an x86 system, the number is stored as little-endian, so you'll need to reverse the loop: for (i = 3; i >=0; i--)


This technique (using reinterpret_cast<char*>) has the advantage of working on little-endian and big-endian platforms.


Rajesh Katalkar

unread,
Dec 3, 2013, 3:14:21 PM12/3/13
to chromi...@chromium.org

Rajesh Katalkar

unread,
Dec 3, 2013, 3:14:36 PM12/3/13
to chromi...@chromium.org, Rajesh Katalkar
i am using win7 64bit
my native host is 32bit

 i get "›" in len[0] which is -101

following is what i did
            for(int i=3;i>=0;i--)
            {  
                leng <<= 8;
                leng += (len[i]);
            }

i am still getting -101
where to put the reinterpret.....
I did not understand the chromium code,,,,it was sending message_length as buff to read from  but its a typedef of unit32 which has nothing
    MessageLengthType message_length;
    int read_result = read_stream_.ReadUntilComplete(
        reinterpret_cast<char*>(&message_length), kMessageHeaderSize);


can you please update my sample give me exact code so that i can try it

Rajesh Katalkar

unread,
Dec 3, 2013, 3:47:14 PM12/3/13
to chromi...@chromium.org, Rajesh Katalkar
I think i understood the code and what you want to say..
below is updated code which works fine for me now ,,for both 32 and 64bit version of native host....
i will test this more tomorrow as i will sleep now,,,
but please confirm the below logic is right or wrong..

while(1)
{
   int cnt = 0;

   cin.read(reinterpret_cast<char*>(&cnt) ,4);


  cin,read(buff,cnt);

Lambros Lambrou

unread,
Dec 3, 2013, 4:38:25 PM12/3/13
to Rajesh Katalkar, Chromium Apps
Your "len" array was signed char[] rather than unsigned char[], which was messing up your arithmetic. Replacing "leng += len[i]" with "leng |= len[i]" would fix this, I think.

The reinterpret_cast version looks OK. Though it's better to use an explicit 32-bit type such as uint32_t instead of int.


Vikas Pushkar

unread,
Jul 5, 2016, 4:05:34 AM7/5/16
to Chromium-Apps-Announce
Hi,

I am trying to do the same thing but i am recieving length as 0

i am using following code:

 while (1){
             int cnt = 0;
             std::cin.read(reinterpret_cast<char*>(&cnt) ,4);
             std::cin.read(buff,cnt);
             sprintf(Log_str," %d and %s \n",cnt,buff);
             LOG(Log_str);//LOG writes it to a opened file.

the output is "0  and   "



On Tuesday, 3 December 2013 22:50:13 UTC+5:30, Rajesh Katalkar wrote:
Reply all
Reply to author
Forward
0 new messages