像腾讯QQ和Skype一样定义自己的类似HTTP的Web协议,可以通过URL来触发自己的程序(附源码)
2008-01-06 20:00
645 查看
像腾讯QQ和Skype一样定义自己的类似HTTP的Web协议,可以通过URL来触发自己的程序(附源码)
【版 本】
1.0.0
【操作系统】
Windows 桌面操作系统
【作 者】
谢红伟·chrys ·chrys@163.com ·http://www.viction.net
【开发公司】
深圳市伟信科技开发有限公司
【开发日期】
2008-01-06 18:29:35
如果你电脑中装有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 );
}
我提供一个详细的源代码可以演示整个过程,程序界面如下:
先设置好需要注册的协议名称和关联的执行程序文件名,然后点[Register Protocol]按钮完成注册,点击[Test Protocol]按钮测试注册后的效果。
如本例中注册一个“HXXP”协议,关联程序就是本程序,当点击[Test Protocol]按钮时就会执行该程序的另一个副本,同时[Test URL Parameter]输入的参数也被显示出来了。您也可以将“HXXP://Para1/Para2/Para2”复制到IE地址栏中执行,实现相同的效果,如下图:
源代码下载地址:http://download.csdn.net/source/327425 http://www.vckbase.com/code/viewcode.asp?id=3109
谢谢曹昌利同志讲述QQ临时会话的相关知识。
【版 本】
1.0.0
【操作系统】
Windows 桌面操作系统
【作 者】
谢红伟·chrys ·chrys@163.com ·http://www.viction.net
【开发公司】
深圳市伟信科技开发有限公司
【开发日期】
2008-01-06 18:29:35
如果你电脑中装有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 );
}
我提供一个详细的源代码可以演示整个过程,程序界面如下:
先设置好需要注册的协议名称和关联的执行程序文件名,然后点[Register Protocol]按钮完成注册,点击[Test Protocol]按钮测试注册后的效果。
如本例中注册一个“HXXP”协议,关联程序就是本程序,当点击[Test Protocol]按钮时就会执行该程序的另一个副本,同时[Test URL Parameter]输入的参数也被显示出来了。您也可以将“HXXP://Para1/Para2/Para2”复制到IE地址栏中执行,实现相同的效果,如下图:
源代码下载地址:http://download.csdn.net/source/327425 http://www.vckbase.com/code/viewcode.asp?id=3109
谢谢曹昌利同志讲述QQ临时会话的相关知识。
相关文章推荐
- 仿腾讯 QQ 和 Skype 通过URL触发自己的程序
- 仿腾讯 QQ 和 Skype 通过URL触发自己的程序--安装时注册
- 仿腾讯 QQ 和 Skype 通过URL触发自己的程序
- 仿腾讯 QQ 和 Skype 通过URL触发自己的程序
- 仿腾讯 QQ 和 Skype 通过URL触发自己的程序
- 仿腾讯 QQ 和 Skype 通过URL触发自己的程序
- 02_tomcat与web程序结构与Http协议与HttpUrlConnection
- C#实现通过URL触发自己的程序
- tomcat与web程序结构与Http协议与HttpUrlConnection
- 第二天(tomcat与web程序结构与Http协议与HttpUrlConnection)
- tomcat与web程序结构与Http协议与HttpUrlConnection
- tomcat与web程序结构与Http协议与HttpUrlConnection(带答案)
- 通过http 协议 可以访问 Hadoop 50070 端口,hadoop 里面配置的servlet 是什么容器呢 ?
- webpack+vue+vue-router创建项目具体(备注:供自己学习时以备随时可以查看用而整理,首发地址:http://blog.csdn.net/fungleo/article/detail)
- 基于NIO实现客户端通过HTTP协议访问WEB站点
- 将OBS录制数据通过RTMP协议引入到自己的程序中
- Web程序调式的利器 - Fiddler (HTTP协议监视工具)
- DELPHI实现通过URL触发自定义协议的软件
- Java客户端通过HttpURLConnection连接Web服务器的Session保存问题
- [Web开发] Web程序调式的利器 - Fiddler (HTTP协议监视工具)