CReg类轻松读取注册表
来源:互联网 发布:java链接oracle数据库 编辑:程序博客网 时间:2024/05/01 15:29
//========================================================================//TITLE:// CReg类轻松读取注册表//AUTHOR:// norains//DATE:// Sunday 8-April-2007//Environment:// EVC4.0 + Standard SDK//========================================================================
开篇之处先说说这个类的来历.准确的说,这个类是我垂诞已久的东东,昨天在查看微软的控制面板的代码时,不小心发现的.觉得使用上挺便利的,所以本着"洋为中用"的原则,拷贝出来用吧!
/**////////////////////////////////////////////////////////////////////// // Reg.h: interface for the CReg class.////Version:// 1.0.0//Date:// 2007.04.07/**///////////////////////////////////////////////////////////////////////#ifndef REG_H#define REG_H class CReg ...{public: BOOL DeleteKey(LPCTSTR szName); BOOL DeleteValue(LPCTSTR szName); BOOL SetMultiSZ(LPCTSTR szName, LPCTSTR lpszValue, DWORD dwLen); BOOL SetBinary(LPCTSTR szName, LPBYTE lpbValue, DWORD dwLen); BOOL SetDW(LPCTSTR szName, DWORD dwValue); BOOL SetSZ(LPCTSTR szName, LPCTSTR szValue); BOOL SetSZ(LPCTSTR szName, LPCTSTR szValue, DWORD dwLen); DWORD GetValueDW(LPCTSTR szName, DWORD dwDefault=0); LPCTSTR GetValueSZ(LPCTSTR szName); LPBYTE GetValueBinary(LPCTSTR szName); DWORD GetValueBinary(LPCTSTR szName, LPBYTE lpbValue, DWORD dwLen); BOOL GetValueSZ(LPCTSTR szName, LPTSTR szValue, DWORD dwLen); BOOL EnumValue(LPTSTR pszName, DWORD dwLenName, LPTSTR pszValue, DWORD dwLenValue); BOOL EnumKey(LPTSTR psz, DWORD dwLen); BOOL IsOK(); operator HKEY(); void Reset(); CReg(HKEY hkRoot, LPCTSTR pszKey); BOOL Open(HKEY hkRoot, LPCTSTR pszKey, REGSAM sam=KEY_READ); BOOL Create(HKEY hkRoot, LPCTSTR pszKey); CReg(); virtual ~CReg();private: HKEY m_hKey; int m_Index; LPBYTE m_lpbValue; // last value read, if any};#endif //#ifndef REG_H//////////////////////////////////////////////////////////////////////// Reg.cpp: implementation of the CReg class.///**///////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "Reg.h"//=======================================================================//Macro define#define MyFree(p) { if(p) LocalFree(p); }//=======================================================================/**///////////////////////////////////////////////////////////////////////// Construction/Destruction/**///////////////////////////////////////////////////////////////////////CReg::CReg()...{ m_hKey = NULL; m_Index = 0; m_lpbValue = NULL;}CReg::CReg(HKEY hkRoot, LPCTSTR pszKey)...{ m_hKey = NULL; m_Index = 0; m_lpbValue = NULL; Open(hkRoot, pszKey);}CReg::~CReg()...{ if(m_hKey) ...{ RegCloseKey(m_hKey); } MyFree(m_lpbValue);}//-------------------------------------------------------------------//Description:// Create the key//-------------------------------------------------------------------BOOL CReg::Create(HKEY hkRoot, LPCTSTR pszKey)...{ DWORD dwDisp; return ERROR_SUCCESS == RegCreateKeyEx(hkRoot, pszKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hKey, &dwDisp);}//-------------------------------------------------------------------//Description:// Open the key//-------------------------------------------------------------------BOOL CReg::Open(HKEY hkRoot, LPCTSTR pszKey, REGSAM sam)...{ return ERROR_SUCCESS == RegOpenKeyEx(hkRoot, pszKey, 0, sam, &m_hKey);}//-------------------------------------------------------------------//Description:// Reset the value//-------------------------------------------------------------------void CReg::Reset()...{ if(m_hKey) ...{ RegCloseKey(m_hKey); } MyFree(m_lpbValue); m_hKey = NULL; m_Index = 0; m_lpbValue = NULL;}//-------------------------------------------------------------------//Description:// Operator overload//-------------------------------------------------------------------CReg::operator HKEY()...{ return m_hKey;}//-------------------------------------------------------------------//Description:// Test whether is the handle of the key OK for next operate//-------------------------------------------------------------------BOOL CReg::IsOK()...{ return m_hKey != NULL;}//-------------------------------------------------------------------//Description:// Enum the key//-------------------------------------------------------------------BOOL CReg::EnumKey(LPTSTR psz, DWORD dwLen)...{ if(!m_hKey) ...{ return FALSE; } return ERROR_SUCCESS == RegEnumKeyEx(m_hKey, m_Index++, psz, &dwLen, NULL, NULL, NULL, NULL);}//-------------------------------------------------------------------//Description:// Enum registry Value//-------------------------------------------------------------------BOOL CReg::EnumValue(LPTSTR pszName, DWORD dwLenName, LPTSTR pszValue, DWORD dwLenValue)...{ DWORD dwType; if(!m_hKey) ...{ return FALSE; } dwLenValue *= sizeof(TCHAR); // convert length in chars to bytes return ERROR_SUCCESS == RegEnumValue(m_hKey, m_Index++, pszName, &dwLenName, NULL, &dwType, (LPBYTE)pszValue, &dwLenValue);}//-------------------------------------------------------------------//Description:// Get the string value//-------------------------------------------------------------------BOOL CReg::GetValueSZ(LPCTSTR szName, LPTSTR szValue, DWORD dwLen)...{ if(!m_hKey) ...{ return FALSE; } dwLen *= sizeof(TCHAR); // convert length in chars to bytes return ERROR_SUCCESS == RegQueryValueEx(m_hKey, szName, NULL, NULL, (LPBYTE)szValue, &dwLen);}//-------------------------------------------------------------------//Description:// Get the binary value//-------------------------------------------------------------------DWORD CReg::GetValueBinary(LPCTSTR szName, LPBYTE lpbValue, DWORD dwLen)...{ if(!m_hKey) ...{ return FALSE; } DWORD dwLenWant = dwLen; if(ERROR_SUCCESS == RegQueryValueEx(m_hKey, szName, NULL, NULL, lpbValue, &dwLen)) ...{ return dwLen; } else ...{ return 0; }}//-------------------------------------------------------------------//Description:// Get the binary value//-------------------------------------------------------------------LPBYTE CReg::GetValueBinary(LPCTSTR szName)...{ return (LPBYTE)GetValueSZ(szName);}//-------------------------------------------------------------------//Description:// Get the string value//-------------------------------------------------------------------LPCTSTR CReg::GetValueSZ(LPCTSTR szName)...{ return 0;}//-------------------------------------------------------------------//Description:// Get the DWORD value////Parameters:// szName:[in] The value of registry// dwDefault:[in] The default value return when failed in getting the //DWORD value.//-------------------------------------------------------------------DWORD CReg::GetValueDW(LPCTSTR szName, DWORD dwDefault)...{ if(!m_hKey) ...{ return FALSE; } DWORD dwValue = dwDefault; DWORD dwLen = sizeof(DWORD); RegQueryValueEx(m_hKey, szName, NULL, NULL, (LPBYTE)&dwValue, &dwLen); return dwValue;}//-------------------------------------------------------------------//Description:// Set the string value//-------------------------------------------------------------------BOOL CReg::SetSZ(LPCTSTR szName, LPCTSTR szValue, DWORD dwLen)...{ //Prefix if(!m_hKey) ...{ return FALSE; } return ERROR_SUCCESS == RegSetValueEx(m_hKey, szName, 0, REG_SZ, (LPBYTE)szValue, sizeof(TCHAR)*dwLen);}//-------------------------------------------------------------------//Description:// Set the string value//-------------------------------------------------------------------BOOL CReg::SetSZ(LPCTSTR szName, LPCTSTR szValue)...{ return SetSZ(szName, szValue, 1+lstrlen(szValue));}//-------------------------------------------------------------------//Description:// Get the DWORD value//-------------------------------------------------------------------BOOL CReg::SetDW(LPCTSTR szName, DWORD dwValue)...{ //Prefix if(!m_hKey) ...{ return FALSE; } return ERROR_SUCCESS==RegSetValueEx(m_hKey, szName, 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(DWORD));}//-------------------------------------------------------------------//Description:// Get the binary value//-------------------------------------------------------------------BOOL CReg::SetBinary(LPCTSTR szName, LPBYTE lpbValue, DWORD dwLen)...{ //Prefix if(!m_hKey) ...{ return FALSE; } return ERROR_SUCCESS == RegSetValueEx(m_hKey, szName, 0, REG_BINARY, lpbValue, dwLen);}//-------------------------------------------------------------------//Description:// Set the Multi value//-------------------------------------------------------------------BOOL CReg::SetMultiSZ(LPCTSTR szName, LPCTSTR lpszValue, DWORD dwLen)...{ return ERROR_SUCCESS == RegSetValueEx(m_hKey, szName, 0, REG_MULTI_SZ, (LPBYTE)lpszValue, sizeof(TCHAR)*dwLen);}//-------------------------------------------------------------------//Description:// Delete the value//-------------------------------------------------------------------BOOL CReg::DeleteValue(LPCTSTR szName)...{ //Prefix if(!m_hKey) ...{ return FALSE; } // return ERROR_SUCCESS == RegDeleteValue(m_hKey, szName);} //-------------------------------------------------------------------//Description:// Delete Key//-------------------------------------------------------------------BOOL CReg::DeleteKey(LPCTSTR szName)...{ if(!m_hKey) ...{ return FALSE; } return ERROR_SUCCESS == RegDeleteKey(m_hKey, szName);}
使用CReg类读取注册表非常方便,至少不用再去翻查platform builder文档上的注册表函数了^_^.
以将注册表中"HKEY_CURRENT_USER/ControlPanel/Volume"的"Screen"(DWORD类型)数值设置为2作例子: CReg reg(HKEY_CURRENT_USER,TEXT("ControlPanel/Volume"));
reg.SetDW(TEXT("Screen"),Screen);
要读取也挺简单:
DWORD dwValue = reg.GetValueDW(TEXT("Screen"));
其它函数的功能,可以依照函数名推断,并且使用方法也很简单,在此不再赘述.
- CReg类轻松读取注册表
- CReg类轻松读取注册表
- CReg类轻松读取注册表
- CReg类轻松读取注册表
- 读取注册表
- 在Delphi中轻松操作注册表TRegistry类
- Qt 类QSettings,读取ini与注册表
- 技巧:在Delphi中轻松操作注册表轻松操作注册表
- VB.NET 轻松操作注册表
- VB.NET轻松操作注册表
- VB.NET轻松操作注册表
- C# 操作注册表 写入注册表 修改注册表 读取注册表
- C# 操作注册表 写入注册表 修改注册表 读取注册表
- 读取注册表键值
- C++ 注册表读取函数
- 读取注册表(delphi)
- 读取注册表值
- VB读取注册表
- 推荐13款JavaScript图形和图表绘制工具
- 利用python线程编程实现生产者与消费者关系
- TRAC 以及 nexus
- 关于LDO芯片的选型问题
- 关于PE文件格式中IMAGE_OPTIONAL_HEADER.FileAlignment的一些说明
- CReg类轻松读取注册表
- 基于 Android NDK 的学习之旅-----目录
- Windbg检查托管代码的内存泄露
- 关于firefox(火狐浏览器)document.all和document.layers
- 学会控制自己的情绪
- 至强7500 CPU介绍
- String对象的==判断
- 开学第一周
- 基于 Android NDK 的学习之旅-----序言