---
title: "【转载】自定义URL Protocol 协议 "
author: "netfork"
date: "2016-07-01T15:37:47Z"
tags: []
categories: ["互联网"]
slug: "url-protocol"
draft: false
lastmod: "2016-07-01T15:40:43Z"
id: 2308606
url: "https://netfork.iteye.com/blog/2308606"
---

自定义URL Protocol 协议

一些应用软件可以在浏览器中点击一个url后自动启动，并执行操作。这是咋实现的呢？  
我在 google了许多也找个博客说明。接照这些文档我也新手试了一下。

通过网站调用可执行程序，主要是修改注册表,注册 URL Protocol 协议。  
第一步：按照如下图所示建立注册表

![](http://img.my.csdn.net/uploads/201304/11/1365664015_4012.jpg)

其中 在注册表的 \[HKEY\_CLASSES\_ROOT] 主键下 建立\[qiaoker]建。  
\[qiaoker] 此键可以自定义任意。

第二步：注册表中建立相关键值对。

![](http://img.my.csdn.net/uploads/201304/11/1365664031_7529.jpg)

 

![](http://img.my.csdn.net/uploads/201304/11/1365664042_6186.jpg)

 

![](http://img.my.csdn.net/uploads/201304/11/1365664057_3946.jpg)

注意到上面 command 项的值为 C:\\Program Files (x86)\\Qiaoker\\updater.exe "%1" ,这个"%1"是传递给 updater.exe的参数。  
使用时我们会在后面讲到如何使。

 

第三步：编写测试页面  
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd](http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd)"&gt;  
&lt; html xmlns="[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)"&gt;  
  &lt;head&gt;  
      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;  
  &lt;/head&gt;  
    &lt;body&gt;  
        &lt;div&gt;  
            &lt;a href="qiaoker://cid:110&username:xxh"&gt;  
            执行可执行文件  
            &lt;/a&gt;  
        &lt;/div&gt;  
    &lt;/body&gt;  
&lt; /html&gt;

说明：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;