Native host exe

Native Messaging – Chrome Developers

Native host exe

Extensions and apps can exchange messages with native applications using an API that is similar to the other message passing APIs. Native applications that support this feature must register a native messaging host that knows how to communicate with the extension. Chrome starts the host in a separate process and communicates with it using standard input and standard output streams.

Native messaging host #

In order to register a native messaging host the application must install a manifest file that defines the native messaging host configuration. Below is an example of the manifest file:

{
“name”: “com.my_company.my_application”,
“description”: “My Application”,
“path”: “C:\\Program Files\\My Application\\chrome_native_messaging_host.exe”,
“type”: “stdio”,
“allowed_origins”: [
“chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/”
]}

The native messaging host manifest file must be valid JSON and contains the following fields:

NameDescription
nameName of the native messaging host. Clients pass this string to runtime.connectNative or runtime.sendNativeMessage. This name can only contain lowercase alphanumeric characters, underscores and dots. The name cannot start or end with a dot, and a dot cannot be followed by another dot.
descriptionShort application description.
pathPath to the native messaging host binary. On Linux and OSX the path must be absolute. On Windows it can be relative to the directory in which the manifest file is located. The host process is started with the current directory set to the directory that contains the host binary. For example if this parameter is set to C:\Applicationm_host.exe then it will be started with current directory C:\Application\.
typeType of the interface used to communicate with the native messaging host. Currently there is only one possible value for this parameter: stdio. It indicates that Chrome should use stdin and stdout to communicate with the host.
allowed_originsList of extensions that should have access to the native messaging host. Wildcards such as chrome-extension://*/* are not allowed.

Native messaging host location #

The location of the manifest file depends on the platform.

On Windows, the manifest file can be located anywhere in the file system. The application installer must create registry key HKEY_LOCAL_MACHINE\SOFTWARE\Google\ChromeativeMessagingHosts\_com.my_company.

my_application_ or HKEY_CURRENT_USER\SOFTWARE\Google\ChromeativeMessagingHosts\_com.my_company.my_application_, and set default value of that key to the full path to the manifest file.

For example, using the following command:

REG ADD “HKCU\Software\Google\ChromeativeMessagingHosts\com.my_company.my_application” /ve /t REG_SZ /d “C:\path\tomh-manifest.json” /f

or using the following .reg file:

Windows Registry Editor Version 5.00[HKEY_CURRENT_USER\Software\Google\ChromeativeMessagingHosts\com.my_company.my_application]@=”C:\\path\\to\mh-manifest.json”When Chrome looks for native messaging hosts, first the 32-bit registry is queried, then the 64-bit registry.

On OS X and Linux, the location of the native messaging host's manifest file varies by the browser (Google Chrome or Chromium). The system-wide native messaging hosts are looked up at a fixed location, while the user-level native messaging hosts are looked up in a subdirectory within the user profile directory called NativeMessagingHosts.

  • OS X (system-wide)
    • Google Chrome: /Library/Google/Chrome/NativeMessagingHosts/_com.my_company.my_application_.json
    • Chromium: /Library/Application Support/Chromium/NativeMessagingHosts/_com.my_company.my_application_.json
  • OS X (user-specific, default path)
    • Google Chrome: ~/Library/Application Support/Google/Chrome/NativeMessagingHosts/_com.my_company.my_application_.json
    • Chromium: ~/Library/Application Support/Chromium/NativeMessagingHosts/_com.my_company.my_application_.json
  • Linux (system-wide)
    • Google Chrome: /etc/opt/chrome/native-messaging-hosts/_com.my_company.my_application_.json
    • Chromium: /etc/chromium/native-messaging-hosts/_com.my_company.my_application_.json
  • Linux (user-specific, default path)
    • Google Chrome: ~/.config/google-chrome/NativeMessagingHosts/_com.my_company.my_application_.json
    • Chromium: ~/.config/chromium/NativeMessagingHosts/_com.my_company.my_application_.json

Native messaging protocol #

Chrome starts each native messaging host in a separate process and communicates with it using standard input (stdin) and standard output (stdout).

The same format is used to send messages in both directions: each message is serialized using JSON, UTF-8 encoded and is preceded with 32-bit message length in native byte order.

The maximum size of a single message from the native messaging host is 1 MB, mainly to protect Chrome from misbehaving native applications. The maximum size of the message sent to the native messaging host is 4 GB.

The first argument to the native messaging host is the origin of the caller, usually chrome-extension://[ID of allowed extension].

This allows native messaging hosts to identify the source of the message when multiple extensions are specified in the allowed_origins key in the native messaging host manifest.

Warning: In Windows, in Chrome 54 and earlier, the origin was passed as the second parameter instead of the first parameter.

When a messaging port is created using runtime.connectNative Chrome starts native messaging host process and keeps it running until the port is destroyed. On the other hand, when a message is sent using runtime.

sendNativeMessage, without creating a messaging port, Chrome starts a new native messaging host process for each message. In that case the first message generated by the host process is handled as a response to the original request, i.e. Chrome will pass it to the response callback specified when runtime.

sendNativeMessage is called. All other messages generated by the native messaging host in that case are ignored.

On Windows, the native messaging host is also passed a command line argument with a handle to the calling chrome native window: –parent-window=. This lets the native messaging host create native UI windows that are correctly focused.

Connecting to a native application #

Sending and receiving messages to and from a native application is very similar to cross-extension messaging. The main difference is that runtime.connectNative is used instead of runtime.connect, and runtime.sendNativeMessage is used instead of runtime.sendMessage.
These methods can only be used if the “nativeMessaging” permission is declared in your app's manifest file.

The Following example creates a runtime.Port object that's connected to native messaging host com.my_company.my_application, starts listening for messages from that port and sends one outgoing message:

var port = chrome.runtime.connectNative('com.my_company.my_application');
port.onMessage.addListener(function(msg) {
console.log(“Received” + msg);
});
port.onDisconnect.addListener(function() {
console.log(“Disconnected”);
});
port.postMessage({ text: “Hello, my_application” });

runtime.sendNativeMessage can be used to send a message to native application without creating a port, e.g.:

chrome.runtime.sendNativeMessage('com.my_company.my_application',
{ text: “Hello” },
function(response) {
console.log(“Received ” + response);
});

Debugging native messaging #

When the native messaging host fails to start, writes to stderr or when it violates the communication protocol, output is written to the error log of Chrome. On Linux and OS X, this log can easily be accessed by starting Chrome from the command line and watching its output in the terminal. On Windows, use –enable-logging as explained at How to enable logging.

Here are some errors and tips for solving the issues:

  • Failed to start native messaging host.
    • Check whether you have sufficient permissions to execute the file.
  • Invalid native messaging host name specified.
    • Check whether the name contains any invalid characters. Only lowercase alphanumeric characters, underscores and dots are allowed. A name cannot start or end with a dot, and a dot cannot be followed by another dot.
  • Native host has exited.
    • The pipe to the native messaging host was broken before the message was read by Chrome. This is most ly initiated from your native messaging host.
  • Specified native messaging host not found.
    • Is the name spelled correctly in the extension and in the manifest file?
    • Is the manifest put in the right directory and with the correct name? See native messaging host location for the expected formats.
    • Is the manifest file in the correct format? In particular, is the JSON syntax correct and do the values match the definition of a native messaging host manifest?
    • Does the file specified in path exist? On Windows, paths may be relative, but on OS X and Linux, the paths must be absolute.
  • Native messaging host host name is not registered. (Windows-only)
    • The native messaging host was not found in the Windows registry. Double-check using regedit whether the key was really created and matches the required format as documented at native messaging host location.
  • Access to the specified native messaging host is forbidden.
    • Is the extension's origin listed in allowed_origins?
  • Error when communicating with the native messaging host.
    • This is a very common error and indicates an incorrect implementation of the communication protocol in the native messaging host.
      • Make sure that all output in stdout adheres to the native messaging protocol. If you want to print some data for debugging purposes, write to stderr.
      • Make sure that the 32-bit message length is in the platform's native integer format (little-endian / big-endian).
      • The message length must not exceed 1024*1024.
      • The message size must be equal to the number of bytes in the message. This may differ from the “length” of a string, because characters may be represented by multiple bytes.
      • Windows-only: 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 ( = 0A) are replaced with Windows-style line endings (\r = 0D 0A). The I/O mode can be set using __setmode.

Examples #

The examples/api/nativeMessaging directory contains an example application that uses native messaging to communicate with a Python script that serves as a native messaging host. The sample host's directory also contains scripts to install/remove the native messaging host.

To try out the example, first download and extract the sample app and sample host. Run install_host.bat (Windows) or install_host.sh (Linux / OS X) to install the native messaging host. Then load the app and interact with the app. Run uninstall_host.bat or uninstall_host.sh to unregister the native messaging host when you are done.

Источник: https://developer.chrome.com/docs/apps/nativeMessaging/

Fixing Scripted diagnostics native host has stopped working?

Native host exe

Occasionally, when you are using Windows’ troubleshooting tools, you may receive the following error message: “Scripted Diagnostics Native Host has stopped working. A problem caused this program to stop working correctly.

Windows will close the program and notify you if a solution is available”.

Some users have even reported getting the “Scripted Diagnostics Native Host has stopped working” error once every few hours — which, naturally, has proved very disruptive to their PC experience.

So, what if Scripted Diagnostics Native Host is not working? There are several methods you can try to fix the “Scripted diagnostics Native Host has stopped working” error.

How to fix the “Scripted diagnostics Native Host has stopped working” error?

There are five possible solutions to the issue. They are:

  • Running a System File Checker scan
  • Using the Check Disk utility
  • Clean booting Windows
  • Using the Reliability Monitor
  • Reverting to an older version of Windows

Let’s see what each of the above methods entails.

Solution one: running a System File Checker scan

In some cases, the “Scripted diagnostics Native Host has stopped working” error can be fixed with the help of the System File Checker that will run a scan of your PC and repair corrupted system files. Here’s how to run an SFC scan on Windows 10:

  • Go to Cortana’s search box (Win + Q).
  • In the search box, type “cmd”.
  • Right-click Command Prompt and choose Run as an administrator.
  • In the Prompt, enter the following address: DISM.exe /Online /Cleanup-image /Restorehealth.
  • Press Enter.
  • Type “sfc /scannow” and press the Return key to start the scan.
  • Wait for the scanning process to complete (this will take upwards of 30 minutes).
  • Finally, restart your PC.

Solution two: using the Check Disk utility

Windows’ Check Disk utility can also be used to get rid of the error message. Here’s what to do:

  • Open Cortana.
  • In the search box, type “cmd”.
  • Right-click Command Prompt and choose the Run as an administrator option.
  • In the Command Prompt, type “CHKDSK /R” and press the Return key.
  • Follow the on-screen instructions and restart your computer if needed.

The Scripted Diagnostics Native Host issue can be a result of third-party software running in the background. Sometimes, you may not even be aware of certain programs being active on your PC. If you clean boot Windows, you will stop these programs and, subsequently, get rid of the annoying error message. To clean boot Windows:

  • Press the Win + R keys to launch Run.
  • In the text box, type “msconfig” and click OK.
  • Go to the Services tab.
  • Check the Hide all Microsoft services option.
  • Click Disable all.
  • In the System Configuration window, go to the General tab.
  • Press the Selective startup radio button.
  • Check Load system services and Use original boot configuration.
  • Uncheck the Load startup items option.
  • Click Apply and OK.
  • Follow the prompts to restart your PC.

Additionally, make sure you have a reliable anti-malware program  Auslogics Anti-Malware protecting your computer from malicious items that can invade your system, causing this and a whole range of other errors.

Auslogics Anti-Malware will run regular automatic scans of your PC and detect and remove any dangerous programs. Plus, the software can run alongside your main anti-virus without causing compatibility issues.

RECOMMENDED

Check your PC for malware your antivirus may miss and get threats safely removed with Auslogics Anti-Malware

DOWNLOAD NOW

Solution four: using the Reliability Monitor

Another tool that can prove useful in fixing the “Scripted Diagnostics Native Host has stopped working” error is the Reliability Monitor, a program that keeps track of program & system crashes. Here’s how to use the Reliability Monitor:

  • Start Run.
  • In the text box, type “perfmon /rel” and click OK.
  • The Reliability Monitor window will open.
  • In the graph, click the red cross for the Scripted diagnostics error.
  • Select View technical details to get the path to the program.
  • You will be able to uninstall any third-party software that might have caused the error in the Programs and Features Control Panel applet.
  • If the issue was caused by a system process, you might need to disable the said service in the Services or Task Manager windows.

Solution five: reverting to an older version of Windows

Occasionally, the error can appear after a Windows update. In this case, you might consider uninstalling the latest Windows updates. In order to do that, you can use the System Restore utility:

  • Start Run.
  • Type “rstrui” and click OK to launch System Restore.
  • Select Next and choose Show more restore points.
  • Select the restore point that will undo the error-causing update.
  • Click Next and Finish to confirm.

Which solution has worked best for you in fixing the Scripted Diagnostics Native Host error? Share in the comments below.

Do you this post?

Источник: https://www.auslogics.com/en/articles/scripted-diagnostics-native-host-stopped-working/

Поделиться:
Нет комментариев

    Добавить комментарий

    Ваш e-mail не будет опубликован. Все поля обязательны для заполнения.