views:

54

answers:

2

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

+1  A: 

At least on Windows 7, you can create a custom protocol as long as you add a URL Protocol value of type REG_SZ to the 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 explorer.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\echo]
"URL Protocol"=""
@="Echo Protocol"

[HKEY_CLASSES_ROOT\echo\shell]

[HKEY_CLASSES_ROOT\echo\shell\open]

[HKEY_CLASSES_ROOT\echo\shell\open\command]
@="C:\\WINDOWS\\SYSTEM32\\CMD.EXE /Q /C (echo %1) && pause"

I've found it will also work in the keys HKCU\Software\Classes and HKLM\Software\Classes too. It isn't listed in the Control Panel\Programs\Default Programs\Set Associations list however. Other keys might need to be updated or it would have to be registered with Windows somehow.

I imagine it is the same or similar in older versions of Windows XP and up.

Jeff M
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.
coderpros
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 M
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.
coderpros
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 M
coderpros
Ah that would explain it. Glad to see you figured it out. ;)
Jeff M
A: 

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

Garett