//========================================================================
//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"));
其它函数的功能,可以依照函数名推断,并且使用方法也很简单,在此不再赘述.
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/norains/archive/2007/04/08/1556296.aspx