Registering URL Scheme

Introduction

Here is an interesting topic to share again! Recently while I was using GIT, the options in the browser on the GITHUB site launched and the application installed on my system! I wondered if it would be possible to run any exe from my web application and create wonders!

Nice story, huh? Actually, that is possible and I will be sharing the process of 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 you may be 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 and hardware devices connected. There are basically a 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 are 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.

Diving into the code

Here, what we will 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.

  1. using Microsoft.Win32;  
  2. using System;  
  3. using System.IO;  
  4. using System.Linq;  
  5. namespace WMConsole   
  6. {  
  7.     class Program   
  8.     {  
  9.         static void Main(string[] args)   
  10.         {  
  11.                 Console.WriteLine("Listening.."); //Gets the current location where the file is downloaded   
  12.                 var loc = System.Reflection.Assembly.GetExecutingAssembly().Location;   
  13.                 if (!Directory.Exists(@"D:\Console\"))   
  14.                 {   
  15.                     System.IO.Directory.CreateDirectory(@"D:\Console\");   
  16.                 } //Creates the Downloaded file in the specified folder  
  17.                 if (!File.Exists(@"D:\Console\" + loc.Split('\\').Last()))   
  18.                 {   
  19.                     File.Move(loc, @"D:\Console\" + loc.Split('\\').Last());   
  20.                 }   
  21.                 var KeyTest = Registry.CurrentUser.OpenSubKey("Software"true).OpenSubKey("Classes"true);   
  22.                 RegistryKey key = KeyTest.CreateSubKey("alert");   
  23.                 key.SetValue("URL Protocol""wnPing");   
  24.                 key.CreateSubKey(@"shell\open\command").SetValue("", @"D:\Console\WMConsole.exe %1");  
  25.         }   
  26.     }   
  27. }   
Understanding the code

In the above snippet, we are first creating a 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 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.

And done, click to see the magic,

output
The above window comes up, asking the user to Launch the application. 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.

Up Next
    Ebook Download
    View all

    testing

    Read by 48 people
    Download Now!
    Learn
    View all