So, let’s say you’re working on a shiny new piece of software, which needs to somehow integrate with existing legacy software. The big problem is your new software is web-based, and the existing software is a desktop application.
So, you need to start or launch a desktop application from a web application. Sounds impossible at first glance, right? Well, at least for windows, which this article focuses on, it is not, and in fact, it’s rather simple.
It’s even better if the desktop application you need to launch is able to receive arguments.
But first things first, how can a browser-based app launch a desktop app. The answer is Custom Protocol Handlers.
All you need to do is to install a new custom protocol, and tell windows which application handles a new protocol.
Let’s imagine I have a desktop application which, when provided with a URL, runs some analysis on its contents and performs a sentiment analysis.
All we need to do is to create a new custom protocol, let’s call it sentiment:// .
The idea is that now, every time you enter a URL in the browser that starts with “sentiment://” the existing desktop application is launched, and any text after the protocol will be interpreted as a parameter.
Here’s how we can achieve that:
1 – the sentiment analysis application
Well, obviously, it’s not a full application, for the sake of this article, but basically, this application can take an argument, parse the URL, obtain the contents and try to get the overall content sentiment.
One thing to note, is that when using protocol handlers, the protocol itself will be part of the argument, so some additional cleaning may be necessary (notice the quick and dirty replace on line 17):
So now if I run this with an argument, I get something like the following:
2 – Registering the custom protocol handler
Ok, but so far all we have is an application which takes an argument. Big deal.
Yes, but this is what will allow us to make a small addition to the windows registry, and that’s where the magic happens. We are now going to register a custom protocol and indicate the handler application.
First, we’ll do it manually, but then we’ll talk about automating it. For now, all we need to do is opening the windows registry (type Regedit in the start menu, or run it as a command) as a system administrator.
We re going to add the following:
- Under HKEY_CLASSES_ROOT, create a new key with the same name as the protocol (in our case “sentiment”)
- Inside it, add a default new string value, with no name (Default) with the following content URL:protocol_name Protocol (in our case URL: sentiment Protocol).
- Add a new String, this time with the name “URL Protocol” and no content, so it looks like this:
- Under the protocol, key add the following keys hierarchically: shell\open\command
- inside command, add a new string, with an empty name (Default) with the location of your executable followed by %1, which represents the argument to send.
- At the end you should have something that looks like this:
And that’s it, if you open the run window and type sentiment://site.com
and press enter, your application will be launched:
You can also do it from the browser, and the browser will prompt for confirmation:
3 – Automatic custom protocol registration
Off course nobody likes to edit the registry manually. One option to automate this is to change the registry during the application installation, or letting the application do it automatically.
Here’s how this can be achieved with C#:
Another option is to create a .REG file.
More info on Protocol Handlers: https://docs.microsoft.com/en-us/windows/win32/search/-search-3x-wds-ph-install-registration
Hope this helps.