WEB程序调用客户端程序

最近一个项目中要点击WEB页面上的链接启动自己编写的程序,而且还要接收参数,google了1.5小时,终于初步试验通过了。

尝试google了:web send message windows form, bs call cs program, custom protocol...多个关键字组合,发现这种技术叫

registered URL protocol,在这篇文章里介绍得比较详细:

http://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx

 

1)首先写一个测试程序:

using System;
using System.Collections.Generic;
using System.Text;

namespace Alert
{
  class Program
  {
    static string ProcessInput(string s)
    {
       // TODO Verify and validate the input 
       // string as appropriate for your application.
       return s;
    }

    static void Main(string[] args)
    {
      Console.WriteLine("Alert.exe invoked with the following parameters.\r\n");
      Console.WriteLine("Raw command-line: \n\t" + Environment.CommandLine);

      Console.WriteLine("\n\nArguments:\n");
      foreach (string s in args)
      {
        Console.WriteLine("\t" + ProcessInput(s));
      }
      Console.WriteLine("\nPress any key to continue...");
      Console.ReadKey();
    }
  }
}

我把程序编译成edss.exe

2)用notepad编辑一个文件,改名为edss.reg

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\EDSS]
@="URL:EDSS Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\EDSS\DefaultIcon]
@="\"D:\\alert\\edss.exe\""

[HKEY_CLASSES_ROOT\EDSS\shell]

[HKEY_CLASSES_ROOT\EDSS\shell\open]

[HKEY_CLASSES_ROOT\EDSS\shell\open\command]
@="\"d:\\alert\\edss.exe\" \"%1\""

运行edss.reg后,总是提示有些注册表项写入不成功,折腾了半天,看了http等协议的定义,最后终于发现是360在干扰。

关闭360安全卫士,注册表终于写入成功了!

原来是360安全卫士阻止最后一个注册表项的写入:

[HKEY_CLASSES_ROOT\EDSS\shell\open\command]
@="\"d:\\alert\\edss.exe\" \"%1\""

3)在IE中输入edss://hello,ie浏览器弹击一个安全警告窗口,确认后就正常启动了我的应用程序

4)在chrome中试了一下不成功,后来发现在chrome中不能直接输入edss://hello来启动,必须写一个html页面。

马上编写了一行html页面:<a href='edss://hello'> start my windows program </a>

chrome也可以启动我的windows程序了!

其它浏览器以后再试。

posted @ 2014-06-02 22:18  申龙斌的程序人生  阅读(2180)  评论(2编辑  收藏  举报