acandylevey / NativeMessaging

C# Chome Native Messaging Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

C# console app not receiving messages

AsianPotato opened this issue · comments

The .exe gets launched when I use the example chrome extension and press "connect" but when I press send the c# program doesn't do anything

public class ChromeServerHost : Host
    {
        private const bool SendConfirmationReceipt = true;

        public override string Hostname
        {
            get { return "com.google.chrome.example.echo-manifest.json"; }
        }

        public ChromeServerHost() : base(SendConfirmationReceipt)
        {

        }

        protected override void ProcessReceivedMessage(JObject data)
        {
            Console.WriteLine("HELLO");
            SendMessage(data);
        }
    }

Then in main I have

ChromeServerHost host = new ChromeServerHost();
host.Listen();

It is registered correctly,

private JObject Read()
        {
            Utils.LogMessage("Waiting for Data");
            var stdin = Console.OpenStandardInput();

            var lengthBytes = new byte[4];
            Console.WriteLine("HELLO1");
            stdin.Read(lengthBytes, 0, 4);
            Console.WriteLine("HELLO2");
            var buffer = new char[BitConverter.ToInt32(lengthBytes, 0)];

            using (var reader = new StreamReader(stdin)) while (reader.Peek() >= 0) reader.Read(buffer, 0, buffer.Length);

            return JsonConvert.DeserializeObject<JObject>(new string(buffer));
        };

Hello1 gets printed but not hello2, also the logger never logs "Data Received:"

Did you start Chrome with the --enable-logging flag as detailed in Debugging native messaging ?

image
image
It's something on the C# side.

Looks like you may have different hosts configured in the chrome extension and in the .net application.

com.google.chrome.example.echo-manifest.json & com.google.chrome.example.echo

The hostname in the c# app shouldn't have mattered anyway since I don't use .register() and that's the only place it's used and the c# application was opening after pressing connect, I used various host names when testing so at the time that was the correct hostname anyway. Like I said Hello1 gets printed but not hello2, also the logger never logs "Data Received:"

I'm thinking it might be something related to on windows there is a need to make sure that the program's I/O mode is set to O_BINARY. By default, the I/O mode is O_TEXT, which corrupts the message format as line breaks (\n = 0A) are replaced with Windows-style line endings (\r\n = 0D 0A).

Does your chrome extension work the provided example host: https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/nativeMessaging/host/

If so, what error message do you get when you comment out the I/O mode of the host (lines 24/25)?

The example host works both with and without the lines being commented

The issue was fixed after compiling in release mode instead of debug.