10

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

Brandon Osborne
  • 835
  • 1
  • 12
  • 26

2 Answers2

16

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.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • That was exactly as far as I got. That works just fine in IE, but doesn't work at all in Windows Explorer. I want to be able to simply call my protocol (and have it in turn call my app) without having to call it through IE. – Brandon Osborne Oct 19 '10 at 05:06
  • I don't know what to say then. "It works on my machine." (Windows 7 Pro x64) Are you running this on 7 too? Does the above work for you? – Jeff Mercado Oct 19 '10 at 06:16
  • Yup. I'm using 7 Ultimate x64. How exactly are you calling echo? The reason I ask is because echo is actually a DOS command, so it would return something if typed into the cmd prompt or even when you type into the start menu (depending on how your %PATH% var is set up). You prolly already knew this, but I wanted to double check because I've been beating my head against the wall half of the night. – Brandon Osborne Oct 19 '10 at 09:59
  • With the "Echo Protocol" installed, I just went into the explorer and typed `echo:foo` and it prints out `echo:foo` and pauses. I called `echo` through `cmd.exe` instead since I couldn't call it directly. Also I was originally testing the "Txt Protocol" which was supposed to open a file in notepad. It didn't work because it always leaves the protocol name in the argument and I didn't want to deal with the string manipulation to open the file. – Jeff Mercado Oct 19 '10 at 19:51
  • Ahh...one can never be too sure. I was just making sure you weren't forgetting to include the colon after echo. Some "genius" on another site was swearing up & down you don't need it. lol. At any rate, you were right, it works just fine, just as it did when I started coding two three days ago. Google Desktop was intercepting what I typed, didn't recognize it & popped up a search box. :| I tried it on another computer w/o Google Desktop on it and had no problems. – Brandon Osborne Oct 21 '10 at 10:18
  • Ah that would explain it. Glad to see you figured it out. ;) – Jeff Mercado Oct 21 '10 at 18:02
5

The Registering an Application to a URL Protocol article details the process. There is a utility on GitHub that can be used to register custom URL protocols. The source code is provided.

Garett
  • 16,632
  • 5
  • 55
  • 63