WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

web调用本地exe应用程序并传入参数

Posted on 2022-12-04 01:28  WebEnh  阅读(2316)  评论(0编辑  收藏  举报

1、exe创建注册表
2、web启动exe,并传真userId
3、exe取得服务器授权sig
4、web取得推流地址: 'http://v.ju918.com/live/26185_21639.m3u8'

从网页中通过自定义URL Protocol调用本地程序,需要将协议写到注册表中。
浏览器在解析到自定义URL Protocol之后,寻找注册表,通过注册表启动相应的程序并传入参数。
协议里面需要记录本地程序的路径信息。

一、HTML调用方式如下:

<a href="Micro.Live://">WebExe,启动Exe应用程序</a

二、PC端注册表格式如下:

在开始菜单处,打开“运行”工具,或者按Win+R,在“运行”输入字母“regedit”,按回车键。这是打开注册表编辑器的命令。

然后注册表编辑器就出现在桌面了,在计算下面,大家可以进行各种设置。

HKEY_CLASSES_ROOT设置注册表Micro.Live
 


 

常用的注册三种编辑注册表的方式如下

方式一、注册表文件格式如下:

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Micro.Live]
"URL Protocol"="C:\\Micro.Live.exe"
@="Micro.Live.Protocol"
[HKEY_CLASSES_ROOT\Micro.Live\DefaultIcon]
@="C:\\Micro.Live.exe,1"
[HKEY_CLASSES_ROOT\Micro.Live\shell]
[HKEY_CLASSES_ROOT\Micro.Live\shell\open]
[HKEY_CLASSES_ROOT\Micro.Live\shell\open\command]
@="\"C:\\Micro.Live.exe\"\"%1\""

复制到(txt)记事本,然后另存为Micro.Live.reg.reg文件,打开运行文件;

方式二、控制台程序(C#)如下:

1、配置文件(ProtocolInfo.xml)放到本地程序目录(Debug)

<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfProtocolInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/2001/XMLSchema-instance">
  <ProtocolInfo ProtocolName="Micro.Live.Protocol" ProgramName="Micro.Live.exe" NodeName="Micro.Live" />
</ArrayOfProtocolInfo>
2、创建控制台应用程序  
try
{
    List<ProtocolInfo> protocalInfos = ProtocolInfo.GetProtocolInfo(string.Format("{0}\\ProtocolInfo.xml", Environment.CurrentDirectory));
    if (protocalInfos == null || protocalInfos.Count == 0)
        Console.WriteLine("未获取协议的配置信息!请确保配置文件 ProtocolInfo.xml 在当前目录下。");
    string nodeName = protocalInfos[0].NodeName;
    string programFullPath = string.Format("{0}\\{1}", Environment.CurrentDirectory, nodeName);
 
    RegistryKey key = Registry.ClassesRoot;
    string a = (string)key.GetValue(nodeName, true);
    if (!key.Name.Contains(nodeName))
    {
        RegistryKey software = key.CreateSubKey(protocalInfos[0].NodeName);
        software.SetValue("URL Protocol", programFullPath);
        software.SetValue("", protocalInfos[0].ProtocolName);
 
        RegistryKey softwareDefaultIcon = software.CreateSubKey("DefaultIcon");
        softwareDefaultIcon.SetValue("", string.Format("{0},{1}", programFullPath, 1));
 
        RegistryKey softwareShell = software.CreateSubKey("shell");
        softwareShell = softwareShell.CreateSubKey("open");
        softwareShell = softwareShell.CreateSubKey("command");
        softwareShell.SetValue("", string.Format("\"{0}\" \"%{1}\"", programFullPath, 1));
    }
}
catch(Exception ex)
{
    Console.Write(ex.Message);
}

  3、如果当前用户没有管理员权限,写注册表会被拒。程序需要添加app.manifest文件

方式三、部署添加注册表(C#)如下:

注册表页面,各个节点的键值为:

键(Key) 名称(Name) 值(Value)
Micro.Live   Micro.Live.Protocol
Micro.Live URL Protocol C:\Micro.Live.exe
Micro.Live/DefaultIcon   C:\Micro.Live.exe,1
Micro.Live/shell    
Micro.Live/shell/open    
Micro.Live/shell/open/command   "C:\Micro.Live.exe""%1"

 

 右键 => New =>键 =>字符串值 => 属性窗口 => Name/Value

 

三、WPF 程序处理参数

static class Program
{
        /// <summary>
        //应用程序的主入口点。
        //</summary>
        [STAThread]
        static void Main(string[] args)
        {
            CustomApplication app = new CustomApplication();
            app.Run();
        }
}
 
class CustomApplication : Application
{
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            if (e.Args.Length > 0)
            {
                MainWindow window = new MainWindow(e.Args);
                window.Show();
            }
            else
            {
                MessageBox.Show("未传入参数!");
                Application.Current.Shutdown();
            }
        }
}

 

源代码下载地址:点击打开链接