0

I spent hours today and yesterday, in how to get Chrome native messaging with C# host. Tried again with Java Host, and the same error too. With the help for these questions, i make several tries, with no success:

C# native host with Chrome Native Messaging

Native messaging from chrome extension to native host written in C#

Very Slow to pass "large" amount of data from Chrome Extension to Host (written in C#)

Tried with VStudio 2017, 2019, with c# 4.7, c# client profile 4.0; With Java, tried with JRE 8, JRE 11, and the same error appears. With Python, the test succeeded, why ? (Binary mode works ?) The only solution acceptable from the Project Manager is C#...

static void Main(string[] args)
{
  bool platform64 = true;
  if (IntPtr.Size == 8)
    platform64 = true;
  else
    platform64 = false;
  logger.InfoFormat("Platform x64?: {0}", platform64);

  logger.InfoFormat("Arguments:");
  foreach (String arg in args)
  {
    logger.InfoFormat("  arg: {0}", arg);
  }

  JObject data;
  while ((data = Read()) != null)
  {
    var processed = ProcessMessage(data);
    SendMessage(processed);
    if (processed["text"].Value<string>().Equals("exit"))
    {
      return;
    }
  }
}

public static JObject Read()
{
  logger.Debug("Init Read()...");
  var stdin = Console.OpenStandardInput();

  var lengthBytes = new byte[4];
  stdin.Read(lengthBytes, 0, 4);

  var length = BitConverter.ToInt32(lengthBytes, 0);

  logger.DebugFormat("Length of bytes...{0}", length);

  var buffer = new char[length];

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

  return JsonConvert.DeserializeObject<JObject>(input);
}

public static void SendMessage(JObject data)
        {
            var bytes = System.Text.Encoding.UTF8.GetBytes(data.ToString(Formatting.None));
            var stdout = Console.OpenStandardOutput();
            stdout.WriteByte((byte)((bytes.Length >> 0) & 0xFF));
            stdout.WriteByte((byte)((bytes.Length >> 8) & 0xFF));
            stdout.WriteByte((byte)((bytes.Length >> 16) & 0xFF));
            stdout.WriteByte((byte)((bytes.Length >> 24) & 0xFF));
            stdout.Write(bytes, 0, bytes.Length);
            stdout.Flush();
        }

From the docs in Google, with respect the binary format and the information from StackOverflow, does any knows what can make this works, and Google comunicate with the Host ?

Sample of sending native message (Direct):

chrome.runtime.sendNativeMessage('com.otisw.signer',
    { text: "list" },
    function (response) {
        console.log("Received " + response);
    });
  • @wOxxOm, thank's. I've edited the post, including SendMessage() method – Jaime Marcondes Jun 13 '19 at 17:41
  • Your SendMessage() uses a hardcoded little-endian order of bytes when it writes the length, however it should be platform-dependent. The correct implementation should arguably use BitConverter just like your Read() does. – wOxxOm Jun 13 '19 at 18:20
  • @wOxxOm, I've tried using BitConverter, same error (Host Tried send bytes...) With BitConverter: using (var stdout = Console.OpenStandardOutput()) { var bytes = System.Text.Encoding.UTF8.GetBytes(json); var lengthBytes = BitConverter.GetBytes(bytes.Length); logger.DebugFormat("Length of bytes...{0}", lengthBytes.Length); stdout.Write(lengthBytes, 0, 4); stdout.Write(bytes, 0, bytes.Length); stdout.Flush(); } – Jaime Marcondes Jun 13 '19 at 19:53

1 Answers1

0

It's working. You wont believe that, but removing the log4net dependency, the Host starts to communicate. Only with dependency imported and , it works too, but when i used to logger.debug() method, the plugin stops working again. Maybe log4net make use stream from Console. Thanks for you help, @wOxxOm, i've modified the Write method to use BitConverter.