URL Protocol and Instancing Fun

2006-06-08 13:15:00.0

URL Protocol and Instancing Fun

As I posted here one of the applications I had to write recently as part of a bigger project was a Guarantee Certificate printing application.

People register for a guarantee certificate on the website and an email is generated with a Guarantee Certificate Number and sent to the marketing department of the specific company offering the guarantees. The person tasked with printing the certificates then took this number from the email and pasted it into my application which in turn both generated a graphical certificate and address label and sent the output of both to a colour printer and Dymo LabelWriter respectively.

Now, the problem here is the amount of time it takes to generate a certificate through this process. The copying and pasting from email to guarantee application was about 10 seconds and with about 70 signups a day, that was 700 seconds of someones time wasted coupled with the amount of time for both printers to actually print. Thats about 10 minutes alone to copy and paste.

I had to find a way of speeding it up and cut down on the monotonous task of copying and pasting.

I noticed that FeedDemon had a custom url protocol called feed:// which would launch the FeedDemon RSS reader when a feed:// link was clicked. This got me thinking about adding a custom URL Protocol called guarantee:// which would call my app and pass the guarantee number into it.

As with anything, if you know it can be done, its only a matter of time before you figure out how to do it yourself. I began to think what has to be set in Windows to allow this custom URL Protocol and thought of the registry. Doing a quick search through the registry gave me the answer.

This is what needs to be added to the registry:

My Computer\HKEY_CLASSES_ROOT\guarantee
My Computer\HKEY_CLASSES_ROOT\guarantee\(Default)=URL:Guarantee Protocol
My Computer\HKEY_CLASSES_ROOT\guarantee\URL Protocol=""
My Computer\HKEY_CLASSES_ROOT\guarantee\DefaultIcon\myApp.exe
My Computer\HKEY_CLASSES_ROOT\guarantee\shell
My Computer\HKEY_CLASSES_ROOT\guarantee\shell\open
My Computer\HKEY_CLASSES_ROOT\guarantee\shell\open\command
My Computer\HKEY_CLASSES_ROOT\guarantee\shell\open\command\(Default)="C:\Program Files\myApp\myApp.exe" "%1"

Now when someone clicks a link <a href="guarantee://234-343-34343">Certificate Number</a> my application is launched and the number is passed to it as a Command Argument

This led to another problem aside from parsing the Command$ input to get the number and strip out the URL Protocol call. My app instancing had to be corrected.

Obviously all I needed was one instance of my app running but the code to do this checked to see if my app was running and if it was killed the second instance of the app and using the windows handle of the previous app, activated it. When you do this though, you have to pass the command$ from the second app to the previous instance and call the load certificate function.

After reading and testing and reading and testing, the code was getting messy with alot of Windows API calls and dealing with Mutex calls and sub-classing. I was under pressure with time and just needed a quick and dirty solution and so used a simple .ini file as storage so the previous app would know what certificate to launch.

End result? Works flawlessly....

Moral of the post?
A quick an dirty hack, although not the correct way of doing things, sometimes works out more efficient time wise than re-writing a big part of the application if it achieves the goal you set out to achieve.

Add to your del.icio.us    DIGG This!    Technorati Cosmos Link    Post to Reddit    Add to your Furl    Add to Blinklist
Comments [1] - Leave a comment

Comments

msk said:
Hi, Thanks for your post.

I tried to do similar like this

I followed your instructions but nothing happens. What are the machine requirements ? and Is there any setting changes required in IE ?

Can you please help me to sort-out this ?
Thanks
MSK