您的位置:首页 > 其它

使用Windows API操作注册表

2012-05-02 17:30 127 查看


来自:http://blog.csdn.net/yunboy4/article/details/4595230


使用Windows API操作注册表

分类: Win322009-09-25
19:22 354人阅读 评论(2) 收藏 举报

注册表中储存着大量的系统设置数据,为了方便管理系统,注册表被设计成树形结构,结构最顶层称为主键,主键下面有注册表项,每个注册表项都可以有子键。注册表项中包含了存储数据的值项,每个数据表都可以有多个值项,但总有一个默认的值项。每个值项都有自己的名称、类型和数据。打开注册表的命令是regedit。

1.常用主键的介绍:

HKEY_CLASSES_ROOT:定义系统中所有文件类型和基本操作标识

HKEY_CURRENT_USER:包含当前用户的配置文件

HKEY_LOCAL_MACHINE:包含于本地计算机系统相关的信息

HKEY_USER:定义所有用户的信息

HKEY_CURRENT_CONFIG:包含计算机在启动时由本地计算机系统使用的硬件配置文件的相关信息

HKEY_DTN_DATA:包含了系统的动态信息,如即插即用设备

2.注册表值项类型

REG_BINARY: 二进制

REG_DWORD: 四字节

REG_SZ: 字符串

REG_Link: 字符串,文件路径

REG_NONE: 未知

3.对注册表的操作

头文件:

#include"Winreg.h"

#include"Windows.h"

#pragma comment(lib, "Advapi32")

(1)新建注册表项并写入默认项值内容

(a)RegCreateKey函数

HKEY Key;

CString skey = "Software//mingrisoft";

CString strdata = "www.mingrisoft.com";

RegCreateKey(HKEY_CURRENT_USER, skey, &Key);

if(ERROR_SUCCESS == RegSetValue(Key, "", REG_SZ, strdata.GetBuffer (strdata.GetLength ()), strdata.GetLength ()))

{

MessageBox("写入成功!");

RegCloseKey(Key);

}

(b)RegCreateKeyEx函数

HKEY key;

DWORD dispos;

SECURITY_ATTRIBUTES sa;

CString skey = "Software//mingrisoft";

CString strdata = "www.mingrisoft.com--Ex";

sa.nLength = sizeof(SECURITY_ATTRIBUTES);

sa.bInheritHandle = TRUE;

sa.lpSecurityDescriptor = NULL;

DWORD value = 1;

long iret = RegCreateKeyEx(HKEY_CURRENT_USER, skey, NULL, NULL,

REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,

&sa, &key, &dispos);

if(iret == 0)

{

RegSetValueEx(key, "", NULL, REG_SZ, (unsigned char *)strdata.GetBuffer (strdata.GetLength ()), strdata.GetLength ());

RegCloseKey(key);

}

else

{

CString strerr;

strerr.Format ("函数返回值%d", iret);

MessageBox(strerr, "出错", MB_OK);

}

(2)在注册表项中写新项值

//打开注册表项

HKEY key;

DWORD dispos;

SECURITY_ATTRIBUTES sa;

CString skey = "Software//mingrisoft";

CString strdata = "www.mingrisoft.com--Ex";

sa.nLength = sizeof(SECURITY_ATTRIBUTES);

sa.bInheritHandle = TRUE;

sa.lpSecurityDescriptor = NULL;

DWORD value = 1;

long iret = RegCreateKeyEx(HKEY_CURRENT_USER, skey, NULL, NULL,

REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,

&sa, &key, &dispos);

if(iret == 0)

{

//在注册表项中加入项值名为"bin",项值内容为"www.mingrisoft.com--Ex"的项值

RegSetValueEx(key, "bin", NULL, REG_SZ, (unsigned char *)strdata.GetBuffer (strdata.GetLength ()), strdata.GetLength ());

RegCloseKey(key);

}

else

{

CString strerr;

strerr.Format ("函数返回值%d", iret);

MessageBox(strerr, "出错", MB_OK);

}

(3)查询注册表项的默认项值内容

CString skey = "Software//mingrisoft";

long size;

char buf[128];

int iret = RegQueryValue(HKEY_CURRENT_USER, skey, buf, &size);

if(0 == iret)

{

CString Value;

buf[size] = 0;

Value.Format ("%s", buf);

MessageBox(Value, "值项是", MB_OK);

}

(4)查询注册表项的指定项值内容

//打开注册表项

HKEY key;

DWORD dispos;

SECURITY_ATTRIBUTES sa;

CString skey = "Software//mingrisoft";

sa.nLength = sizeof(SECURITY_ATTRIBUTES);

sa.bInheritHandle = TRUE;

sa.lpSecurityDescriptor = NULL;

DWORD value = 1;

long iret = RegCreateKeyEx(HKEY_CURRENT_USER, skey, NULL, NULL,

REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,

&sa, &key, &dispos);

if(0 == iret)

{

DWORD size;

char buf[128];

DWORD a = REG_SZ;

//返回项值名为bin的项值内容

int ret = RegQueryValueEx(key, "bin", NULL, &a, (BYTE *)buf, &size);

if(0 == ret)

{

buf[size] = 0;

CString Value;

Value.Format ("%s", buf);

MessageBox(Value, "值项是", MB_OK);

}

}

(5)删除指定的注册表项项值

//打开注册表项

HKEY key;

DWORD dispos;

SECURITY_ATTRIBUTES sa;

CString skey = "Software//mingrisoft";

CString strdata = "www.mingrisoft.com--Ex";

sa.nLength = sizeof(SECURITY_ATTRIBUTES);

sa.bInheritHandle = TRUE;

sa.lpSecurityDescriptor = NULL;

DWORD value = 1;

long iret = RegCreateKeyEx(HKEY_CURRENT_USER, skey, NULL, NULL,

REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,

&sa, &key, &dispos);

if(0 == iret)

{ //删除项值名为bin的项值

RegDeleteValue(key, "bin");

RegCloseKey(key);

}

(6)删除注册表项

//打开注册表项

HKEY key;

DWORD dispos;

SECURITY_ATTRIBUTES sa;

CString skey = "Software";

CString strdata = "www.mingrisoft.com--Ex";

sa.nLength = sizeof(SECURITY_ATTRIBUTES);

sa.bInheritHandle = TRUE;

sa.lpSecurityDescriptor = NULL;

DWORD value = 1;

long iret = RegCreateKeyEx(HKEY_CURRENT_USER, skey, NULL, NULL,

REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,

&sa, &key, &dispos);

if(0 == iret)

{ //删除"mingrisoft"项

RegDeleteKey(key, "mingrisoft");

RegCloseKey(key);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: