[翻译] 将应用程序注册为URL协议

The About Asynchronous Pluggable Protocols article describes how to develop handlers for new protocols. In some cases, it may be desirable to invoke another application to handle a custom protocol. To do so, register the existing application as a URL Protocol handler. Once the application has successfully launched, it can use command-line parameters to retrieve the URL that launched it.
文章《About Asynchronous Pluggable Protocols》(中译[翻译]关于“异步可插协议”(About Asynchronous Pluggable Protocols(APPs)))描述了如何为一个新的协议开发处理程序(处理器handlers)。在一些案例中,可能会描述如何调用另外一个应用程序来处理自定义协议(custom protocol)。注册已经存在的应用程序为一个URL协议处理器即可。一旦应用程序被成功地启动,我们可以使用命令行参数来重新找回URL来启动它。

·Registering the Application Handling the Custom Protocol
·Launching the Handling Application
·Example
·Related Topics
·注册应用程序来处理自定义协议
·启动处理程序
·示例
·相关主题

Registering the Application Handling the Custom Protocol
注册应用程序来处理自定义协议


To enable an application to handle a particular URL protocol, you must add a new key, along with the appropriate keys and values, to HKEY_CLASSES_ROOT.
你必须添加一个新的key以及相关的value到HKEY_CLASSES_ROOT中,来使应用程序可以处理特殊的URL协议。

The new registry key must match the protocol scheme that is being added. For instance, to add an "alert:" protocol, the key added to HKEY_CLASSES_ROOT should be alert. Under this new key, the Default string value should be the display name of the new protocol, and the URL Protocol string value should contain either protocol-specific information or an empty string. Keys should also be added for DefaultIcon and shell.
新注册的key必须与协议scheme相匹配才可以被添加。例如,增加一个“alert:”协议,被增加到HKEY_CLASSES_ROOT的key必须是alert。在这个新的key之下,默认的字符串value将显示新协议的名字,并且URL协议字符串value将包含协议特有的信息或者空字符串。Keys将同样被添加到DefaultIcon和shell中。

The Default string value of the DefaultIcon key must be the file name to use as an icon for this new URL protocol.
默认的DefaultIcon key的字符串value必须是新URL协议图标文件名的路径。(译注1:DefaultIcon key)

Under the shell key, a key using a verb (such as open) should be added. A command key and a DDEEXEC key may also be added under the key using a verb. The values under the command and DDEEXEC keys are used to invoke (or launch) the application handling the new protocol.
在shell key之下,一个key使用一个动词(就像open)将被添加。一个command(命令) key和一个DDEEXEC(动态数据交换执行) key都是使用动词来添加的。这command和DDEEXEC keys之后的values都是用来调用(或者启动)处理新协议的应用程序。

Launching the Handling Application
启动处理程序


When a user clicks a link registered to your custom URL protocol, Windows Internet Explorer launches the registered URL protocol handler. If the specified shellopen command specified in the Registry contains a %1 parameter, Internet Explorer passes the URI to the registered protocol handler. The final Uniform Resource Identifier (URI) is decoded; that is, hexadecimal escape characters are converted to equivalent UTF-16 characters. For example, the %20 character sequence is replaced with a space.
当一个用户点击一个注册了你的自定义URL协议的链接后,Windows Internet Explorer(IE)启动注册的URL协议的处理器。如果指定shellopen命令在注册表中包含一个%1参数的话,Internet Explorer传递这个URI给注册协议的处理器。这最后的统一资源标识符(URI)被编码(%1);即16进制换码符被转换为等价的UTF-16字符。例如,用%20字符串取代空格。

Security Alert  Applications handling URL protocols must be robust in the face of malicious data. Because handler applications receive data from untrusted sources, the URL and other parameter values passed to the application may contain malicious data attempting to exploit the handling application. For this reason, handling applications that could initiate unwanted actions based on external data must first confirm those actions with the user.
安全警示:应用程序处理URL协议必须全力面对恶意数据。因为处理程序接收来自不信任源的数据,URL和其它参数值传递给应用程序可能包含的恶意数据企图使用处理程序。因此,处理程序可以首先启动基于外部数据的空闲行为确认这些行为以及它们的用户。


Note  In addition, handling applications should robustly handle URLs that are overly long or contain unexpected (or undesirable) character sequences. For more information, please see Writing Secure Code .
注意:另外,处理程序将要有能力处理URLs有可能太长或者包含意想不到(或者多余的)字符串。更多信息,请参看《Writing Secure Code


Example
示例


The following example shows how to register an application, alert.exe in this case, to handle an alert protocol.
接下来的例子演示如何注册alert.exe应用程序,来处理alert协议。

引用内容 引用内容
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\alert]
@="URL:Alert Protocol"
"URL Protocol"="C:\Program Files\Alert\alert.exe"

[HKEY_CLASSES_ROOT\alert\DefaultIcon]
@="C:\Program Files\Alert\alert.exe,1"

[HKEY_CLASSES_ROOT\alert\shell]

[HKEY_CLASSES_ROOT\alert\shell\open]

[HKEY_CLASSES_ROOT\alert\shell\open\command]
@="C:\Program Files\Alert\alert.exe" "%1"

By adding these settings to the registry, attempts to navigate to URLs such as "alert:Hello%20World" would attempt to launch alert.exe and pass Hello World in the command-line.
增加这些设置信息到注册表,尝试导航到像“alert:Hello%20World”这样的URLs中,将会尝试启动alert.exe程序并且在命令行中传递“Hello World”。

The following sample code contains a simple C# console application demonstrating one way to implement a protocol handler for the alert protocol:
下面的代码包含了一个简单的C#控制台应用程序演示了一种实现alert协议处理程序的方式:

using System;
using System.Collections.Generic;
using System.Text;

namespace Alert1
{
  class Program
  {
    static string ProcessInput(string s)
    {
       // TODO Verify and validate the input
       // string as appropriate for your application.
       return s;
    }

    static void Main(string[] args)
    {
      Console.WriteLine("Alert.exe invoked with the following parameters.\r\n");
      Console.WriteLine("Raw command-line: \n\t" + Environment.CommandLine);

      Console.WriteLine("\n\nArguments:\n");
      foreach (string s in args)
      {
        Console.WriteLine("\t" + ProcessInput(s));
      }
      Console.WriteLine("\nPress any key to continue...");
      Console.ReadKey();
    }
  }
}

Related Topics
相关主题


·About Asynchronous Pluggable Protocols
·Debugging Tips

控制台和Windows应用程序示例:点击下载

英文原文:http://msdn2.microsoft.com/en-us/library/aa767914.aspx
中文翻译:http://www.cnblogs.com/volnet/archive/2008/03/26/Registering_an_Application_to_a_URL_Protocol.html


2008-10-31:Visual Basic6.0中获取"%1"的方法

Private Sub Form_Load()
    Dim tmpMessage As String
    tmpMessage = Interaction.Command()
    Label1.Caption = tmpMessage
End Sub


上一篇: QQ不能安装,双击QQ安装程序毫无反应
下一篇: Register protocol
文章来自: 网络
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
评论: 0 | 引用: 0 | 查看次数: 8071
发表评论
登录后再发表评论!