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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM