CentForever

C++注册表操作

数据结构

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

数据类型

注册表的数据类型主要有以下四种:
显示类型(在编辑器中)   数据类型    说明                   
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[10] = {0}; 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==1){ lRet = RegCreateKeyEx(HKEY_CURRENT_USER,lpRun,0,NULL,0,0,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, 0, 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",0,REG_SZ,(BYTE *)"C:\\windows\\system32\\notepad.exe",strlen("C:\\windows\\system32\\notepad.exe")*2); 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, 0, 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 0; }
红色背景的为开机启动程序所在路径

 

posted on 2015-07-18 18:26  CentForever  阅读(674)  评论(0编辑  收藏  举报

导航