65.9K
CodeProject 正在变化。 阅读更多。
Home

注册表类

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.54/5 (25投票s)

2001年8月7日

2分钟阅读

viewsIcon

217602

downloadIcon

2699

一个使注册表易于使用的类

概述

我创建了这个类是为了简化注册表的操作。为此,我创建了 CRegistry 类。

CRegistry 类

CRegistry 类有一组函数,使使用注册表变得容易。

CRegistry::OpenKey

OpenKey 函数打开指定的注册表键。

BOOL OpenKey(enum Keys hKey, LPCTSTR szKey);

参数

enum Keys hKey - 句柄,指向以下任何预定义值

CRegistry::classesRoot

CRegistry::currentUser

CRegistry::localMachine

CRegistry::currentConfig

CRegistry::users

Windows NT/2000: CRegistry::performanceData

Windows 95/98: CRegistry::dynData

LPCTSTR szKey - 指向一个以 null 结尾的字符串的指针,该字符串包含要打开的键的名称。

如果函数成功,则返回 TRUE

请参阅示例


  CRegistry pReg;
  
  pReg.OpenKey(CRegistry::currentUser, "Entry1\\carlos1");
  CString str = _T("");

  if(pReg.GetValue("SZVAL1", str))
    AfxMessageBox("The Value SZVAL1 don't exists", MB_ICONWARNING);
    
  DWORD dwVal = 0;
  pReg.GetValue("DWVAL", dwVal);

  pReg.GetValue(NULL, str);
  pReg.CloseKey();

CRegistry::CreateKey

CreateKey 函数创建指定的注册表键。如果该键已存在于注册表中,则该函数将打开它。

BOOL CreateKey(enum Keys hKey, LPCTSTR szKey);

参数

enum Keys hKey - 句柄,指向以下任何预定义值

CRegistry::classesRoot

CRegistry::currentUser

CRegistry::localMachine

CRegistry::currentConfig

CRegistry::users

Windows NT/2000: CRegistry::performanceData

Windows 95/98: CRegistry::dynData

LPCTSTR szKey - 指向一个以 null 结尾的字符串的指针,该字符串包含要打开或创建的键的名称。

如果函数成功,则返回 TRUE

CRegistry::DeleteKey

DeleteKey 函数删除一个子键。

BOOL DeleteKey(enum Keys hKey, LPCTSTR szKey);

参数

enum Keys hKey - 句柄,指向以下任何预定义值

CRegistry::classesRoot

CRegistry::currentUser

CRegistry::localMachine

CRegistry::currentConfig

CRegistry::users

Windows NT/2000: CRegistry::performanceData

Windows 95/98: CRegistry::dynData

LPCTSTR szKey - 指向一个以 null 结尾的字符串的指针,该字符串包含要删除的键的名称。

如果函数成功,则返回 TRUE

CRegistry::GetValue

GetValue 函数从打开的注册表键中检索指定值名称的数据。

BOOL GetValue(LPCTSTR lpValueName, CString& strValue);
BOOL GetValue(LPCTSTR lpValueName, DWORD& dwValue);

参数

LPCTSTR lpValueName - 指向一个以 null 结尾的字符串的指针,该字符串包含要获取的值的名称

CString& strValue - 指向一个接收值的数据的缓冲区

DWORD& dwValue - 指向一个接收值的数据的缓冲区

如果函数成功,则返回 TRUE

请参阅示例

CRegistry::SetValue

SetValue 函数设置注册表键下指定值的类型和数据。

BOOL SetValue(LPCTSTR lpValueName, LPCTSTR lpData);
BOOL SetValue(LPCTSTR lpValueName, DWORD dwValue);

参数

LPCTSTR lpValueName - 指向一个以 null 结尾的字符串的指针,该字符串包含要设置的值的名称

CString& strValue - 指向一个包含要与指定值名称一起存储的数据的缓冲区

DWORD& dwValue - 指向一个包含要与指定值名称一起存储的数据的缓冲区

如果函数成功,则返回 TRUE

请参阅示例

pReg.OpenKey(CRegistry::currentUser, "Entry1\\carlos1");

pReg.SetValue("SZVAL", "STRVAL");
pReg.SetValue(NULL, "default");
pReg.SetValue("DWVAL", 34); 

pReg.CloseKey();

CRegistry::DeleteValue

DeleteValue 函数从指定的注册表键中删除一个命名值

BOOL DeleteValue(LPCTSTR lpValueName);

参数

LPCTSTR lpValueName - 指向一个以 null 结尾的字符串的指针,该字符串命名要删除的值。

如果函数成功,则返回 TRUE

请参阅示例

  CRegistry pReg;

  pReg.OpenKey(CRegistry::currentUser, "Entry1\\carlos1");

  pReg.DeleteValue("SZVAL");	
  pReg.CloseKey();

CRegistry::CloseKey

CloseKey 函数关闭注册表键。

void CloseKey();

请参阅示例

更新

2001年8月4日:版本 1.0 发布。

© . All rights reserved.