Why my native app and chrome extension disconnect when native app call function from dll?

1,168 views
Skip to first unread message

Tuấn Trần

unread,
Oct 24, 2014, 2:57:55 AM10/24/14
to chromi...@chromium.org
I am trying to use native messaging to send some data to my native windows application. 
When my app native call function in dll writen by C/C++, it is disconnected!
main.js of chrome app :
var port = null;

var getKeys = function(obj){
   var keys = [];
   for(var key in obj){
      keys.push(key);
   }
   return keys;
}


function appendMessage(text) {
  document.getElementById('response').innerHTML += "<p>" + text + "</p>";
}

function updateUiState() {
  if (port) {
    document.getElementById('connect-button').style.display = 'none';
    document.getElementById('input-text').style.display = 'block';
    document.getElementById('send-message-button').style.display = 'block';
  } else {
    document.getElementById('connect-button').style.display = 'block';
    document.getElementById('input-text').style.display = 'none';
    document.getElementById('send-message-button').style.display = 'none';
  }
}

function sendNativeMessage() {
  message = {"text": document.getElementById('input-text').value};
  port.postMessage(message);
  appendMessage("Sent message: <b>" + JSON.stringify(message) + "</b>");
}

function onNativeMessage(message) {
  appendMessage("Received message: <b>" + JSON.stringify(message) + "</b>");
}

function onDisconnected() {
  appendMessage("Failed to connect: " + chrome.runtime.lastError.message);
  port = null;
  updateUiState();
}

function connect() {
  var hostName = "com.google.chrome.example.echo";
  appendMessage("Connecting to native messaging host <b>" + hostName + "</b>")
  port = chrome.runtime.connectNative(hostName);
  port.onMessage.addListener(onNativeMessage);
  port.onDisconnect.addListener(onDisconnected);
  updateUiState();
}

document.addEventListener('DOMContentLoaded', function () {
  document.getElementById('connect-button').addEventListener(
      'click', connect);
  document.getElementById('send-message-button').addEventListener(
      'click', sendNativeMessage);
  updateUiState();
});

and native app :
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
typedef int (*pHello)();
int main(int argc, char* argv[])
{

std::cout.setf( std::ios_base::unitbuf ); //instead of "<< eof" and "flushall"
unsigned int a, c, i, t=0;
std::string inp;  

do {

inp="";
t=0;
// Sum the first 4 chars from stdin (the length of the message passed).
for (i = 0; i <= 3; i++) {
t += getchar();
}

// Loop getchar to pull in the message until we reach the total
//  length provided.
for (i=0; i < t; i++) {
c = getchar();
inp += c;
}
pHello Hello;
HINSTANCE s;
TCHAR* sz = _T("SimpleDLL.dll");
s = LoadLibrary(sz);
Hello = (pHello)GetProcAddress(s,"Hello");
Hello();
//Collect the length of the message
unsigned int len = inp.length();
//// We need to send the 4 btyes of length information
std::cout << char(((len>>0) & 0xFF))
<< char(((len>>8) & 0xFF))
<< char(((len>>16) & 0xFF))
<< char(((len>>24) & 0xFF));
//// Now we can output our message
std::cout << inp<<"Gui lai!";
}while(true);
system("pause")  ;
}

Can you help me? 
Thank you very much!

Lessard, Steve

unread,
Oct 24, 2014, 12:58:59 PM10/24/14
to Tuấn Trần, chromi...@chromium.org
My C and C++ is pretty rusty so I can’t be sure, but is there maybe a memory access violation or similar error that is causing your native app to terminate?

When your Chrome extension gets disconnected verify that your native app is still running AND that it still has the same process ID. 



Steve Lessard
Senior Software Engineer
Agilysys, Inc.
Steve....@agilysys.com
Facebook | Twitter | LinkedIn | Agilysys Blog





--
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/d/optout.

Scott Fujan

unread,
Oct 24, 2014, 1:02:36 PM10/24/14
to Lessard, Steve, Tuấn Trần, chromi...@chromium.org
Well, the easy thing to see is that you are sending more data than the length you specify

unsigned int len = inp.length();
std::cout << inp<<"Gui lai!";

Tuấn Trần

unread,
Oct 28, 2014, 1:45:28 PM10/28/14
to chromi...@chromium.org
thanks you!

Vào 13:57:55 UTC+7 Thứ sáu, ngày 24 tháng mười năm 2014, Tuấn Trần đã viết:
Reply all
Reply to author
Forward
0 new messages