自定义URL Protocol 协议
一些应用软件可以在浏览器中点击一个url后自动启动,并执行操作。这是咋实现的呢? 我在 google了许多也找个博客说明。接照这些文档我也新手试了一下。
通过网站调用可执行程序,主要是修改注册表,注册 URL Protocol 协议。 第一步:按照如下图所示建立注册表
其中 在注册表的 [HKEY_CLASSES_ROOT] 主键下 建立[qiaoker]建。 [qiaoker] 此键可以自定义任意。
第二步:注册表中建立相关键值对。
注意到上面 command 项的值为 C:\Program Files (x86)\Qiaoker\updater.exe "%1" ,这个"%1"是传递给 updater.exe的参数。 使用时我们会在后面讲到如何使。
第三步:编写测试页面 <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <div> <a href="QiaokerProtocol://cid:110&username:xxh"> 执行可执行文件 </a> </div> </body> </html>
说明:QiaokerProtocol: 是注册表中注册的协议名称。 cid:110&username:xxh 是传入可执行程序的参数。 程序收到参数据自已去解析字符串中的相关信息。
其它说明: 我是用 Inno Setup IDE 生成的注册表, 在程序安装时写入注册表,卸载时删除相关注册表信息。 Inno Setup 写入注册表的代码如下: [Registry] Root: HKCR; SubKey: QiaokerProtocol; ValueData: "Qiaoker Protocol"; ValueType: string; Flags: CreateValueIfDoesntExist UninsDeleteKey; Root: HKCR; SubKey: QiaokerProtocol; ValueName: "URL Qiaoker Protocol"; Flags: CreateValueIfDoesntExist; ValueType: string; Root: HKCR; SubKey: QiaokerProtocol\DefaultIcon; ValueData: E:\Client1VNSkia2\bin\debug\updater.exe; Flags: CreateValueIfDoesntExist; ValueType: string; Root: HKCR; SubKey: QiaokerProtocol\shell\open\command; ValueData: "E:\Client1VNSkia2\bin\debug\updater.exe ""%1"""; Flags: CreateValueIfDoesntExist; ValueType: string;