注册表添加策略方法及实现,添加桌面右键菜单
2017-12-21 10:02
645 查看
Windows Registry Editor Version 5.00 //////////////////////注册表添加协议方法/////////////////////////// [HKEY_CLASSES_ROOT\destoplancher] @="destoplancher Protocol" "URL Protocol"="" [HKEY_CLASSES_ROOT\destoplancher\DefaultIcon] @="C:\\Users\\Administrator\\Desktop\\notepad.exe" [HKEY_CLASSES_ROOT\destoplancher\shell] [HKEY_CLASSES_ROOT\destoplancher\shell\Open] [HKEY_CLASSES_ROOT\destoplancher\shell\Open\Command] @="C:\\Users\\Administrator\\Desktop\\notepad.exe" //////////////////////注册表添加协议方法/////////////////////////// //////////////////////协议测试 html/////////////////////////// <html> <meta charset="utf-8"> <body bgcolor="green"> <hr/> <h4>协议测试</h4> <a name="label" href="destoplancher://"> 打开计算机</a> <hr/> </body> </html> //////////////////////协议测试 html/////////////////////////// //////////////////////C++实现/////////////////////////// #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) { // DefaultIcon HKEY hSubKey = NULL; WCHAR szSubKey[1024] = { 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 { // Protocol if (!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 Protocol if (!WritRegKeyAndValue(hSubKey, _T("URL Protocol"), _T(""))) { throw(_T("WritRegKeyAndValue error!\n")); } // DefaultIcon WCHAR 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); } //////////////////////C++实现/////////////////////////// //////////////////////C++调用/////////////////////////// SetProtocolToKey(_T("desktopicon"), "文件路径"); //////////////////////C++调用/////////////////////////// //////////////////////注册表添加桌面右键菜单及图标方法/////////////////////////// Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\Background\shell] [HKEY_CLASSES_ROOT\Directory\Background\shell\启动★软件1★] @="" "icon"="C:\\Users\\Administrator\\Desktop\\工具条软件Exe\\MyClientIcon.exe" "Position"="top" [HKEY_CLASSES_ROOT\Directory\Background\shell\启动★软件1★\command] @="C:\\Users\\Administrator\\Desktop\\工具条软件Exe\\MyClientIcon.exe" //////////////////////注册表添加桌面右键菜单及图标方法///////////////////////////
相关文章推荐
- C++实现添加桌面右键新建菜单
- 桌面右键菜单,添加程序的方法
- 【转】windows7的桌面右键菜单的“新建”子菜单,在注册表哪个位置,如何在“新建"里面添加一个新项
- 实现文件右键“打开方式”菜单功能(查看系统这种功能对比实现)(读注册表的方法)
- 【电脑使用】修改注册表,添加右键菜单“打开”的方法
- C#实现窗体最小化时存至系统托盘,并为notifyicon添加右键菜单
- 讲用记事本打开添加到桌面右键菜单中
- 修改注册表在特定文件右键菜单中添加菜单项
- 用VB脚本实现向右键菜单添加快捷方式
- ListBox添加右键菜单并选择该项功能的实现
- 在Windows注册表中添加右键菜单删除CVS Folds的选项
- (转)在CListView列表视图中添加右键菜单的方法
- 文件夹右键菜单添加快捷DOS命令的两种方法
- 《ArcGIS Engine+C#实例开发教程》第六讲 右键菜单添加与实现
- 通过修改注册表添加IE右键菜单
- 为jQuery.Treeview添加右键菜单的实现代码
- 添加删除右键菜单的方法
- 修改注册表在右键菜单中添加清空文件夹命令
- windows注册表右键菜单添加
- C#实现窗体最小化时存至系统托盘,并为notifyicon添加右键菜单