Run .exe from Html – using custom URL Protocol

Sometimes there is a business need of integrating you windows application with your web application in a manner that you can directly invoke your windows application from the web application with custom parameters.

e.g. in yahoo, you can directly open the message windows of a user from web site by clicking on “Send IM”.

run exe from javascript
 How this works?

To achieve we need to implement register a custom URL Protocol for our application. This mainly involves registry entries. Let?s take one example. Suppose we want to open our .net application with custom parameters from a web application. First of all chose a URL protocol. Let it be “Owf”. Following are the registry entries we need to ensure:

[HKEY_CLASSES_ROOT\Owf] @="URL: Owf Protocol" "URL Protocol"="" [HKEY_CLASSES_ROOT\Owf\shell] 
[HKEY_CLASSES_ROOT\Owf\shell\open] [HKEY_CLASSES_ROOT\Owf\shell\open\command] @="\"C:\\Program Files\\Owf\\OwfTest.exe\" %1"

Once the above entries are added we can safely use the protocol “Owf”.

e.g. Owf:OpenForm?id=111″>Open Owf Form [ Open Owf Form – 111 ]

Whenever above link is clicked, this will invoke our windows application stored at registry entry [HKEY_CLASSES_ROOT\Owf\shell\open\command].

Handling in .NET application

Always check whether the URL protocol is registered before at application start

public const string UrlProtocol = "Owf"; 
public static void RegisterUrlProtocol() { RegistryKey rKey = Registry.ClassesRoot.OpenSubKey(UrlProtocol, true);
 if (rKey == null)
 { rKey = Registry.ClassesRoot.CreateSubKey(UrlProtocol); rKey.SetValue("", "URL: Owf Protocol"); rKey.SetValue("URL Protocol", "");
 rKey = rKey.CreateSubKey(@"shell\open\command"); rKey.SetValue("", "\"" + Application.ExecutablePath + "\" %1"); } 
if (rKey != null) { rKey.Close(); } }

Check for URL parameters

private static bool CheckForProtocolMessage() { string[] arguments = Environment.GetCommandLineArgs(); 
if (arguments.Length > 1) 
{ // Format = "Owf:OpenForm?id=111" string[] args = arguments[1].Split(':'); 
if (args[0].Trim().ToUpper() == "Owf" && args.Length > 1)
 { // Means this is a URL protocol string[] actionDetail = args[1].Split('?');
 if (actionDetail.Length > 1) 
{ switch(actionDetail[0].Trim().ToUpper()) { case "OPENFORM": string[] details = actionDetail[1].Split('=');
 if (details.Length > 1) { string id = details[1].Trim();
 // perform desire action return true; } break; } } } } return false; }

Add above calls in Main function

[STAThread] static void Main() { 
// Register the client protocol for invoking the client
 // from a web page link RegisterUrlProtocol();

// Check protocol message

// Rest logic…

I have used this approach froim my project ( after customizing it a little bit as per my requirement) and i found it really nice.This post is  taken from OpenWinForm


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s