vc创建注册表协议,使用协议启动进程

来源:互联网 发布:数控机床编程入门斯沃 编辑:程序博客网 时间:2024/06/06 00:57

下面是创建注册表协议的方法,及隐藏协议启动进程时的警告对话框

#pragma once#include <windows.h>#include <iostream>#include <tchar.h>// 创建注册表项BOOL CreateSubkey(HKEY hKey, LPCWSTR lpszSubkey, HKEY& hResult){DWORD dwDisposition = REG_CREATED_NEW_KEY;if (ERROR_SUCCESS != RegCreateKeyEx(hKey, lpszSubkey, 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS | KEY_WOW64_64KEY, NULL, &hResult, &dwDisposition)){return FALSE;}return TRUE;}// 写入注册表键值BOOL WritRegKeyAndValue(HKEY hKey, LPCWSTR lpszKey, LPCWSTR lpszValue, DWORD dwType = REG_SZ){if (ERROR_SUCCESS != RegSetValueEx(hKey, lpszKey, 0, dwType, (BYTE*)lpszValue, sizeof(TCHAR)*(_tcsclen(lpszValue) + 2 * sizeof(TCHAR)))){return FALSE;}return TRUE;}// 写入注册表键值BOOL WritRegValue(HKEY hKey, LPCWSTR lpszValue, DWORD dwType = REG_SZ){if (ERROR_SUCCESS != RegSetValueEx(hKey, NULL, 0, dwType, (BYTE*)lpszValue, sizeof(TCHAR)*(_tcsclen(lpszValue) + 2 * sizeof(TCHAR)))){return FALSE;}return TRUE;}//隐藏启动协议对话框BOOL HideWarnDlg(LPCWSTR lpProtocolName, LPCWSTR lpszValue, DWORD dwType = REG_DWORD){// DefaultIconHKEY hSubKey = NULL;WCHAR szSubKey[200] = { 0 };_stprintf_s(szSubKey, _T("Software\\Microsoft\\Internet Explorer\\ProtocolExecute\\%s"), lpProtocolName);if (!CreateSubkey(HKEY_CURRENT_USER, szSubKey, hSubKey)){throw(_T("DefaultIcon create error\n"));}DWORD dwValue = 0;if (ERROR_SUCCESS != RegSetValueEx(hSubKey, _T("WarnOnOpen"), 0, dwType, (BYTE*)&dwValue, sizeof(dwValue))){return FALSE;}return TRUE;}// 添加策略BOOL SetProtocolToKey(LPCWSTR lpProtocolName, LPCWSTR lpExePath, LPCWSTR lpszProPath = NULL){BOOL bSuccess = FALSE;HKEY hSubKey = NULL;HKEY hSubKeyDefaultIcon = NULL;HKEY hSubKeyShell = NULL;HKEY hSubKeyOpen = NULL;HKEY hSubKeyCommand = NULL;try{// Protocolif (!CreateSubkey(HKEY_CLASSES_ROOT, lpProtocolName, hSubKey)){throw(_T("CreateSubkey error"));}WCHAR chProtoclValue[100] = { 0 };_stprintf_s(chProtoclValue, _T("%s Protocol"), lpProtocolName);if (!WritRegValue(hSubKey, chProtoclValue)){throw(_T("WritRegValue error!\n"));}//Protocol - URL Protocolif (!WritRegKeyAndValue(hSubKey, _T("URL Protocol"), _T(""))){throw(_T("WritRegKeyAndValue error!\n"));}// DefaultIconWCHAR szSubKey[100] = { 0 };_stprintf_s(szSubKey, _T("%s\\DefaultIcon"), lpProtocolName);if (!CreateSubkey(HKEY_CLASSES_ROOT, szSubKey, hSubKeyDefaultIcon)){throw(_T("DefaultIcon create error\n"));}if (!WritRegValue(hSubKeyDefaultIcon, lpExePath)){throw(_T("write DefaultIcon error"));}// shell_stprintf_s(szSubKey, _T("%s\\shell"), lpProtocolName);if (!CreateSubkey(HKEY_CLASSES_ROOT, szSubKey, hSubKeyShell)){throw(_T("shell create error\n"));}// Open_stprintf_s(szSubKey, _T("%s\\shell\\Open"), lpProtocolName, hSubKeyOpen);if (!CreateSubkey(HKEY_CLASSES_ROOT, szSubKey, hSubKeyOpen)){throw(_T("create error"));}// Command_stprintf_s(szSubKey, _T("%s\\shell\\Open\\Command"), lpProtocolName, hSubKeyCommand);if (!CreateSubkey(HKEY_CLASSES_ROOT, szSubKey, hSubKeyCommand)){throw(_T("create error"));}else{if (!WritRegValue(hSubKeyCommand, lpExePath)){throw(_T("write Command error\n"));}}HideWarnDlg(lpProtocolName, L"");bSuccess = TRUE;}catch (TCHAR* pError){}// 关闭注册表if (hSubKey)RegCloseKey(hSubKey);if (hSubKeyDefaultIcon)RegCloseKey(hSubKeyDefaultIcon);if (hSubKeyShell)RegCloseKey(hSubKeyShell);if (hSubKeyOpen)RegCloseKey(hSubKeyOpen);if (hSubKeyCommand)RegCloseKey(hSubKeyCommand);return bSuccess;}// 删除注册表项BOOL DelRegKey(HKEY hRoot, LPCWSTR lpszSubkey){HKEY hResult = NULL;if (ERROR_SUCCESS == RegOpenKeyEx(hRoot, lpszSubkey, 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY, &hResult)){if (ERROR_SUCCESS != RegDeleteKey(hResult, _T(""))){}RegCloseKey(hResult);return TRUE;}return FALSE;}// 删除写入的协议注册表项void DelSubKey(LPCWSTR lpProtocolName){TCHAR szSubKey[MAX_PATH] = { 0 };_stprintf_s(szSubKey, _T("%s\\shell\\Open\\Command"), lpProtocolName);DelRegKey(HKEY_CLASSES_ROOT, szSubKey);_stprintf_s(szSubKey, _T("%s\\shell\\Open"), lpProtocolName);DelRegKey(HKEY_CLASSES_ROOT, szSubKey);_stprintf_s(szSubKey, _T("%s\\shell"), lpProtocolName);DelRegKey(HKEY_CLASSES_ROOT, szSubKey);_stprintf_s(szSubKey, _T("%s\\DefaultIcon"), lpProtocolName);DelRegKey(HKEY_CLASSES_ROOT, szSubKey);_stprintf_s(szSubKey, _T("%s"), lpProtocolName);DelRegKey(HKEY_CLASSES_ROOT, szSubKey);}


写入的注册表项为:

Windows Registry Editor Version 5.00#destoplancher为新建的协议名[HKEY_CLASSES_ROOT\destoplancher]@="destoplancher Protocol""URL Protocol"=""#DefaultIcon的值为要启动的进程路径名[HKEY_CLASSES_ROOT\destoplancher\DefaultIcon]@="C:\\Users\\Administrator\\Desktop\\Test.exe"[HKEY_CLASSES_ROOT\destoplancher\shell]@=""[HKEY_CLASSES_ROOT\destoplancher\shell\Open]@=""#Command的值为要启动的进程路径名[HKEY_CLASSES_ROOT\destoplancher\shell\Open\Command]@="C:\\Users\\Administrator\\Desktop\\Test.exe"#屏蔽启动协议时对话框的弹出[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\ProtocolExecute\destoplancher]"WarnOnOpen"=dword:00000000

协议测试HTML:

<html>   <head >   <meta charset="utf-8">  </head>  <body bgcolor="green">  <hr/>  <h4>协议测试</h4>  <a  name="label" href="destoplancher://"  target="qq_main"> 启动进程</a>  <hr/>  </body>  </html>