|
Introduction
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".
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. <a href="Owf:OpenForm?id=111">Open Owf Form</a> [ 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
CheckForProtocolMessage();
// Rest logic...
//...
}
|