利用URL protocol在网页打开本地exe

本文介绍如何通过注册表设置在Windows系统中为应用程序注册自定义的URI处理方案,包括创建注册表键值、指定默认图标及打开命令等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Registering the Application Handling the Custom URI Scheme

 

To register an application to handle a particular URI scheme, add a new key, along with the appropriate subkeys and values, to HKEY_CLASSES_ROOT. The root key must match the URI scheme that is being added. For instance, to add an "alert:" scheme, add an alert key to HKEY_CLASSES_ROOT, as follows:

 

 

 

HKEY_CLASSES_ROOT
   alert
      URL Protocol = ""

 

Under this new key, the URL Protocol string value indicates that this key declares a custom pluggable protocol handler. Without this key, the handler application will not launch. The value should be an empty string.

 

Keys should also be added for DefaultIcon and shell. The Default string value of the DefaultIcon key must be the file name to use as an icon for this new URI scheme. The string takes the form "path, iconindex" with a maximum length of MAX_PATH. The name of the first key under the shell key should be an action verb, such as open. Under this key, a command key or a DDEEXEC key indicate how the handler should be invoked. The values under the command and DDEEXEC keys describe how to launch the application handling the new protocol.

 

Finally, the Default string value should contain the display name of the new URI scheme. The following example shows how to register an application, alert.exe in this case, to handle the alert scheme.

 

 

 

HKEY_CLASSES_ROOT
   alert
      (Default) = "URL:Alert Protocol"
      URL Protocol = ""
      DefaultIcon
         (Default) = "alert.exe,1"
      shell
         open
            command
               (Default) = "C:\Program Files\Alert\alert.exe" "%1"

 

When a user clicks a link containing your custom URI scheme, Windows Internet Explorer launches the pluggable protocol handler registered for that URI scheme. If the specified open command specified in the registry contains a %1 parameter, Internet Explorer passes the URI to the registered pluggable protocol handler application.


 

 

  • 在注册表里新建register key

在注册表里新建一个key,按照URL protocol的格式,设置好路径。

也可以写一个.reg文件,打开文件即可注册key了。

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\HelloRio]
"URL Protocol"="C:\\Program Files (x86)\\HelloRio\\HelloRio.exe"
@="HelloRioProtocol"
[HKEY_CLASSES_ROOT\HelloRio\DefaultIcon]
@="C:\\Program Files (x86)\\HelloRio\\HelloRio.exe,1"
[HKEY_CLASSES_ROOT\HelloRio\shell]
[HKEY_CLASSES_ROOT\HelloRio\shell\open]
[HKEY_CLASSES_ROOT\HelloRio\shell\open\command]
@="\"C:\\Program Files (x86)\\HelloRio\\HelloRio.exe\" \"%1\""

第一行表示用的注册表工具

然后就是对应需要新建项在注册表中的路径,以及对应键值。双击该文件,即可自动在注册表中注册一个URL Protocol。

 

 写一个test的html。或者在地址行直接键入。

最后就会提示luanch你的exe了。这里我写了一个比较简单的应用程序,实际上当我们在register key,%1代表我们接受参数。

实际上传入的参数是HelloRio:myrio,这里需要解析字符串。本来想用正则表达式,无奈正则表达式学得不好。+。+

#include <Windows.h>
#include <iostream>
#include <cstring>
#include <regex>


using namespace std;

int main(int argc,char* argv[]){
    if(argc<2){
        MessageBox(NULL,TEXT("Please input a parameter£¡"),TEXT("HelloRio"), MB_OK);
        exit(1);
    }
    else{

        string s1=string(argv[1]);
        //search the right parameter

        auto i=find(s1.begin(),s1.end(),':');
        i++;
        string s2="hello"+string(i,s1.end());
        wstring tmp(s2.begin(),s2.end());
        const wchar_t* t=tmp.c_str();
        t+='\0';
        MessageBox(NULL,t,TEXT("HelloRio"), MB_OK);
        return 0;
    }
}

 

腾讯的key:


 

用JavaScript在HTML里打开exe,需要Active X且不被浏览器接受(因为安全性因素)

<html> 
<head> 
<script language="javascript"> 
function Run(strPath) 
{ 
var objShell = new ActiveXObject("wscript.shell"); 
objShell.exec(strPath); 
objShell = null; 
} 
</script> 
</head> 
<body> 
请输入要运行的程序路径:<br> 
<input name=exe type=text size=20 value="D:\\a.doc"> 
<BUTTON class=button onclick="Run(exe.value)">确定</BUTTON> 
</body> 
</html>

 

转载于:https://www.cnblogs.com/LUO77/p/5646588.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值