Click here to Skip to main content
15,885,309 members
Articles / .NET
Tip/Trick

Register URL Scheme

Rate me:
Please Sign up or sign in to vote.
3.91/5 (6 votes)
1 Jun 2016CPOL3 min read 17.4K   4   3
This article covers how to register URL schemes in Windows registry and run an EXE from web app.

Introduction

An interesting topic to share again! Recently, while I was using GIT, the options in the browser on the GITHUB site, launched the application installed on my system! I wondered if that is possible I can run any EXE from my web application and create wonders! Nice story, huh! Actually, that is possible and I will be sharing the process how we can achieve the same from code. I will be using Windows registry to register my EXE to be pinged from any web application. Now wondering what is Windows Registry!

The Windows Registry is a hierarchical database that stores low-level settings for the Microsoft Windows operating system and for applications that opt to use the Registry..Wiki

Now yes, Windows registry is an important section of the Windows operating system having all the low level settings, but very essential for the applications to run on the OS. Windows registry is basically used to store all the important information for the software programs, hardware devices connected. There are basically few root keys pre-defined under the registry belt!

1 2

They are:

  • HKEY_LOCAL_MACHINE: Settings are stored which are local or specific to the system.
  • HKEY_CURRENT_CONFIG: All the gathered run time information is contained within this key. This key is never in the memory, but generated at the boot time process.
  • HKEY_CURRENT_USER: Settings that are valid/applicable to the current logged in user on the system.
  • HKEY_USERS: All sub keys corresponding to the current user are stored.
  • HKEY_CLASSES_ROOT: Information about any registered application on the system is contained.

Each of the above root keys have sub-keys attached to them. For example: HKEY_CURRENT_USER\Software\Classes\ means 'Classes' is the sub key of the 'Software' sub key under the 'HKEY_CURRENT_USER' root key. We will be creating a key inside the HKEY_CURRENT_USER in order to ping the EXE. We have chosen this root key as that would not require any Admin access to add/edit any key inside this root. So the EXE can be pinged by any user. <!--more-->

Diving Into the Code!

Here, what we would be doing is, we will be creating one console application(.exe), which will act as a medium to add a registry sub key to the "HKEY_CURRENT_USER/Software/Classes". The same console application will be used to be executed/run from the web application. alert is the key command name which is used for this purpose.

C#
using Microsoft.Win32;
using System;
using System.IO;
using System.Linq;

namespace WMConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Listening..");
            
            //Gets the current location where the file is downloaded
            var loc = System.Reflection.Assembly.GetExecutingAssembly().Location;
            if (!Directory.Exists(@"D:\Console\"))
            {
                System.IO.Directory.CreateDirectory(@"D:\Console\");
            }
            //Creates the Downloaded file in the specified folder
            if (!File.Exists(@"D:\Console\" + loc.Split('\\').Last()))
            {
                File.Move(loc, @"D:\Console\" + loc.Split('\\').Last());
            }
            var KeyTest = Registry.CurrentUser.OpenSubKey("Software", true).OpenSubKey("Classes", true);
            RegistryKey key = KeyTest.CreateSubKey("alert");
            key.SetValue("URL Protocol", "wnPing");
            key.CreateSubKey(@"shell\open\command").SetValue("", @"D:\Console\WMConsole.exe %1");
        }                
    }
}

Understanding the Code!

In the above snippet, we are first creating folder where the same EXE will be downloaded from the web application and stored. The path is being dynamically generated and used.

  • Then, we open the existing root key of the registry, i.e., HKEY_CURRENT_USER through the Registry class field "CurrentUser" and the "OpenSubKey("Software", true)", opens the instance of the sub-key or allows the write access to it. Same with the sub-key "Classes" which lies under the Software key.
  • Then, we are creating a new key using the "CreateSubKey("alert")" command, with the name "alert".
  • Then we set the value to the sub key "alert", as "URL protocol" and the name to be used in the anchor tag in web application as "wnPing". Subkey alert created also gets a subkey added to it in the following hierarchy: shell->open->command and to this, the value is set as the path to the EXE file, which will run.

Once the registry is created, we add the below anchor tag for the EXE application to be triggered from any web.

Click to trigger

And done, click to see the magic!

3

The above window comes up, asking user to Launch the application. Done!! You can always click the check box to remember the action, if you want the EXE to run in the background without prompting.

Conclusion

This is it. I hope you find this topic interesting and try the same. This can be helpful in any web application when you are dealing with any hardware components attached to local client systems and your server needs input from them. Please share your thoughts and views on any improvements! Keep learning and keep sharing!

This article was originally posted at http://surajpassion.in/run-an-exe-from-web-application

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionEXE always open IE Pin
Member 133458157-Aug-17 23:52
Member 133458157-Aug-17 23:52 
QuestionURL protocols Pin
Mr. xieguigang 谢桂纲2-Jun-16 12:53
professionalMr. xieguigang 谢桂纲2-Jun-16 12:53 
Here is my code for implements the URL protocols in VisualBasic
VisualBasic_AppFramework/WindowsServices.vb at master · xieguigang/VisualBasic_AppFramework · GitHub[^]
VB
''' <summary>
''' (**** Please notice, that the application has To have admin privileges To be able To write the needed stuff into registry. ****)
'''
''' Everyone knows HTTP-URLs. Windows Shell also enables to define own URL protocols.
''' Some programs (like Visual Studio Help ms-help:// ... or Steam steam:// ...) take advantage of this feature.
''' By creating some registry entries one is able to set up a self-made URL protocol.
''' This allows to access your applications by URL (originating from every software).
'''
''' Please notice, that the application has To have admin privileges To be able To write the needed stuff into registry.
''' You can test your application very easy by opening Windows Explorer And typing "yoururlprotocol://testdata"
''' into the path/address field.
'''
''' Registers an user defined URL protocol for the usage with
''' the Windows Shell, the Internet Explorer and Office.
'''
''' Example for an URL of an user defined URL protocol:
'''
'''   rainbird://RemoteControl/OpenFridge/GetBeer
''' </summary>
''' <param name="protocolName">
''' Name of the protocol (e.g. "rainbird" for "rainbird://...")
''' </param>
''' <param name="applicationPath">
''' Complete file system path to the EXE file, which processes the URL being called (the complete URL is handed over as a Command Line Parameter).
''' </param>
''' <param name="description">
''' Description (e.g. "URL:Rainbird Custom URL")
''' </param>
Public Sub RegisterURLProtocol(protocolName As String, applicationPath As String, description As String)
    ' Create new key for desired URL protocol
    Dim protocol As RegistryKey = Registry.ClassesRoot.CreateSubKey(protocolName)

    ' Assign protocol
    Call protocol.SetValue(Nothing, description)
    Call protocol.SetValue("URL Protocol", String.Empty)

    ' Register Shell values
    Call Registry.ClassesRoot.CreateSubKey(protocolName & "\Shell")
    Call Registry.ClassesRoot.CreateSubKey(protocolName & "\Shell\open")
    protocol = Registry.ClassesRoot.CreateSubKey(protocolName & "\Shell\open\command")

    ' Specify application handling the URL protocol
    Call protocol.SetValue(Nothing, """" & applicationPath, +""" %1")
End Sub

AnswerRe: URL protocols Pin
Passion4Code2-Jun-16 21:57
professionalPassion4Code2-Jun-16 21:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.