Giao thức URL Protocol cho phép người dùng kích hoạt một chương trình cục bộ thông qua việc click vào một liên kết trên trang web, đồng thời truyền dữ liệu đến ứng dụng đó. Tức là, thực hiện gọi chương trình cục bộ từ trình duyệt web. Để làm được điều này, bạn cần đăng ký một giao thức URL tùy chỉnh trong registry với nội dung như sau:
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\""
Phân tích từng dòng:
-
Windows Registry Editor Version 5.00– Dòng này xác định phiên bản của công cụ chỉnh sửa registry. -
[HKEY_CLASSES_ROOT\PWFileVersion]– Tạo một nhánh mới tên làPWFileVersiondướiHKEY_CLASSES_ROOT. -
"URL Protocol"="C:\\Program Files (x86)\\PWFileVersion\\FileVersion.exe"– Đường dẫn tuyệt đối tới file exe sẽ được mở khi người dùng click vào liên kết. -
@="FileVersionProtocol"– Tên giao thức, có thể là bất kỳ chuỗi nào, nhưng không được sử dụng trong quá trình xử lý. -
[HKEY_CLASSES_ROOT\PWFileVersion\DefaultIcon]– Tạo một nhánh con để thiết lập biểu tượng mặc định cho giao thức. -
@="C:\\Program Files (x86)\\PWFileVersion\\FileVersion.exe,1"– Đường dẫn đến file exe và chỉ số biểu tượng. -
[HKEY_CLASSES_ROOT\PWFileVersion\shell],[HKEY_CLASSES_ROOT\PWFileVersion\shell\open],[HKEY_CLASSES_ROOT\PWFileVersion\shell\open\command]– Tạo các nhánh con cho hành động mở và lệnh thực thi. -
@="\"C:\\Program Files (x86)\\PWFileVersion\\FileVersion.exe\" \"%1\""– Lệnh thực thi, trong đó%1là tham số đầu vào từ URL.
Sau khi tạo tệp .reg, chạy nó để cập nhật registry. Khi đó, bạn có thể truy cập vào:
pwfileversion://để khởi động ứng dụng.pwfileversion://argumentđể truyền tham số vào ứng dụng.
Một số lưu ý quan trọng:
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\""
Khi thử nghiệm giao thức URL trên Windows, nếu đặt tên giao thức chứa ký tự _ (ví dụ: My_Protocol), chương trình sẽ không khởi động. Tuy nhiên, nếu đổi thành MyProtocol thì hoạt động bình thường.
Khi xây dựng liên kết trong HTML, toàn bộ URL sẽ được truyền vào ứng dụng cục bộ. Ví dụ:
<a href="myprotocol://123">Click here</a>
Trong ứng dụng sẽ nhận được chuỗi myprotocol://123.
Cài đặt thủ công giao thức:
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;
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;
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;
}
BOOL UnRegWebProtocol ( LPCTSTR lpszProtocolName )
{
if ( !lpszProtocolName || lstrlen(lpszProtocolName) < 1 )
return FALSE;
return RegDeleteAllSubKey ( HKEY_CLASSES_ROOT, lpszProtocolName );