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>
阅读全文
0 0
- vc创建注册表协议,使用协议启动进程
- Java添加注册表协议
- VC创建注册表
- VC使用HTTP协议下载文件
- 使用wvdial启动ppp协议拨号上网
- 使用wvdial启动ppp协议拨号上网
- VC设置注册表启动项目
- VC++实现注册表自启动
- 用Chrome外部协议请求启动IE进程
- 使用BAPI_CONTRACT_CREATE创建采购合同框架协议
- 使用BAPI_CONTRACT_CREATE创建采购合同框架协议
- VC 实现ping协议
- VC++实现ARP协议
- VC++实现ARP协议
- VC网络协议
- VC以太网 FINS 协议
- VC根据进程名获得进程ID 和 VC获得系统特殊文件夹 SHGetSpecialFolderPath 和 VC写入注册表,使程序开机启动
- LINUX/I386启动协议
- Java-简单的数组赋值
- 提示 java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig 的解决办法
- html5
- Java源码之String
- MFC在不同的类窗体之间传递消息
- vc创建注册表协议,使用协议启动进程
- 调用系统相机相册
- this指针详解
- cs231n assignment1(KNN)用到的函数:enumerate()/flatnonzero()/argsort()/array_split()
- YYGraft v1.0.1 发布,基于 HTM5 的JS在线涂鸦工具
- Zero To One
- 解决 log4j.xml 问题 http//jakarta.apache.org/log4j/ uri is not registered 的方法
- ConcurrentHashMap 的实现原理
- 字体大小