注册表添加策略方法及实现,添加桌面右键菜单

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"
//注册表添加桌面右键菜单及图标方法///


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值