Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Content Extensions
Pluggable Protocols
Overview/Tutorials
 Registering an Application to a URL...

  Switch on low bandwidth view
Registering an Application to a URL Protocol

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.

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.

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.

The Default string value of the DefaultIcon key must be the file name to use as an icon for this new URL protocol.

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.

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.

security note 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.
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.

Example

The following example shows how to register an application, alert.exe in this case, to handle an alert protocol.

HKEY_CLASSES_ROOT
     alert
          (Default) = "URL:Alert Protocol"
          URL Protocol = ""
          DefaultIcon
               (Default) = "alert.exe"
          shell
               open
                    command
                         (Default) = "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.

The following sample code contains a simple C# console application demonstrating one way to implement a protocol handler for the alert protocol

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
URL decoding      Dee ... Stanley Roark   |   Edit   |   Show History
Contrary to what this article says, on at least Windows XP SP3 with IE8b1, URLs are not decoded and are passed direct to the URL handler unchanged, including the protocol

With the alert: example above, the final command executed will be:
"C:\Program Files\Alert\alert.exe" "alert:Hello%20World"
(note the double space between the two parts)
Tool for regestering URL protocols      kallelundberg ... Stanley Roark   |   Edit   |   Show History

I have built a small tool for regestering URL protocols: http://www.codeplex.com/CustomURL


This tool will help you register URL protocols and lauch a specific application when a URL is executed. This tool is free and open source.

Tags What's this?: Add a tag
Flag as ContentBug
Blank IE Window      Ian Horwill ... ThomasM2   |   Edit   |   Show History

This works great except that an IE window is left open showing the URI that was invoked. Is there any way (e.g. a value in the registry, EditFlags perhaps) to make this window close or not appear in the first place?

=> The problem with the blank IE window may be related to the zone where the page that hosts the link is located. If you test a html page form the file system (file://... ) try to host the page in IIS instead (so that it is http://...) - this worked for me.

Tags What's this?: Add a tag
Flag as ContentBug
Internet Explorer 6 Return an Error      MiguelGuzman ... Thomas Lee   |   Edit   |   Show History

So far this works great on IE7 using the "command" key, however for some reason it does not work in IE6. I'm getting an "Invalid Systax Error" when I click on the link from within the browser. Everything works great if I type the URL on the "START > Run..." window. I'm not sure if the "DDEEXEC" key is required by IE6 though. If so, please can you provide an example?

Tags What's this?: ddeexec (x) error (x) ie6 (x) ie7 (x) Add a tag
Flag as ContentBug
Not working in Vista SP1      aggieben ... Thomas Lee   |   Edit   |   Show History

I'm finding that the handling application in my mailto/shell/open/command key isn't being invoked at all. How do I debug that?

[tfl - 18 05 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at

http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&

Tags What's this?: Add a tag
Flag as ContentBug
Operating Folder      OtaconSamy ... Thomas Lee   |   Edit   |   Show History

Is there a way to set a Folder where the file should operate?


[tfl - 18 05 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at

http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker