How to run a desktop application from a web page!

Before I start talking about the content of this post, I am going to draw your attention into two points:

  1. I have tested this method on Windows 8.0 and 8.1. I think this should also work on older versions of this operating system but I do not have not a clue about similar technics for other operating systems such as Linus and Mac. If you tried this out, let us know about the result in comments.
  2. Why on earth do we need to run a desktop app from a page?! Well, if you are developing a web app for public or normal users (which is probably 99% of the times), you may never have such kind of requirements. But if you are developing a website for a specific business or organization, you may get such requirement. For instance, recently I received a requirement that we need to send the data directly from a web page to the printer. Obviously this is not possible by default and you should always ask internet browser to print the data. Users have to open the print dialog and press the print button. Another example is if you want to show list of the installed printers on the client's machine in a combobox in the web page! For such kind of requirements, the web page should communicate with client's windows API which is not allowed by browsers for security reasons. As you see, these are not regular needs but still they exist!
  3. You can download the sourcecode of this post at the bottom of the page.

Generally, There are two ways of accomplishing such kind of requirement tasks. The first one is to employ third-party browser plugins like Adobe Flash, Java Applet, ActiveX and so on, which pretty much all of them are deprecated (or getting deprecated) and off the table. The second way is what we are going to discuss in this post.

The method we are going to discuss is fortunately supported by all modern browsers. To do so, we need to define a so called Custom URL Protocol on client's machine. In fact Custom URL Protocol is a key in the Windows registry. If you would like to know more about it, you may check out Custom URL Protocol for Invoking Application post in CodeProject (we are using the tool from this article to define our custom protocol).

First you should download CustomURLProtocol.exe tools (from the mentioned article or this post - about 50 kb!) and run it on client's machine.

Now you will see this dialog:

You may now fill out the empty fields. Protocol name is your unique identifier (for example MyCompanyProtocol). Company name (optional) and the full path of your desktop app on the clients machine. Then press Create button to create your key in Windows registry.

Now to see what you have already done, open Windows registry and search for you protocol name (in this case MyCompanyProtocol).

Here are two images from my Windows registry from MyCompanyProtocol and "command" nodes.

Pay attention to the Data columns. In the first image you can see the Protocol which we have defined and in the second image you may see the full path of our desktop app.

It's time to test your page. Put the following like in your page:

<a href="MyCompanyProtocol:" >Run Desktop App</a>

As you see, this is a regular anchor tag in html with having MyCompanyProtocol: in its href attribute.

Our desktop app is a simple console application with a couple of lines of code:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hey I am up and running!");
        Console.ReadKey();
    }
}

Click on the link of the page the see what will happen. You will see a warning dialog from browser like this (image from Chrome, IE and Firefox):

Accept the dialog and you will see your app up and running.

That's it. You have run your desktop app from the web page.

If you need to send some parameters to your desktop app, you need to append the parameters in the link url.

<a href="MyCompanyProtocol: commandtype=getPrinterList" >Run Desktop App</a>

Now we change the code in the console app as follow:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(args[0]);
        Console.ReadKey();
    }
}

Now refresh the page and click on the link, first you will see the browser warning dialog again and then this result:

You need to parse the string and get your data. Worth to note here, you may use Command Line Parser Library to simplify the process of parsing data.

To not to having user to see the browser warning every time they want to run the app, you may ask them to select the checkbox to not to see that dialog again and again.

The last thing I want to point out here is how to run your app after the page is loaded and without having users to click on any link or button. To do so, you need to add an iframe to the page, then add your url to the src attribute of the iframe via JavaScript.

 Download the sourcecode from here:  Download

9 Comments

  • Exactly what I have been looking for, Thanks.

  • Is there a way to pass parameter or file in the other side ? I mean from desktop app to web app ? And what should I use to do so ?

  • Hello Morteza Sahragard,
    I want to run a desktop application from a web page with parameters.
    For example start a remote-desktop-session (rdp) with IP, username and Password.
    How secure is that, how can I do it and are there other/safer possibilities?
    Thanks

  • Not Working for Java Enterprise

  • Sorry, Works Perfectly even in JEE7, I love u so much babe!!!

  • Hello Morteza
    I would like to start RealVNC viewer (vncviewer.exe) parsing a CI name as variable. Do you have any idea how I can do this using this method?

    Thank you very much for any help

    Regards
    Mike

    ---------------------------------------
    @Morteza:
    What I described in this article was how to run your desktop app and send parameters along. What you should do is to have your desktop app and get it running from the page. When your desktop app is executed, you are free to do whatever you wish.

    You should figure out how to do your business by using your desktop app.

  • I got side by side error while running CustomURLProtocol.exe . Installing Microsoft Visual C++ 2008 Service Pack 1 Redistributable Package ATL Security Update worked !

  • Can run exe on browser over internet? I mean different locations different pc's. Not same network.

    ----------------------
    @Morteza: The whole article is about running an exe over a browser in the internet.

  • Thanks!

Comments have been disabled for this content.