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

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

#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[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
	{
		// 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);
}


写入的注册表项为:

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>  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值