windows - Windows 上的 Chrome native 消息传递

标签 windows google-chrome google-chrome-extension chrome-native-messaging

我认为它已经在等待答案,但不确定: Native app does not work in Chrome extension
在 Linux 上,它工作正常,但在 Windows 7 和 8 上,我总是收到错误消息“未找到指定的 native 消息传递主机”。

这是我的注册表(我已经尝试过使用双反斜杠和 HKEY_LOCAL_MACHINE):

REG ADD HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\NativeMessagingHosts\com.google.chrome.example.echo /ve /d C:\Users\Chriss\Desktop\nativeMessaging\host\com.google.chrome.example.echo-win.json


list .json:

{
    // Extension ID: knldjmfmopnpolahpmmgbagdohdnhkik
    "key":"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
    "name": "Native Messaging Example",
    "version": "1.0",
    "manifest_version": 2,
    "description": "Send a message to a native application.",
    "browser_action": {
    "default_title": "Test Extension",
    "default_popup": "main.html"
    },
    "icons": {
    "128": "icon-128.png"
    },
    "permissions": [
    "nativeMessaging"
    ]
}


com.google.chrome.example.echo-win.json:

{
    "name": "com.google.chrome.example.echo",
    "description": "Chrome Native Messaging API Example Host",
    "path": "C:\Users\Chriss\Desktop\nativeMessaging\host\native-messaging-example-host.exe",
    "type": "stdio",
    "allowed_origins": [
    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
    ]
}


native-messaging-example-host.exe:

int main(int argc, char* argv[]) {
    // Define our message
    std::string message = "{\"text\": \"This is a response message\"}";
    // Collect the length of the message
    unsigned int len = message.length();
    // We need to send the 4 bytes of length information
    std::cout 
        << char(((len >> 0) & 0xFF))
        << char(((len >> 8) & 0xFF))
        << char(((len >> 16) & 0xFF))
        << char(((len >> 24) & 0xFF));
    // Now we can output our message
    std::cout << message;
    return 0;
}


JS 片段(来自 http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/nativeMessaging/app/main.js?revision=228976 ):

function connect() {
    var hostName = "com.google.chrome.example.echo";
    appendMessage("Connecting to native messaging host <b>" + hostName + "</b>")
    port = chrome.runtime.connectNative(hostName);
    port.onMessage.addListener(onNativeMessage);
    port.onDisconnect.addListener(onDisconnected);
    updateUiState();
}

我找不到问题所在。我的错在哪里?

更新
在使用 Procces Monitor 监控注册表之后。我发现 chrome.exe 在 64 位 key 中搜索 key 。现在我可以看到没有丢失相关的注册表项,但我仍然收到错误。

最佳答案

我在 Windows 上也遇到过这个问题,但能够让它正常工作。尝试以下操作:

关于注册表(我在 HKLM 上,但 HKCU 应该没问题)你应该使用双反斜杠。这是我的 .reg 文件:

[HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\tcchrome.native.handler]
@="G:\\\ExtChrome\\\Extension\\\Native\\\manifest.json"
  1. 使用小写字母且仅包含 3 个部分的 native 进程名称 - 即 com.google.chrome。这听起来很奇怪,但这就是它对我有用的方式......
  2. 将 exe 和 list 放在同一个文件夹中,这样路径将是“native-messaging-example-host.exe”——在这种情况下我很确定,因为我测试过它。

例如,这是我的 list :

{
    "name": "chrome.native.handler",
    "description": "BlaBla helper process",
    "path": "chrome.native.handler.exe",
    "type": "stdio",
    "allowed_origins": [
    "chrome-extension://eoknpfoeckmeidbmbfoklejkppcnlfdm/"
    ]
}

顺便说一句,您没有正确处理响应。您应该以“ native 字节顺序”发送消息长度——您所做的不适用于较大的消息。相反,您可以执行以下操作:

cout.write((char*)&resSize, 4);
cout.write(responseBuf, resSize);

希望对你有帮助

关于windows - Windows 上的 Chrome native 消息传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21961775/

相关文章:

javascript - 如何在回调函数中为 chrome 扩展图标设置动画?

c++ - 通过映射到的地址获取映射到内存中的文件名

c++ - 从 WNDCLASS 切换到 WNDCLASSEX 后标题栏中没有图标

php - Jquery 切换行为不一致

html - 额外填充仅在 Chrome 中

javascript - 如何使用 Chrome 用户脚本获取已放置图像的 URL?

windows - 使用 cmd 删除文件名前缀

Windows 批处理 : sleep

reactjs - FCM(Firebase 云消息): Firebase push notifications are displayed in Firefox but not in Chrome

javascript - 将文件从一台远程服务器发送到另一台远程服务器