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!
- HKEY_LOCAL_MACHINE:- Setting are stored which are local or specific to the system.
- HKEY_CURRENT_CONFIG:- All the gathered run time information are contained withing 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 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.
static void Main(string args)
//Gets the current location where the file is downloaded
var loc = System.Reflection.Assembly.GetExecutingAssembly().Location;
//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:
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.
<a href="alert:wmPing">Click to trigger</a>
And done, click to see the magic!
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.
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 improvement!
Keep learning and keep sharing!