简体   繁体   English

通过Google Chrome本机消息传递检测exe何时启动

[英]Detect when exe launched via Google Chrome Native Message Passing

I'm trying to retrofit an existing NPAPI plugin to use Google's native message passing technology. 我正在尝试改造现有的NPAPI插件,以使用Google的本机消息传递技术。 Since it's an existing exe, we already have some console behavior programmed in so that users can call our program from the terminal. 由于它是一个现有的exe,我们已经编写了一些控制台行为,以便用户可以从终端调用我们的程序。 Is there any way for us to detect, in a C# application, that the exe has been launched by Google Chrome for message passing? 在C#应用程序中,我们有什么方法可以检测到Google Chrome已经启动了用于邮件传递的exe吗? If we could do that, we could launch the message passing loop if we're called from Chrome but resume normal behavior if called from Powershell/cmd. 如果我们可以这样做,我们可以启动消息传递循环,如果我们从Chrome调用,但如果从Powershell / cmd调用则恢复正常行为。

I've tried inspecting the command line arguments passed to the program when launched by Chrome, but there are none. 我已经尝试检查Chrome启动时传递给程序的命令行参数,但没有。 Having a configurable option there would solve this for us, but as far as I can tell it's not possible. 有一个可配置的选项,我们可以解决这个问题,但据我所知,这是不可能的。 I haven't yet had a chance to inspect the current working directory in case it could also be used as an identifier. 我还没有机会检查当前的工作目录,以防它也可以用作标识符。

Actually, yes, I believe it is possible. 实际上,是的,我相信这是可能的。

When, for example, a C# console application is started as a native message client host, it is passed two arguments: 例如,当C#控制台应用程序作为本机消息客户端主机启动时,它会传递两个参数:

--parent-window=<number>

and

chrome-extension://<extension identifier>/

I think the second argument is probably the ideal one for determining that not only was it Chrome that launched the process, but that the specific extension you authored and intended to call it launched it! 我认为第二个论点可能是一个理想的判断,不仅是Chrome启动了这个过程,而且您编写并打算称之为的特定扩展程序启动了它!

Please make note in the above, the "<" and ">" are not literally part of the argument, and just used to denote the beginning and end of that part of the message, much like double quotes. 请注意上面的内容,“<”和“>”不是字面上的参数的一部分,只是用来表示消息部分的开头和结尾,就像双引号一样。

Chrome原生主机

Alternatively, just have your extension invoke a script (.bat, .sh, etc.) that passes special arguments to your native host. 或者,只需让您的扩展程序调用一个脚本(.bat,.sh等),它将特殊参数传递给您的本机主机。 This way you could pass specific arguments of your own. 这样你就可以传递自己的特定参数。

API不支持传递命令行参数,但您的主机进程应该能够检查其自己的父进程,以确定它是由Chrome还是其他人启动的。

An alternative to checking the arguments as suggested by @aikeru would be to check for the existence of certain environment variables that were passed from Chrome to the native messaging host. 检查@aikeru建议的参数的另一种方法是检查是否存在从Chrome传递到本机消息传递主机的某些环境变量。 My host has the following variables that seem to be specific to Chrome (found with Sysinternals Process Monitor): 我的主机有以下似乎特定于Chrome的变量(在Sysinternals Process Monitor中找到):

CHROME_ALLOCATOR=TCMALLOC
CHROME_BREAKPAD_PIPE_NAME=\\.\pipe\GoogleCrashServices\S-1-5-18
CHROME_MAIN_TIME=13037817851797830
CHROME_METRO_DLL=0
CHROME_PRE_READ_EXPERIMENT=100-pct-default
CHROME_RESTART=Google Chrome|Whoa! Google Chrome has crashed. Relaunch now?|LEFT_TO_RIGHT
CHROME_VERSION=33.0.1750.117

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM