HTML打开Windows本地应用程序

一、背景

有时候需要通过HTML网页打开Windows的本地应用程序,由于HTML并无提供本地接口调用方法,因此需要借助于Windows系统的注册表进行操作。

二、本地应用程序注册表注入

1、规范

需要编写.reg注册表注入文件,格式为:

Windows Registry Editor Version 5.00
;; 名称: 指定要注入到系统注册表中的标识(最好保证其唯一性), 注意: 只能由数字和字母组成
[HKEY_CLASSES_ROOT\名称]
@="URL:名称 Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\名称\DefaultIcon]
@="本地应用程序全路径"
[HKEY_CLASSES_ROOT\名称\Shell]
[HKEY_CLASSES_ROOT\名称\Shell\Open]
[HKEY_CLASSES_ROOT\名称\Shell\Open\command]
@="本地应用程序全路径 \"%1\""

2、示例

本地应用程序:C:\Program Files (x86)\test1\test1.exe。创建注册表文件,例如:test.reg

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\test1]
@="URL:test1 Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\test1\DefaultIcon]
@="C:\\Program Files (x86)\\test1\\test.exe"
[HKEY_CLASSES_ROOT\test1\Shell]
[HKEY_CLASSES_ROOT\test1\Shell\Open]
[HKEY_CLASSES_ROOT\test1\Shell\Open\command]
@="\"C:\\Program Files (x86)\\test1\\test1.exe\" \"%1"

三、HTML调用

在HTML中需要通过超链接的方式调用,例如:

<html>
	<a href="test1:" >调用test1</a>
</html>

四、注册表注入文件创建脚本

regmaker.bat

@echo off
setlocal enabledelayedexpansion

set app_name=%1
set app_file=%2

:: 参数判断
if "%app_name%"=="" goto args_error
if {
    
    %app_file%}=={
    
    } goto args_error
goto args_ok

:args_error
    echo 用法: regmaker.bat 应用名称 程序文件 1(可选, 执行注册文件)
    echo 示例: regmaker.bat test1 "C:\\Program Files (x86)\\test1\test1.exe" 1
    goto end

:args_ok
    :: 去除首尾双引号
    set "app_file=%app_file:"=%"
    :: 统一路径分隔符为\\
    set app_file=%app_file:\=\\%
    set app_file=%app_file:\\\\\\\\=\\% 
    set app_file=%app_file:\\\\\\=\\% 
    set app_file=%app_file:\\\\=\\%
    echo 应用名称: %app_name%
    echo 程序文件: %app_file%
    :: 删除已存在的注册文件
    set reg_file="%~dp0%app_name%.reg"
    if exist %reg_file% (
        del %reg_file%
    )
    :: 创建注册文件
    echo Windows Registry Editor Version 5.00 >> %reg_file%
    echo [HKEY_CLASSES_ROOT\%app_name%] >> %reg_file%
    echo @="URL:%app_name% Protocol" >> %reg_file%
    echo "URL Protocol"="" >> %reg_file%
    echo [HKEY_CLASSES_ROOT\%app_name%\DefaultIcon] >> %reg_file%
    echo @="%app_file%" >> %reg_file%
    echo [HKEY_CLASSES_ROOT\%app_name%\Shell] >> %reg_file%
    echo [HKEY_CLASSES_ROOT\%app_name%\Shell\Open] >> %reg_file%
    echo [HKEY_CLASSES_ROOT\%app_name%\Shell\Open\command] >> %reg_file%
    echo @="%app_file% \"%%1\"" >> %reg_file%
    :: 执行注册文件
    if {
    
    %3}=={
    
    1} (
        %reg_file%
    )
    
:end

猜你喜欢

转载自blog.csdn.net/hezhanran/article/details/125001270
今日推荐