一、效果图
二、思路
通过自定义URL Protocol来调用应用程序。浏览器在解析到自定义URL Protocol之后,会寻找注册表,然后通过注册表启动相应的程序。这样就可以在WEB页面调到你的程序了。比如在浏览器地址栏输入“tencent://message/?uin=88888888&Site=JooIT.com&Menu=yes”就会出现一个QQ对话框。
三、步骤
1、新建.reg文件,并运行
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\注册表名]
"URL Protocol"="程序路径(也可以不填)"
@="注册表名Protocol"
[HKEY_CLASSES_ROOT\注册表名\DefaultIcon]
@="程序路径,1"
[HKEY_CLASSES_ROOT\注册表名\shell]
[HKEY_CLASSES_ROOT\注册表名\shell\open]
[HKEY_CLASSES_ROOT\注册表名\shell\open\command]
@="\"程序路径\" \"%1\""
其中,@="“程序路径” “%1"”,此处的%1表示传入的参数,比如:tencent://message,解析后就可以得到参数message。
保存为WebCall.reg文件,双击导入注册表。
示例
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\call-demo]
"URL Protocol"=""
@="call-demoProtocol"
[HKEY_CLASSES_ROOT\call-demo\DefaultIcon]
@="D:\\MyDemo\\demo.exe"
[HKEY_CLASSES_ROOT\call-demo\shell]
[HKEY_CLASSES_ROOT\call-demo\shell\open]
[HKEY_CLASSES_ROOT\call-demo\shell\open\command]
@="D:\\MyDemo\\demo.exe"
2、网页上调用
<html>
<body>
<a href="call-demo://"> 打开demo客户端 </a>
</body>
</html>
3、点击网页上的打开客户端,就可以启用设定的exe应用程序啦。
四、特殊情况
一般应用程序通过上面的步骤就都能打开啦,但是我在实际操作时遇到了一个现象:electron
开发的一个客户端,直接双击exe程序可以运行,但是从网页上打开,就报错(我这里报ffi-napi
相关的错,由于当时忘记截图记录,这里就不放图了)。于是请教前辈,使用了以下方法解决:
1、新建一个.bat文件,编辑如下:
cd exe程序所在文件夹的路径
start demo.exe
示例
cd D:\MyDemo
start demo.exe
保存为lunch.bat,双击.bat可以打开demo.exe
2、在上述.reg文件中,将所有.exe程序路径替换为现在的.bat路径
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\call-demo]
"URL Protocol"=""
@="call-demoProtocol"
[HKEY_CLASSES_ROOT\call-demo\DefaultIcon]
@="D:\\MyDemo\\lunch.bat"
[HKEY_CLASSES_ROOT\call-demo\shell]
[HKEY_CLASSES_ROOT\call-demo\shell\open]
[HKEY_CLASSES_ROOT\call-demo\shell\open\command]
@="D:\\MyDemo\\lunch.bat"
接下来的步骤同上。换句话说,现在通过网页打开的其实是.bat脚本,然后通过.bat来启动.exe程序。
五、参考
1、https://blog.csdn.net/djfjkj52/article/details/104610597/
2、https://www.cnblogs.com/Jimc/p/13232106.html
3、如果想自己去注册表手动添加来加深理解,参考:https://blog.csdn.net/mr_wanter/article/details/52784958