Search code examples
c#windowsprotocols

How do I create a "custom protocol" and map it to an application?


How would one go about creating a "custom protocol?" I know you can create a URL protocol by adding a number of registry entries to HKEY_CLASSES_ROOT, but that seems to only work in a browser. I need for it to work in Windows Explorer also.

I know that I can write a client/server sort of interface, but I think that is overkill for my client's needs (and budget).

Long story short...

  • A third-party application should call: tbwx:<row_id>
  • My app should load and delete a record from the database.

It sounds fairly simple (or so I thought). Any ideas?

Thanks


Solution

  • You can create a custom protocol as long as you add a URL Protocol value of type REG_SZ to a class's key. It doesn't need an actual value, just needs to be present. Here's a simple example of an "Echo Protocol" I just created which works in Windows Explorer.

    Windows Registry Editor Version 5.00
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\echo]
    "URL Protocol"=""
    @="Echo Protocol"
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\echo\shell]
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\echo\shell\open]
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\echo\shell\open\command]
    @="C:\\WINDOWS\\SYSTEM32\\CMD.EXE /Q /C (echo %1) && pause"
    

    Then if you type in Windows Explorer (or Run menu) for the path:

    enter image description here

    enter image description here

    It should even work from a browser as well, you'll just need to confirm like any other protocol: enter image description here enter image description here

    It should run the command:

    enter image description here

    I've found it will also work in the keys HKCR and HKLM\Software\Classes too.