红鱼儿

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

UniGui web客户端与本地exe通讯

 URL Protocol服务协议,简单说是点击一个网页的链接,通过这个链接执行本地应用程序,并向其传递相应的数据,也就是说,实现网页调用本地应用程序。为此,需要向注册表注册一个自定义的URL Protocol协议,内容如下:

复制代码
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\PWFileVersion]
"URL Protocol"="C:\\Program Files (x86)\\PWFileVersion\\FileVersion.exe"
@="FileVersionProtocol"
[HKEY_CLASSES_ROOT\PWFileVersion\DefaultIcon]
@="C:\\Program Files (x86)\\PWFileVersion\\FileVersion.exe,1"
[HKEY_CLASSES_ROOT\PWFileVersion\shell]
[HKEY_CLASSES_ROOT\PWFileVersion\shell\open]
[HKEY_CLASSES_ROOT\PWFileVersion\shell\open\command]
@="\"C:\\Program Files (x86)\\PWFileVersion\\FileVersion.exe\" \"%1\""
 
复制代码

1.一行行来解释:
Windows Registry Editor Version 5.00
(1)表示注册表工具的版本信息;

[HKEY_CLASSES_ROOT\PWFileVersion]
(2)PWFileVersion表示的时注册表的HKEY_CLASSES_ROOT下新增一个PWFileVersion树(理解为在HKEY_CLASSES_ROOT下新增一个文件夹就可以了)

"URL Protocol"="C:\Program Files (x86)\PWFileVersion\FileVersion.exe"
(3)你在网页中要调用打开的程序绝对路径,记得一定要是exe文件

@="FileVersionProtocol"
(4)协议名称,可以是任意字符串,后面不会用到,也可以为空

[HKEY_CLASSES_ROOT\PWFileVersion\DefaultIcon]
(5)在PWFileVersion下新增一个分支,不用管

@="C:\Program Files (x86)\PWFileVersion\FileVersion.exe,1"
(6)地址和(3)中保持一致,1照抄

[HKEY_CLASSES_ROOT\PWFileVersion\shell]
[HKEY_CLASSES_ROOT\PWFileVersion\shell\open]
[HKEY_CLASSES_ROOT\PWFileVersion\shell\open\command]
(7)(8)(9)和(5)一样,新增分支而已

@=""C:\Program Files (x86)\PWFileVersion\FileVersion.exe" "%1""
(10)向要调用的程序内传递参数。前面的地址与(3)保持一致,后面的%1表示参数。敲黑板,这里面的/千万不要有所遗漏!本人在这个坑上蹲了很久- -;

2.运行reg文件,进行注册表注册。

这时候在浏览器输入:

pwfileversion://即可调用该程序

pwfileversion://argument随便什么字符串,即可将参数传入该程序

3.细节

复制代码
Windows Registry Editor Version 5.00
 
[HKEY_CLASSES_ROOT\WebGoExe]
@="URL: WebGoExe Protocol Handler"
"URL Protocol"=""
 
[HKEY_CLASSES_ROOT\WebGoExe\DefaultIcon]
@="C:\\Program Files\\Losng\\WebGoExe.EXE"
 
[HKEY_CLASSES_ROOT\WebGoExe\Shell]
 
[HKEY_CLASSES_ROOT\WebGoExe\Shell\Open]
 
[HKEY_CLASSES_ROOT\WebGoExe\Shell\Open\Command]
@="C:\\Program Files\\Losng\\WebGoExe.EXE \"%1\""
复制代码

在Windows下,按照网上找到的例子实现自己的Url Protocol,但是发现一直启动不了指定的Exe,浏览器一直提示未找到指定文件,通过测试发现这个Protocol起名字不能有"_",比如定义成My_Protocol就会启动不了,但是定义成MyProtocol就可以。 

4.在写链接的时候自定义参数,程序会把整个链接都获取到,自己在进行解析 :

比如:

<a href="myprotocol://123">

会获取到

"myprotocol://123"

5、代码实现方式

复制代码
如果你电脑中装有QQ,在IE地址栏输入:“tencent://Message/?menu=yes&exe=&uin=13231462”然后[回车],
立即可以与我的QQ建立临时会话,Skype也有类似的功能。到底是如何实现的呢?看MSDN中有这么一段话: 
 The IURLSearchHook interface is used by the browser to translate the address of an unknown URL protocol.
 When attempting to browse to a URL address that does not contain a protocol, 
 the browser will first attempt to determine the correct protocol from the address. 
 If this is not successful, the browser will create URL Search Hook objects and call each object's
 Translate method until the address is translated or all of the hooks have been queried.   
 IURLSearchHook接口被浏览器用来转换一个未知的URL协议地址。
 当浏览器企图去打开一个未知协议的URL地址时,浏览器首先尝试从这个地址得到当前的协议,如果不成功,
 浏览器将创建在系统中注册的URL Search Hook对象并调用每一个对象的Translate方法,直到地址被转换或所有的URL Search Hook都尝试过。  
 也就是说,我们可以注册一种目前不存在的协议(类似HTTP),当浏览器遇到新的协议时会自动调用Translate方法来翻译我们的协议,
 甚至激活我们自己的程序。以下源代码将实现注册一个新的自定义的Web协议: 
  注册自定义的Web协议
//return : ------------------------------------------------------------------------//  
 0    -    失败//        1    -    成功//        2    -    已经存在//
int RegWebProtocol ( LPCTSTR lpszProtocolName, LPCTSTR lpszAssociatedApp, int nIconIndex/*=0*/ )
{    
    if ( !lpszProtocolName || lstrlen(lpszProtocolName) < 1 || !lpszAssociatedApp ||  lstrlen(lpszAssociatedApp) < 1 )          
        return 0;    
        
    CString csSubKey;   
    DWORD dwBufSize = 0;    
    // 该协议已经存在    
    HKEY hKey = NULL;    
    if ( RegOpenKeyEx ( HKEY_CLASSES_ROOT,  lpszProtocolName,   0, KEY_ALL_ACCESS,  &hKey ) == ERROR_SUCCESS )  
    {       
        return 2;   
    }   
    else 
        hKey = NULL; 
        
    // 创建协议子键    
    if ( !CreateRegisterSubKey ( HKEY_CLASSES_ROOT, lpszProtocolName ) )        
        return 0;   
    
    // 设置协议描述字符串    
    CString csProtocolDesc; 
    csProtocolDesc.Format ( _T("%sProtocol"), lpszProtocolName );    
    dwBufSize = csProtocolDesc.GetLength();    
    if (!WriteRegister ( HKEY_CLASSES_ROOT, lpszProtocolName, _T(""), REG_EXPAND_SZ, (PUCHAR)csProtocolDesc.GetBuffer(0),&dwBufSize) )        
        return 0;    
    CString csAppFile; 
    csAppFile.Format ( _T("%s"), lpszAssociatedApp );    
    dwBufSize = csAppFile.GetLength();    
    if ( !WriteRegister ( HKEY_CLASSES_ROOT, lpszProtocolName,  _T("URL Protocol"), REG_EXPAND_SZ, (PUCHAR)csAppFile.GetBuffer(0),&dwBufSize) )        
        return 0; 
        
    // DefaultIcon 子键    
    csSubKey.Format ( _T("%s\\DefaultIcon"), lpszProtocolName );    
    if ( !CreateRegisterSubKey ( HKEY_CLASSES_ROOT, csSubKey ) )        
        return 0;    
    CString csIconParameter; 
    csIconParameter.Format ( _T("%s,%d"), lpszAssociatedApp, nIconIndex );    
    dwBufSize = csIconParameter.GetLength();    
    if ( !WriteRegister ( HKEY_CLASSES_ROOT, csSubKey,  _T(""), REG_EXPAND_SZ, (PUCHAR)csIconParameter.GetBuffer(0),&dwBufSize) )        
        return 0; 
        
    // shell\open\command 子键    
    csSubKey.Format ( _T("%s\\shell\\open\\command"), lpszProtocolName );    
    if ( !CreateRegisterSubKey ( HKEY_CLASSES_ROOT, csSubKey ) )       
        return 0;    
    CString csCommand; csCommand.Format ( _T("\"%s\" \"%%1\""), lpszAssociatedApp );    
    dwBufSize = csCommand.GetLength();    
    if ( !WriteRegister ( HKEY_CLASSES_ROOT, csSubKey, _T(""), REG_EXPAND_SZ, (PUCHAR)csCommand.GetBuffer(0),&dwBufSize) )        
        return 0; 
        
    return 1;
}
    
 卸载自定义的Web协议//
BOOL UnRegWebProtocol ( LPCTSTR lpszProtocolName )
 {       
    if ( !lpszProtocolName || lstrlen(lpszProtocolName) < 1 )      
        return FALSE;    
    return RegDeleteAllSubKey ( HKEY_CLASSES_ROOT, lpszProtocolName );
复制代码

 

posted on   红鱼儿  阅读(557)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2019-06-18 kbmMW均衡负载与容灾(1)
点击右上角即可分享
微信分享提示