Click here to Skip to main content
15,561,769 members
Articles / Web Development
Technical Blog
Posted 31 Jan 2017

Tagged as

Stats

22.6K views
8 bookmarked

Run an EXE from Web Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
31 Jan 2017CPOL3 min read
Recently while I was using GIT, the options in the browser on the GITHUB site, launched the application installed on my system!

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.

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 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 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.

HTML
<a href="alert:wmPing">Click to trigger</a>

And done, click to see the magic!

3

The above window comes up, asking the 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

 
QuestionWhat if Pin
Ameer Salah27-May-19 23:32
Ameer Salah27-May-19 23:32 
Questioncan we pass 2 parameters Pin
Murali Krishna KR1-May-19 21:57
Murali Krishna KR1-May-19 21:57 
QuestionProvide sample code Pin
Member 106855402-Feb-17 1:21
Member 106855402-Feb-17 1:21 
GeneralMy vote of 5 Pin
Member 123643901-Feb-17 22:56
Member 123643901-Feb-17 22:56 
GeneralRe: My vote of 5 Pin
Nyerguds8-Oct-18 22:24
Nyerguds8-Oct-18 22:24 
GeneralMy vote of 5 Pin
Member 78703451-Feb-17 3:18
professionalMember 78703451-Feb-17 3:18 
very interesting
Praisevery nice Pin
opilo@netvision.net.il31-Jan-17 22:50
opilo@netvision.net.il31-Jan-17 22:50 

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.