Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Build Simple Asynchronous Pluggable Protocols for Windows

You're not limited to the familiar asynchronous pluggable protocols (APPs) such as ftp://, http://, and javascript:// that are built into Windows; you can implement your own protocols, add them to Windows' recognized protocol list, and then use them from any application—and you can implement your protocol in any language. This article uses the Rebol scripting language to create a protocol that works similarly to the javascript:// protocol, compiling and evaluating arbitrary Rebol statements and displaying the results.


advertisement
very protocol—whether familiar (such as http, mailto, ftp, and about) or less familiar (such as JavaScript, VBScript, Outlook, NNTP, news, snews, and so forth)—is implemented on Windows machines as an asynchronous pluggable protocol (APP). These protocols can plug directly into Windows' existing protocol framework. The only common protocol not implemented as an APP (at least on my computer) is SMTP. You can call any of these protocols from any interface in Windows that allows protocol interaction, regardless of whether the interface is programmatic or graphical.

To prove it, go to the Run command on your Start button and write the following:

javascript: alert("hello world");



This should initiate whichever application is associated with the JavaScript protocol on your system, probably Internet Explorer, and execute the script. This is the same principle involved in bookmarklets. A bookmarklet is defined as "a tiny program (a JavaScript application) contained in a bookmark (the URL is a "javascript:" URL) which can be saved and used the same way you use normal bookmarks."

You can use the JavaScript protocol from other browsers such as Mozilla, as Mozilla does not start up Internet Explorer when someone writes the code above in its address bar. Mozilla will pass any APP that it does not handle natively to the proper system handler. As I showed above, you can even use the JavaScript protocol from the Run box under your Start menu.

What You Need
Windows 98 or later with IE 4 or later, Wscript, Rebol/View, and a text editor.

MSDN provides some good information about APPs, but be wary of what you read: You may come away with a misconception about what is required to achieve the most basic implementation of an APP under Windows. For example, one article implies that to write an APP one needs to implement certain interfaces, but APPs are actually very flexible. The basic implementation of an APP under Windows requires only that you make the proper entries in the registry for your protocol and that the handler for your protocol is a valid Windows .exe file. You can see an example at http://msdn.microsoft.com/library/default.asp?url=/workshop/networking/pluggable/overview/appendix_a.asp. The "note" protocol defined in that example opens Notepad, similar to the way the view-source:// protocol can be used to open the .htm source of an HTML file in Notepad. For example, try putting this in your browser: view-source:http://www.devx.com.

I doubt that Notepad implements iinternetprotocol or any related interfaces, as to do so it should be a COM implementation and found at the following registry address HKEY_CLASSES_ROOT\PROTOCOLS. Without these related interfaces, in the end all that happens is the protocol address is passed as a string via the command line to your application for analysis.

The W3C provides an incomplete list of addressing schemes, which you can use to search your system to see if these addressing schemes are implemented as APPs. Those that do more than just call over the command line should be found at the HKEY_CLASSES_ROOT\PROTOCOLS\Handler registry key.

There are many tools that allow you to dynamically load files via the command line, and pass on command lines to files. One example is script interpreters such as Wscript.exe. Here is the sample registry file, which shows how to set a simple WScript as the handler for a protocol called ws-proto.

Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\ws-proto] @="\"URL: ws-proto Protocol\"" "URL Protocol"="" [HKEY_CLASSES_ROOT\ws-proto\shell] [HKEY_CLASSES_ROOT\ws-proto\shell\open] [HKEY_CLASSES_ROOT\ws-proto\shell\open\command] @="wscript.exe c:\\wsproto.js %1"

Here is the protocol that handles the WScript. (Both the registry file and the protocol handler are available in the code download for this article—see left column).

WScript.Echo(WScript.Arguments(0));

Figure 1: Howdy. As you can see the first command line argument passed on to our Wscript is everything you wrote in your address bar.
To test this handler you can either make a new text file called settings.reg, copy the registry settings there, and then click to add the settings to the registry or use the settings.reg file in the code download. You should save the protocol handler code in c:\ in a file called wsproto.js. Once you've done this you can interact with the protocol in the same way you would with any other protocol. As an example, open your preferred browser and write the following: ws-proto://howdy. The result are shown in Figure 1.


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap