Registering an Application to a URL Protocol

    技术2022-05-11  16

    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"  

    最新回复(0)