在此,简单地记录一下如何实现从浏览器网页中启动桌面应用程序,以备查阅。
我们可以分三步实现:
1. 编写一个简单的桌面应用程序,我们可以把它命名为AgentApp。
2. 把AgentApp的唤起协议注册到注册表中,这样它就能被其他应用程序唤起。
3. 开发一个简单的网页,用来唤起AgentApp。
AgentApp
AgentApp是一个简单的桌面应用程序,它能接收两个整数,把两个整数相加并输出结果。它是个简单的桌面程序而已,我们几乎可以用任何自己喜欢的编程语言开发出来。这是用C#写的示例:
using System;
namespace AgentApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\nI am an agent application.");
int sum = 0;
Console.WriteLine("\nHere are the arguments that I recieved:\n");
foreach (var arg in args)
{
Uri uri = new Uri(arg);
var decodeArgument = System.Web.HttpUtility.UrlDecode(arg);
var splitArgs = decodeArgument.Split();
foreach (var splitArg in splitArgs)
{
int number = 0;
if (int.TryParse(splitArg, out number))
{
sum += number;
Console.WriteLine(" {0}", splitArg);
}
}
}
Console.WriteLine("\nHere is the result that I calculated from the arguments:\n\n {0}", sum);
Console.ReadLine();
}
}
}
为AgentApp注册唤起协议
运行以下注册表脚本(RegisterAgentApp.reg)即可:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\AgentApp]
"URL Protocol"=""
@="AgentApp"
[HKEY_CLASSES_ROOT\AgentApp\DefaultIcon]
@="C:\\path\\to\\your\\AgentApp.exe,1"
[HKEY_CLASSES_ROOT\AgentApp\shell]
[HKEY_CLASSES_ROOT\AgentApp\shell\open]
[HKEY_CLASSES_ROOT\AgentApp\shell\open\command]
@="\"C:\\path\\to\\your\\AgentApp.exe\" \"%1\""
网页端测试程序
我们可以用一个简单超链接去唤起我们的AgentApp。这是一个简单的示例:
<html>
<head>
<title>
Agent App Tester
</title>
</head>
<body bgcolor="white">
<h1 align="center">
<a href="AgentApp:\\www.company.com 2 3">
<font>Launch agent app with 2 and 3</font>
</a>
</h1>
</body>
</html>
这是测试效果: