如何在网页中启动桌面客户端

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/coaxhappy/article/details/8685712

在网页中使用URL Protocol可以调用桌面应用(Winform/WPF)的客户端,并传递给客户端简单的参数。

1、先在客户端中操作注册表写入下列键值

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SNBOOK]
@="Test"
"URL Protocol"="C:\Program Files (x86)\Test\\Test.exe %l"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SNBOOK\Shell]

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SNBOOK\Shell\open]

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SNBOOK\Shell\open\command]
@="C:\Program Files (x86)\Test\\Test.exe %l"

这里的1%是从网页上的Url传递给客户端的参数。

2、在网页中写入如下脚本:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>Url Protocol Test</title>
    <script type="text/javascript">
		function clientDl() {
        var ret = confirm('确定启动客户端?');
        if (ret == true) {
            window.location = "test:///DL_-1";
        } 
        return;
    };
	</script>
</head>
<body>
<br/><br/>
<a href="javascript:void(0);" onclick = "clientDl();">启动客户端</a>
</body>
</html>window.location = "test:///DL_-1";
        } 
        return;
    };
	</script>
</head>
<body>
<br/><br/>
<a href="javascript:void(0);" onclick = "clientDl();">启动客户端</a>
</body>
</html>


红字部分:“window.location = "test:///DL_-1";”中,test是自定义的Url协议名称,冒号后面的“///DL_-1”传递给客户端的参数。

3、在程序中解析参数

在Winform或者WPF客户端的启动应用程序的命令行参数列表中可以直接获取到传递来的命令“test:///DL_-1”;

猜你喜欢

转载自blog.csdn.net/coaxhappy/article/details/8685712