zoukankan      html  css  js  c++  java
  • C# winFrom 通过注册新协议实现网页链接打开本地程序

    通过网页链接打开本地本地程序,想到最多的方法就是通过activex控件,但这里介绍一个通过注册新协议来打开本地程序的方法。

    参考网上的对QQ的分析,原理很简单:注册新协议并且关联该协议的执行程序,这样当点击该协议的URL链接时就会启动相应的执行程序。

    具体原理方法可以参考:

    http://www.vckbase.com/document/viewdoc/?id=1804   上面网页打不开 可以看以下内容~

    View Code
     如果你电脑中装有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协议:
    //
    // 卸载自定义的Web协议
    //
    BOOL UnRegWebProtocol ( LPCTSTR lpszProtocolName )
    {
    if ( !lpszProtocolName || lstrlen(lpszProtocolName) < 1 )
    return FALSE;

    return RegDeleteAllSubKey ( HKEY_CLASSES_ROOT, lpszProtocolName );
    }

    windows下注册新协议当然就是写注册表了,注册内容如下:

    Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT\RunLocal]
    @="RunLocal Protocol"
    "URL Protocol"=""

    [HKEY_CLASSES_ROOT\RunLocal\DefaultIcon]
    @="c:\\windows\\RunLocal.exe,1"

    [HKEY_CLASSES_ROOT\RunLocal\shell]
    @=""

    [HKEY_CLASSES_ROOT\RunLocal\shell\open]
    @=""

    [HKEY_CLASSES_ROOT\RunLocal\shell\open\command]
    @="\"c:\\windows\\RunLocal.exe\" \"%1\""

    这里我贴个Nsis脚本,可以通过安装程序注册新协议。新协议为“RunLocal://######”,该脚本会在$WINDIR目录下安装一个协议处理程序runlocal.exe,功能很简单,就是执行“cmd.exe”。想实现复杂的功能只要编写这个协议处理程序就可以了(提示:链接地址会当作参数传给处理程序)。


    Name "Runlocal install"
    OutFile "install.exe"

    Section "ThisNameIsIgnoredSoWhyBother?"
    SetOutPath "$WINDIR"
    SetOverwrite on
    File "RunLocal.exe"

    WriteRegStr HKCR "RunLocal" "" "RunLocal Protocol"
    WriteRegStr HKCR "RunLocal" "URL Protocol" ""

    WriteRegStr HKCR "RunLocal\DefaultIcon" "" '"$WINDIR\RunLocal.exe,1"'
    WriteRegStr HKCR "RunLocal\shell" "" ""
    WriteRegStr HKCR "RunLocal\shell\open" "" ""
    WriteRegStr HKCR "RunLocal\shell\open\command" "" '"$WINDIR\RunLocal.exe" "%1"'
    SectionEnd

    ; eof

  • 相关阅读:
    QT 应用程序关闭某个窗口时,关闭打开的所有其他窗口并退出程序 【转】
    XP配置DCOM服务【转】
    Android最佳性能实践(二)——分析内存的使用情况
    Android最佳性能实践(一)——合理管理内存
    快速实现 ListView下拉,图片放大刷新操作
    Android布局实现圆角边框
    android 自定义文字跑马灯 支持拖拽,按住停止滚动,自定义速度
    Android NDK 环境搭建 + 测试例程
    Android -- 桌面悬浮,仿360
    android-async-http AsyncHttpClient介绍
  • 原文地址:https://www.cnblogs.com/weixing/p/2364990.html
Copyright © 2011-2022 走看看