数据结构

注册表由键(或称"项")、子键(子项)和值项构成.一个键就是分支中的一个文件夹,而子键就是这个文件夹中的子文件夹,子键同样是一个键.一个值项则是一个键的当前定义,由名称、数据类型以及分配的值组成.一个键可以有一个或多个值,每个值的名称各不相同,如果一个值的名称为空,则该值为该键的默认值.

数据类型

注册表的数据类型主要有以下四种:
显示类型(在编辑器中)   数据类型    说明                   
REG_SZ          字符串     文本字符串
REG_MULTI_SZ       多字符串    含有多个文本值的字符串
REG_BINARY         二进制数    二进制值,以十六进制显示.
REG_DWORD        双字      一个32位的二进制值,显示为8位的十六进制值.

各主键的简单介绍

    • HKEY_LOCAL_MACHINE  是一个显示控制系统和软件的处理键.HKLM键保存着计算机的系统信息.它包括网络和硬件上所有的软件设置.
    • HKEY_CLASSES_ROOT  是系统中控制所有数据文件的项.
    • HKEY_USERS  将缺省用户和目前登陆用户的信息输入到注册表编辑器
    • HKEY_CURRENT_USER  包含着在HKEY_USERS安全辨别里列出的同样信息
    • HKEY_CURRENT_CONFIG  包括了系统中现有的所有配置文件的细节.HKEY_CURRENT_CONFIG允许软件和设备驱动程序员很方便的更新注册表,而不涉及到多个配置文件信息. HKEY_LOCAL_MACHINE中同样的数据和任何注册表的变化都会同时的变化.
---------------------------------------------------------------------------------------------------------------------------------------------------
参考网页http://www.cnblogs.com/kzloser/archive/2012/11/07/2758404.html 添加开机启动程序 #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <windows.h>
#include <cstdlib>
using namespace std;
int main()
{ HKEY hKey;
LPCTSTR lpRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
DWORD state,dwtype,sizeBuff;
long lRet;
char reBuff[] = {}; cout<<"输入0或者1.执行操作.1插入0删除"<<endl;
int operatorNum;
cin>>operatorNum;
cout<<operatorNum<<endl;
cout<<"======================================================================="<<endl;
/**
LONG RegCreateKeyEx(
HKEY hKey, // 主键名称
LPCWSTR lpSubKey,// 子键名称或路径
DWORD Reserved, //0 保留字段,默认设置为0
LPWSTR lpClass, // 一般设置为NULL
DWORD dwOptions, //对你建立的键的一些选项,可以是这些值:REG_OPTION_NON_VOLATILE,REG_OPTION_VOLATILE,REG_OPTION_BACKUP_RESTORE第一个是默认的了。一般用第一个就可以了。
REGSAM samDesired,// 设置你对你建立的这个键的访问权限Ignored. Set to zero to ensure compatibility with future versions of Windows Mobile.
LPSECURITY_ATTRIBUTES lpSecurityAttributes, //一般设置为NULL
PHKEY phkResult, // 返回新建注册表项的句柄
LPDWORD lpdwDisposition//用来查看是打开一个已经有的键,还是新建了键
);
解释:打开指定的键或子键。如果要打开的键不存在的话,本函数会试图建立它。
当在创建或打开注册表的键时,需要指定访问权限,而这些访问权限需要到一级。
默认的权限是KEY_ALL_ACCESS权限。
还有KEY_CREATE_LINK创建字符链权限,
KEY_CREATE_SUB_KEY创建子键权限,
KEY_EXECUTE读取键权限,
KEY_NOTIFY获得修改键通知的权限,
KEY_QUERY_VALUE查询键值的权限,
KEY_SET_VALUE设置数据值的权限。
注意不能在根一级建键,在注册表的根一级仅可有预定义的键。具体使用,请查看联机手册。
*/
//https://msdn.microsoft.com/zh-cn/aa911940
if(operatorNum==){
lRet = RegCreateKeyEx(HKEY_CURRENT_USER,lpRun,,NULL,,,NULL,&hKey,&state);
if(lRet == ERROR_SUCCESS)
{
if(state == REG_CREATED_NEW_KEY)
cout<<"create ok"<<endl;
RegCloseKey(hKey);
}else{
cout<<"create fail"<<endl;
}
/*
LONG RegOpenKeyEx(
HKEY hKey,
LPCWSTR lpSubKey,
DWORD ulOptions, //Reserved. Set to zero.保留字段,默认设置为0
REGSAM samDesired, //Not supported. Set to zero.
PHKEY phkResult // Pointer to a variable that receives a handle to the opened key. When you no longer need the returned handle, call the RegCloseKey function to close it.
);
*/
/*
LONG RegSetValueEx(
HKEY hKey,
LPCWSTR lpValueName,//输入
DWORD Reserved,//Reserved. Must be set to zero.
DWORD dwType,//Type of information to be stored as the value data
const BYTE* lpData,//[in] Pointer to a buffer that contains the data to be stored with the specified value name.
DWORD cbData//[in] Size, in bytes, of the information pointed to by lpData. If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, This parameter must include the size of the terminating null character. The maximum size of data allowed is 4 KB.
);
*/
lRet= RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, , KEY_WRITE, &hKey);
if(lRet == ERROR_SUCCESS)
{
cout<<"open ok"<<endl;
//RegSetValueEx(hKey, "mgtest",0,REG_SZ,(BYTE *)"success",10);
//RegSetValueEx(hKey, "mgtest",0,REG_SZ,(BYTE *)"C:\\windows\\system32\\notepad.exe",strlen("C:\\windows\\system32\\notepad.exe")*2);
long temp= RegSetValueEx(hKey, "mgtest",,REG_SZ,(BYTE *)"C:\\windows\\system32\\notepad.exe",strlen("C:\\windows\\system32\\notepad.exe")*);
if(temp!=ERROR_SUCCESS){
cout<<"set fail"<<endl;
}else{
cout<<"set ok"<<endl;
}
RegCloseKey(hKey);
}else{
cout<<"open fail"<<endl;
}
}else
{
lRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, , KEY_WRITE, &hKey);
if(lRet==ERROR_SUCCESS)
{
cout<<"open ok"<<endl;
//删除键
long temp=RegDeleteValue(hKey,"mgtest");
if(temp!=ERROR_SUCCESS){
cout<<"del fail"<<endl;
}else{
cout<<"del ok"<<endl;
}
//关闭键
RegCloseKey(hKey);
}else{
cout<<"open fail"<<endl;
}
}
getchar();
return ;
}
红色背景的为开机启动程序所在路径

C++注册表操作的更多相关文章

  1. MFC学习 文件操作注册表操作

    c读写文件 void CFileView::OnRead() { FILE *pFile = fopen("1.txt", "r"); /*char ch[10 ...

  2. delphi 注册表操作(读取、添加、删除、修改)完全手册

    DELPHI VS PASCAL(87)  32位Delphi程序中可利用TRegistry对象来存取注册表文件中的信息. 一.创建和释放TRegistry对象 1.创建TRegistry对象.为了操 ...

  3. CRegKey 注册表操作

    CRegKey 注册表操作 标签: accessnulluserpathbyteie 2011-11-03 13:55 3477人阅读 评论(0) 收藏 举报  分类: win32(7)  1.简介 ...

  4. C# 我的注册表操作类

    using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using Syst ...

  5. Delphi的注册表操作

    转帖:Delphi的注册表操作 2009-12-21 11:12:52 分类: Delphi的注册表操作 32位Delphi程序中可利用TRegistry对象来存取注册表文件中的信息.     一.创 ...

  6. C#注册表操作类--完整优化版

    using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; namespace ...

  7. 《天书夜读:从汇编语言到windows内核编程》八 文件操作与注册表操作

    1)Windows运用程序的文件与注册表操作进入R0层之后,都有对应的内核函数实现.在windows内核中,无论打开的是文件.注册表或者设备,都需要使用InitializeObjectAttribut ...

  8. C#注册表操作类(完整版) 整理完整

    /// <summary> /// 注册表基项静态域 /// /// 主要包括: /// 1.Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT主键 /// ...

  9. QSettings配置读写-win注册表操作-ini文件读写

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSettings配置读写-win注册表操作-ini文件读写     本文地址:http:// ...

  10. Atitit. 注册表操作查询 修改 api与工具总结 java c# php js python 病毒木马的原理

    Atitit. 注册表操作查询 修改 api与工具总结 java c# php js python 病毒木马的原理 1. reg 工具 这个cli工具接口有,优先使用,jreg的要调用dll了,麻烦的 ...

随机推荐

  1. asp.net中选择数字时,另外的数字同时发生变化(适用dev控件)

      关键: <ClientSideEvents ValueChanged="AgioChanged" />   <div class="col-sm-4 ...

  2. iOS软件开发架构理解

    这个东西是硬伤,框架?自带的mvc? 自带的UIViewController UIView UINavigationController 这些算不算?当然算的,cocoa框架嘛,大家都知道. 其实,我 ...

  3. Application tried to present a nil modal view controller on target “Current View Controller”解决方案

    情景再现 1,自定义一个storyboard: 打开xcode,按下cmd+N,新建一个Storyboard--->next 将新建立的storyboard命名为:TestViewControl ...

  4. textContent 与innerText

    转自下面这位大神: http://zhangyaochun.iteye.com/blog/1391370 其实关于这textContent与innerText有很多碎碎的东西,不过个人觉得还是一个不错 ...

  5. java的InputStream和OutputStream的理解【转】

    1.在java中stream代表一种数据流(源),javaio的底层数据元,---(想像成水龙头)2.任何有能力产生数据流(源)的javaio对象就可以看作是一个InputStream对象既然它能产生 ...

  6. zabbix3.0.4监控mysql主从同步

    zabbix3.0.4监控mysql主从同步 1.监控mysql主从同步原理: 执行一个命令 mysql -u zabbix -pzabbix -e 'show slave status\G' 我们在 ...

  7. JAVA如何获取GUID

    UUID.randomUUID().toString().toUpperCase().replaceAll("-", "")

  8. LightOJ 1313 - Protect the Mines(凸包)

    1313 - Protect the Mines   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 M ...

  9. 【iOS问题】The file “XXX.app” couldn’t be opened because you don’t have permission to view it.

    当引入第三方的框架的时候 容易产生以下问题: The file "XXX.app" couldn't be opened because you don't have permis ...

  10. AFNetwork 作用和用法详解

    转自:http://www.cnblogs.com/mkai/p/5729685.html AFNetworking是一个轻量级的iOS网络通信类库.它建立在NSURLConnection和NSOpe ...