Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native messaging to .NET application on Windows

I followed the native messaging documentation to the best of my abilities, but can't seem to get my native .NET application to even start running on Windows. While looking for additional information, I noticed some things are undocumented and unclear.

First off, the documentation does not mention the requirement of adding the "nativeMessaging" permission. After changing this I got a bit further; Chrome actually mentions "New background app added" in a balloon popup now.

However, the following code keeps disconnecting immediately, regardless of some of the things I tried.

var port = chrome.runtime.connectNative( ... );
port.onDisconnect.addListener(
    function()
    {
        console.log( "Disconnected" );
    } );

Based on my attempts to debug this thing, I have several questions:

  1. On Windows, when does Chrome try to load the manifests from NativeMessagingHosts in the registry? In other words, should I restart Chrome when I change the value, or is it sufficient to reload the extension in "chrome://extensions/"?
  2. The absolute path in the registry, how should it be formatted? E.g. I've seen people stating only "\\\\" slashes as path separators work. Does it need surrounding quotes?
  3. Is there any way to get more information on what is going wrong? For testing purposes, I renamed the registry key to see whether I would get a different error (assuming since it can't load it would notify me of that). Instead "onDisconnect" still seems to be called.
  4. Similar to the path in the registry, how should the relative path pointing to the native application in the native application manifest file which the registry value points to be formatted? (there is an example of this in the documentation, but just to be sure what works and what doesn't ...)

There seem to be too many variables right now for me to debug this, and don't immediately see how I can verify whether or not my registry value at least is set up correctly.

like image 836
Steven Jeuris Avatar asked Oct 21 '22 10:10

Steven Jeuris


1 Answers

I got it working and figured I'd try breaking my solution again in as many different ways possible to answer my originally posed questions. However, I do not remember/know why it didn't work before. In fact, it only started working after I gave up; I had to restart Chrome since an update was installed (34.0.1847.116 m), after which all of a sudden my native application showed up since the plugin I was working on was still enabled.

That aside, the following is a report on what does and does not work by changing some things and seeing whether the native application is still launched.

1. On Windows, when does Chrome try to load the manifests from NativeMessagingHosts in the registry?

Whenever you click "Reload" on the extension you are developing in "chrome://extensions/", the native host will be loaded again from the registry. In other words, it suffices to click reload to verify whether your manifest files and registry are set up correctly.

2. The absolute path in the registry, how should it be formatted?

This is an example of a valid registry value:

C:\Users\Bleh\Some\Path Even With Spaces\No\Problem\someManifestFile.json

  • Without surrounding quotes.
  • Using single backslashes works, as do forwards slashes, or even an infinite amount of slashes. :O E.g. "C:\Some\\\Very Weird\Path".
  • ".json" extension is required.
  • It needs to be absolute.

3. Is there any way to get more information on what is going wrong?

Well, you might be able to look at Chromium's source code to determine exactly what happens, but other than that I don't see any more extensive feedback you can get from the offered APIs.

4. How should the path in the manifest be formatted? Example from the documentation, which seems to be correct:

"path": "C:\\Program Files\\My Application\\chrome_native_messaging_host.exe"

  • Two backslashes are needed as separators.
  • The path can be relative.
like image 92
Steven Jeuris Avatar answered Oct 26 '22 22:10

Steven Jeuris