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.
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.
- using Microsoft.Win32;
- using System;
- using System.IO;
- using System.Linq;
- namespace WMConsole
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Listening..");
- var loc = System.Reflection.Assembly.GetExecutingAssembly().Location;
- if (!Directory.Exists(@"D:\Console\"))
- {
- System.IO.Directory.CreateDirectory(@"D:\Console\");
- }
- 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 codeIn 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,
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.