---
title: "如何建立自己的协议(转自MSDN)"
author: "zbw"
date: "2007-03-31T09:10:00Z"
tags: []
slug: "msdn"
draft: false
lastmod: "2007-03-31T09:10:00Z"
id: 66892
url: "https://zzz.iteye.com/blog/66892"
---

from Msdn:http://msdn.microsoft.com/library/default.asp?url=/workshop/networking/pluggable/overview/appendix\_a.asp

 

To enable an application to handle a particular URL Protocol, you must add a new key, with the appropriate keys and values, to the registry in HKEY\_CLASSES\_ROOT.

The new registry key must match the protocol scheme that is being added. For instance, to add a "note:" protocol, the key added to HKEY\_CLASSES\_ROOT should be **note**. 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. Also under the new key, a **DefaultIcon** key and a **shell** key should be added. The Default string value under 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 call the application.

The following example shows which registry values might be added to register a new application (Notepad.exe in this example) to handle the new URL protocol.

 

**HKEY\_CLASSES\_ROOT**

***note***

(Default) = "URL:Note Protocol"

URL Protocol**=** ""

**DefaultIcon**

(Default) = "notepad.exe"

**shell**

**open**

**command**

(Default) = "C:\\WINDOWS\\notepad.exe" "%1"

 

By adding these settings to the registry, any attempts to navigate to URLs such as "note:C:\\MyFile.txt" would attempt to launch Notepad to edit the file C:\\MyFile.txt. However, because the URL Protocol handler passes the complete URL string to the application registered in the command, Notepad.exe would be called with the following unanticipated command line syntax:

```
C:\WINDOWS\notepad.exe "note:C:\MyFile.txt"
```

Normally, this argument format would be handled by the application that processes the request. However, because Notepad.exe cannot be changed, the protocol prefix must be trimmed from the filename before calling Notepad. The following shell command will trim the prefix from the input string, and pass the rest to Notepad.exe:

```
@FOR /F "tokens=1* delims=:" %%a IN ("%~1") DO start notepad.exe "%%b"
```

If the preceding text were saved into a batch file, C:\\WINDOWS\\Note.cmd for example, the following registry value would correctly launch Notepad.exe with the file specified in the URL string:

 

**HKEY\_CLASSES\_ROOT**

***note***

**shell**

**open**

**command**

(Default) = "C:\\WINDOWS\\note.cmd" "%1"