0

I have an chrome native application in windows form( c#). it use to open when i enable the extension in chrome. Can we manage this to open only when the application exe is clicked? How can we do that? This is the c# code

  private static string OpenStandardStreamIn()
    {
    //// We need to read first 4 bytes for length information
    Stream stdin = Console.OpenStandardInput();
    int length = 0;
    byte[] bytes = new byte[4];
    stdin.Read(bytes, 0, 4);
    length = System.BitConverter.ToInt32(bytes, 0);

    string input = "";
    for (int i = 0; i < length;i++ )    
    {
    input += (char)stdin.ReadByte();
    }

    return input;  
    }

    private static void OpenStandardStreamOut(string stringData)
    {
    //// We need to send the 4 btyes of length information
    string msgdata = "{\"text\":\"" + stringData + "\"}";
    int DataLength = stringData.Length;
    Stream stdout = Console.OpenStandardOutput();
    stdout.WriteByte((byte)((DataLength >> 0) & 0xFF));
    stdout.WriteByte((byte)((DataLength >> 8) & 0xFF));
    stdout.WriteByte((byte)((DataLength >> 16) & 0xFF));
    stdout.WriteByte((byte)((DataLength >> 24) & 0xFF));
    //Available total length : 4,294,967,295 ( FF FF FF FF )

    Console.Write(msgdata);
    }

background.Js Code

  var host_name = "com.example.native";
    var port = null;

    connectToNative();
    function connectToNative()
    {
    console.log('Connecting to native host: ' + host_name);
    port = chrome.runtime.connectNative(host_name);
    port.onMessage.addListener(onNativeMessage);
    port.onDisconnect.addListener(onDisconnected);
    sendNativeMessage("test");

}

function sendNativeMessage(msg) {
message = {"text" : msg};
console.log('Sending message to native app: ' + JSON.stringify(message));
port.postMessage(message);
console.log('Sent message to native app: ' + msg);
}

function onNativeMessage(message) {
console.log('recieved message from native app: ' + JSON.stringify(msg));
}

function onDisconnected() {
    console.log(chrome.runtime.lastError);
console.log('disconnected from native app.');
port = null;
}

Menifest file to connect chrome extension with application

{
  "name": "com.example.native",
  "description": "Native support for Chrome Extension",
  "path": "NativeApp.exe",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://ajfkjfmkedgcgdckdkmppfblonpeench/"
  ]
}  
Cœur
  • 37,241
  • 25
  • 195
  • 267
Shivang MIttal
  • 990
  • 1
  • 14
  • 36
  • Can I please ask for a bit of context background? What do you mean by a chrome native application in C#? Do you mean an NPAPI plugin? Do you mean a NaCl app (typically written in C/C++)? Do you mean a chrome app (typically written in JS)? – petewil-G Aug 27 '14 at 17:39
  • @petewil-G : I have an application in c# and a chrome extension which includes background.js, content.js and a menifest file. i am using native messaging to send and recieve message from a chrome extension. I have edited my question, please check – Shivang MIttal Aug 28 '14 at 06:36

1 Answers1

2

The point of a Native Host app is to be opened by Chrome as necessary. If you open an instance of it manually, it won't be connected to Chrome.

In pseudocode, you could implement the following logic in your native app:

if( /* opened by Chrome */ ){
  // Do not show UI
  // Listen for command to show UI
  // Communicate with Chrome
} else {
  if( /* there is an open instance of the app */) {
    // Message that instance to show UI
    // Terminate
  } else {
    // Show error, terminate
  }
}

This question might be of interest.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206