//========================================================================
//TITLE:
// CReg更新至V1.1.0
//AUTHOR:
// norains
//DATE:
// wednesday 60-June-2007
//Environment:
// EVC4.0 + Standard SDK
//========================================================================
v1.0.0版本及具体用法:http://blog.youkuaiyun.com/norains/archive/2007/04/08/1556296.aspx
相对V1.0.0版本的改进:
1.删除难以实现并且容易出问题的两个函数: LPCTSTR GetValueSZ(LPCTSTR szName), LPBYTE GetValueBinary(LPCTSTR szName);
2.修改如下函数的声明:EnumValue,GetValueSZ
3.完善部分函数的说明
4.增加如下函数:GetValueType,CheckKeyExist
5.修正一些已知的bug
/ //
// Reg.h:interfacefortheCRegclass.
//
// Version:
// 1.1.0
// Date:
// 2007.05.28
/ /
#ifndefREG_H
#define REG_H
// ---------------------------------------------------------------------
enum ValueType
{
VAL_ERROR,
VAL_DWORD,
VAL_MULTISZ,
VAL_SZ,
VAL_BINARY
};
// ---------------------------------------------------------------------
class CReg
{
public :
BOOLCheckKeyExist(HKEYhkRoot,LPCTSTRpszKey);
ValueTypeGetValueType(LPCTSTRszName);
BOOLDeleteKey(LPCTSTRszName);
BOOLDeleteValue(LPCTSTRszName);
BOOLSetMultiSZ(LPCTSTRszName,LPCTSTRlpszValue,DWORDdwLen);
BOOLSetBinary(LPCTSTRszName,LPBYTElpbValue,DWORDdwLen);
BOOLSetDW(LPCTSTRszName,DWORDdwValue);
BOOLSetSZ(LPCTSTRszName,LPCTSTRszValue);
BOOLSetSZ(LPCTSTRszName,LPCTSTRszValue,DWORDdwLen);
DWORDGetValueDW(LPCTSTRszName,DWORDdwDefault = 0 );
DWORDGetValueBinary(LPCTSTRszName,LPBYTElpbValue,DWORDdwLen);
DWORDGetValueSZ(LPCTSTRszName,LPTSTRszValue,DWORDdwLen);
BOOLEnumValue(LPTSTRpszName,DWORDdwLenName,LPBYTElpValue,DWORDdwLenValue);
BOOLEnumKey(LPTSTRpsz,DWORDdwLen);
BOOLIsOK();
operator HKEY();
void Reset();
CReg(HKEYhkRoot,LPCTSTRpszKey);
BOOLOpen(HKEYhkRoot,LPCTSTRpszKey,REGSAMsam = KEY_READ);
BOOLCreate(HKEYhkRoot,LPCTSTRpszKey);
CReg();
virtual ~ CReg();
private :
HKEYm_hKey;
int m_Index;
LPBYTEm_lpbValue; // lastvalueread,ifany
};
#endif // #ifndefREG_H
/ /
// Reg.cpp:implementationoftheCRegclass.
//
/ /
#include " stdafx.h "
#include " Reg.h "
// =======================================================================
// Macrodefine
#define MyFree(p){if(p)LocalFree(p);}
// =======================================================================
/ /
// Construction/Destruction
/ /
// -------------------------------------------------------------------
// Description:
// Constructionandopenthekey
// -------------------------------------------------------------------
CReg::CReg()
{
m_hKey = NULL;
m_Index = 0 ;
m_lpbValue = NULL;
}
// -------------------------------------------------------------------
// Description:
// Constructionandopenthekey
//
// Parameters:
// hkRoot:[in]Therootkey
// pszKey:[in]Thekeytoopen
// -------------------------------------------------------------------
CReg::CReg(HKEYhkRoot,LPCTSTRpszKey)
{
m_hKey = NULL;
m_Index = 0 ;
m_lpbValue = NULL;
Open(hkRoot,pszKey);
}
// -------------------------------------------------------------------
// Description:
// Destruction
// -------------------------------------------------------------------
CReg:: ~ CReg()
{
if (m_hKey)
{
RegCloseKey(m_hKey);
}
MyFree(m_lpbValue);
}
// -------------------------------------------------------------------
// Description:
// Thisfunctioncreatesthespecifiedkey.
// Ifthekeyalreadyexistsintheregistry,thefunctionopensit.
// Aftersucceedincallingthefuction,shouldcalltheReset()toreleasetheresource.
//
// Parameters:
// hkRoot:[in]Therootkey
// pszKey:[in]Thekeytocreate
// -------------------------------------------------------------------
BOOLCReg::Create(HKEYhkRoot,LPCTSTRpszKey)
{
DWORDdwDisp;
return ERROR_SUCCESS == RegCreateKeyEx(hkRoot,pszKey, 0 ,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL, & m_hKey, & dwDisp);
}
// -------------------------------------------------------------------
// Description:
// Thisfunctionopensthespecifiedkey.
// Aftersucceedincallingthefuction,shouldcalltheReset()toreleasetheresource.
//
// Parameters:
// hkRoot:[in]Therootkey
// pszKey:[in]Thekeytoopen
// sam:[in]Notsupported;setto0
// -------------------------------------------------------------------
BOOLCReg::Open(HKEYhkRoot,LPCTSTRpszKey,REGSAMsam)
{
return ERROR_SUCCESS == RegOpenKeyEx(hkRoot,pszKey, 0 ,sam, & m_hKey);
}
// -------------------------------------------------------------------
// Description:
// Resetthevalueandreleasetheresource.
// -------------------------------------------------------------------
void CReg::Reset()
{
if (m_hKey)
{
RegCloseKey(m_hKey);
}
MyFree(m_lpbValue);
m_hKey = NULL;
m_Index = 0 ;
m_lpbValue = NULL;
}
// -------------------------------------------------------------------
// Description:
// Operatoroverload
// -------------------------------------------------------------------
CReg:: operator HKEY()
{
return m_hKey;
}
// -------------------------------------------------------------------
// Description:
// Checkwhetherisreadyfornextoperate
//
// Returnvalue:
// TRUE:Readytodonextoperate
// FALSE:Notready.
// -------------------------------------------------------------------
BOOLCReg::IsOK()
{
return m_hKey != NULL;
}
// -------------------------------------------------------------------
// Description:
// Thisfunctionenumeratessubkeysofthespecifiedopenregistrykey.
//
// Parameters:
// psz:[out]Pointertoabufferthatreceivesthenameofthesubkey,includingtheterminatingnullcharacter.
// Thefunctioncopiesonlythenameofthesubkey,notthefullkeyhierarchy,tothebuffer.
// dwLen:[in]Specifiesthelengthofthebufferpointedtobythepszparameter.
// -------------------------------------------------------------------
BOOLCReg::EnumKey(LPTSTRpsz,DWORDdwLen)
{
if ( ! m_hKey)
{
return FALSE;
}
dwLen *= sizeof (TCHAR);
return ERROR_SUCCESS == RegEnumKeyEx(m_hKey,m_Index ++ ,psz, & dwLen,NULL,NULL,NULL,NULL);
}
// -------------------------------------------------------------------
// Description:
// Thisfunctionenumeratesthevaluesforthespecifiedopenregistrykey.
//
// Parameters:
// pszName:[out]Pointertoabufferthatreceivesthenameofthevalue,includingtheterminatingnullcharacter.
// dwLenName:[in]SpecifiesthelengthofthebufferpointedtobythepszNameparameter.
// lpValue:[out]Pointertoabufferthatreceivesthedataforthevalueentry.ThisparametercanbeNULLifthedataisnotrequired.
// dwLenValue:[in]SpecifiesthelengthofthebufferpointedtobythelpValueparameter.
// -------------------------------------------------------------------
BOOLCReg::EnumValue(LPTSTRpszName,DWORDdwLenName,LPBYTElpValue,DWORDdwLenValue)
{
DWORDdwType;
if ( ! m_hKey)
{
return FALSE;
}
dwLenValue *= sizeof (BYTE); // convertlengthinnumbertobytes
return ERROR_SUCCESS == RegEnumValue(m_hKey,m_Index ++ ,pszName, & dwLenName,NULL, & dwType,(LPBYTE)lpValue, & dwLenValue);
}
// --------------------------------------------------------------------------------------------------------------
// Description:
// Getthestringvalue
//
// Parameters:
// szName:[in]Pointertoastringcontainingthenameofthevaluetoquery
// szValue:[out]Pointertoabufferthatreceivesthevalue'sdata.
// Ifit'snull,itwillreturnthesizeofbufferneed.
// dwLen:[in]SpecifiesthelengthofthebufferpointedtobytheszValueparameter.
// IfszValueisnull,theparameterwouldignore.
//
// Returnvalue:
// Thenumberofthecharactershavebeenget.
// 0meansfailed.Othersmeanssucceed,andthevalueincludethenullcharacter
// ----------------------------------------------------------------------------------------------------------------------
DWORDCReg::GetValueSZ(LPCTSTRszName,LPTSTRszValue,DWORDdwLen)
{
if ( ! m_hKey)
{
return FALSE;
}
dwLen *= sizeof (TCHAR); // convertlengthincharstobytes
DWORDdwType;
if (ERROR_SUCCESS == RegQueryValueEx(m_hKey,szName,NULL, & dwType,(LPBYTE)szValue, & dwLen))
{
if (REG_SZ != dwType)
{
dwLen = 0 ;
return FALSE;
}
else
{
dwLen /= sizeof (TCHAR);
}
}
else
{
dwLen = 0 ;
}
return dwLen;
}
// -------------------------------------------------------------------
// Description:
// Getthebinaryvalue
//
// Parameters:
// szName:[in]Pointertoastringcontainingthenameofthevaluetoquery
// lpbValue:[out]Pointertoabufferthatreceivesthevalue'sdata.
// Ifit'snull,itwillreturnthesizeofbufferneed.
// dwLen:[in]SpecifiesthelengthofthebufferpointedtobythelpbValueparameter.
// IfszValueisnull,theparameterwouldignore.
//
// Returnvalue:
// Thenumberofthebinaryhavebeenget.
// 0meansfailed.Othersmeanssucceed,andthevalueincludethenullcharacter
// -------------------------------------------------------------------
DWORDCReg::GetValueBinary(LPCTSTRszName,LPBYTElpbValue,DWORDdwLen)
{
if ( ! m_hKey)
{
return FALSE;
}
DWORDdwType;
if (ERROR_SUCCESS == RegQueryValueEx(m_hKey,szName,NULL, & dwType,lpbValue, & dwLen))
{
if (dwType != REG_BINARY)
{
dwLen = 0 ;
}
}
else
{
dwLen = 0 ;
}
return dwLen;
}
// -------------------------------------------------------------------
// Description:
// GettheDWORDvalue
//
// Parameters:
// szName:[in]Thevalueofregistry
// dwDefault:[in]ThedefaultvaluereturnwhenfailedingettingtheDWORDvalue.
//
// Returnvalue:
// IfthereturnvaluejustbesameasthedwDefault,itmaymeanfailed.
// -------------------------------------------------------------------
DWORDCReg::GetValueDW(LPCTSTRszName,DWORDdwDefault)
{
if ( ! m_hKey)
{
return FALSE;
}
DWORDdwValue = dwDefault;
DWORDdwLen = sizeof (DWORD);
DWORDdwType;
if (ERROR_SUCCESS == RegQueryValueEx(m_hKey,szName,NULL, & dwType,(LPBYTE) & dwValue, & dwLen))
{
if (dwType != REG_DWORD)
{
dwValue = dwDefault;
}
}
else
{
dwValue = dwDefault;
}
return dwValue;
}
// -------------------------------------------------------------------
// Description:
// Setthestringvalue
//
// Parameters:
// szName:[in]Pointertoastringcontainingthenameofthevaluetoset
// szValue:[in]Thebufferincludethedatatobeset
// dwLen:[in]SpecifiesthelengthofthebufferpointedtobytheszValueparameter.
// -------------------------------------------------------------------
BOOLCReg::SetSZ(LPCTSTRszName,LPCTSTRszValue,DWORDdwLen)
{
// Prefix
if ( ! m_hKey)
{
return FALSE;
}
return ERROR_SUCCESS == RegSetValueEx(m_hKey,szName, 0 ,REG_SZ,(LPBYTE)szValue, sizeof (TCHAR) * dwLen);
}
// -------------------------------------------------------------------
// Description:
// Setthestringvalue
//
// Parameters:
// szName:[in]Pointertoastringcontainingthenameofthevaluetoset
// szValue:[in]Thestringbufferincludenullcharactertobeset
// -------------------------------------------------------------------
BOOLCReg::SetSZ(LPCTSTRszName,LPCTSTRszValue)
{
return SetSZ(szName,szValue, 1 + _tcslen(szValue));
}
// -------------------------------------------------------------------
// Description:
// GettheDWORDvalue
//
// Parameters:
// szName:[in]Pointertoastringcontainingthenameofthevaluetoset
// dwValue:[in]Thevaluetobeset
// -------------------------------------------------------------------
BOOLCReg::SetDW(LPCTSTRszName,DWORDdwValue)
{
// Prefix
if ( ! m_hKey)
{
return FALSE;
}
return ERROR_SUCCESS == RegSetValueEx(m_hKey,szName, 0 ,REG_DWORD,(LPBYTE) & dwValue, sizeof (DWORD));
}
// -------------------------------------------------------------------
// Description:
// Getthebinaryvalue
//
// Parameters:
// szName:[in]Pointertoastringcontainingthenameofthevaluetoset
// lpbValue:[in]Thebufferincludethedatatobeset
// dwLen:[in]SpecifiesthelengthofthebufferpointedtobythelpbValueparameter.
// -------------------------------------------------------------------
BOOLCReg::SetBinary(LPCTSTRszName,LPBYTElpbValue,DWORDdwLen)
{
// Prefix
if ( ! m_hKey)
{
return FALSE;
}
return ERROR_SUCCESS == RegSetValueEx(m_hKey,szName, 0 ,REG_BINARY,lpbValue, sizeof (BYTE) * dwLen);
}
// -------------------------------------------------------------------
// Description:
// SettheMultivalue
//
// Parameters:
// szName:[in]Pointertoastringcontainingthenameofthevaluetoset
// lpszValue:[in]Thebufferincludethedatatobeset
// dwLen:[in]SpecifiesthelengthofthebufferpointedtobythelpszValueparameter.
// -------------------------------------------------------------------
BOOLCReg::SetMultiSZ(LPCTSTRszName,LPCTSTRlpszValue,DWORDdwLen)
{
return ERROR_SUCCESS == RegSetValueEx(m_hKey,szName, 0 ,REG_MULTI_SZ,(LPBYTE)lpszValue, sizeof (TCHAR) * dwLen);
}
// -------------------------------------------------------------------
// Description:
// Thisfunctionremovesanamedvaluefromthespecifiedregistrykey.
//
// Parameters:
// szName:[in]Pointertoanull-terminatedstringthatnamesthevaluetoremove.
// -------------------------------------------------------------------
BOOLCReg::DeleteValue(LPCTSTRszName)
{
// Prefix
if ( ! m_hKey)
{
return FALSE;
}
//
return ERROR_SUCCESS == RegDeleteValue(m_hKey,szName);
}
// -------------------------------------------------------------------
// Description:
// Thisfunctionrecursivelydeletesallsubkeysofanamedsubkeyofaspecifiedregistrykey.
//
// Parameters:
// szName:[in]Pointertoanull-terminatedstringspecifyingthenameofthekeytodelete.ThisparametercannotbeNULL.
// -------------------------------------------------------------------
BOOLCReg::DeleteKey(LPCTSTRszName)
{
if ( ! m_hKey)
{
return FALSE;
}
return ERROR_SUCCESS == RegDeleteKey(m_hKey,szName);
}
// -------------------------------------------------------------------
// Description:
// Getthevaluetype
//
// Parameters:
// szName:[in]Pointertoanull-terminatedstringthatnamesthevalue
//
// ------------------------------------------------------------------------
ValueTypeCReg::GetValueType(LPCTSTRszName)
{
if (m_hKey == NULL)
{
return VAL_ERROR;
}
DWORDdwType;
if (ERROR_SUCCESS != RegQueryValueEx(m_hKey,szName,NULL, & dwType, 0 , 0 ))
{
return VAL_ERROR;
}
switch (dwType)
{
case REG_BINARY:
return VAL_BINARY;
case REG_DWORD:
case REG_DWORD_BIG_ENDIAN:
return VAL_DWORD;
case REG_EXPAND_SZ:
case REG_SZ:
return VAL_SZ;
case REG_MULTI_SZ:
return VAL_MULTISZ;
default :
return VAL_ERROR; // Thereareothertypes,butnotsupportedbyCReg
}
}
// -------------------------------------------------------------------
// Description:
// Checkthekeyexist.
//
// Parameters:
// pszKey:[in]Pointertoanull-terminatedstringthatnameskey
//
// ------------------------------------------------------------------------
BOOLCReg::CheckKeyExist(HKEYhkRoot,LPCTSTRpszKey)
{
HKEYhKey = 0 ;
if (ERROR_SUCCESS == RegOpenKeyEx(hkRoot,pszKey, 0 , 0 , & hKey))
{
RegCloseKey(hKey);
return TRUE;
}
else
{
return FALSE;
}
}
//TITLE:
// CReg更新至V1.1.0
//AUTHOR:
// norains
//DATE:
// wednesday 60-June-2007
//Environment:
// EVC4.0 + Standard SDK
//========================================================================
v1.0.0版本及具体用法:http://blog.youkuaiyun.com/norains/archive/2007/04/08/1556296.aspx
相对V1.0.0版本的改进:
1.删除难以实现并且容易出问题的两个函数: LPCTSTR GetValueSZ(LPCTSTR szName), LPBYTE GetValueBinary(LPCTSTR szName);
2.修改如下函数的声明:EnumValue,GetValueSZ
3.完善部分函数的说明
4.增加如下函数:GetValueType,CheckKeyExist
5.修正一些已知的bug
/ //
// Reg.h:interfacefortheCRegclass.
//
// Version:
// 1.1.0
// Date:
// 2007.05.28
/ /
#ifndefREG_H
#define REG_H
// ---------------------------------------------------------------------
enum ValueType
{
VAL_ERROR,
VAL_DWORD,
VAL_MULTISZ,
VAL_SZ,
VAL_BINARY
};
// ---------------------------------------------------------------------
class CReg
{
public :
BOOLCheckKeyExist(HKEYhkRoot,LPCTSTRpszKey);
ValueTypeGetValueType(LPCTSTRszName);
BOOLDeleteKey(LPCTSTRszName);
BOOLDeleteValue(LPCTSTRszName);
BOOLSetMultiSZ(LPCTSTRszName,LPCTSTRlpszValue,DWORDdwLen);
BOOLSetBinary(LPCTSTRszName,LPBYTElpbValue,DWORDdwLen);
BOOLSetDW(LPCTSTRszName,DWORDdwValue);
BOOLSetSZ(LPCTSTRszName,LPCTSTRszValue);
BOOLSetSZ(LPCTSTRszName,LPCTSTRszValue,DWORDdwLen);
DWORDGetValueDW(LPCTSTRszName,DWORDdwDefault = 0 );
DWORDGetValueBinary(LPCTSTRszName,LPBYTElpbValue,DWORDdwLen);
DWORDGetValueSZ(LPCTSTRszName,LPTSTRszValue,DWORDdwLen);
BOOLEnumValue(LPTSTRpszName,DWORDdwLenName,LPBYTElpValue,DWORDdwLenValue);
BOOLEnumKey(LPTSTRpsz,DWORDdwLen);
BOOLIsOK();
operator HKEY();
void Reset();
CReg(HKEYhkRoot,LPCTSTRpszKey);
BOOLOpen(HKEYhkRoot,LPCTSTRpszKey,REGSAMsam = KEY_READ);
BOOLCreate(HKEYhkRoot,LPCTSTRpszKey);
CReg();
virtual ~ CReg();
private :
HKEYm_hKey;
int m_Index;
LPBYTEm_lpbValue; // lastvalueread,ifany
};
#endif // #ifndefREG_H
/ /
// Reg.cpp:implementationoftheCRegclass.
//
/ /
#include " stdafx.h "
#include " Reg.h "
// =======================================================================
// Macrodefine
#define MyFree(p){if(p)LocalFree(p);}
// =======================================================================
/ /
// Construction/Destruction
/ /
// -------------------------------------------------------------------
// Description:
// Constructionandopenthekey
// -------------------------------------------------------------------
CReg::CReg()
{
m_hKey = NULL;
m_Index = 0 ;
m_lpbValue = NULL;
}
// -------------------------------------------------------------------
// Description:
// Constructionandopenthekey
//
// Parameters:
// hkRoot:[in]Therootkey
// pszKey:[in]Thekeytoopen
// -------------------------------------------------------------------
CReg::CReg(HKEYhkRoot,LPCTSTRpszKey)
{
m_hKey = NULL;
m_Index = 0 ;
m_lpbValue = NULL;
Open(hkRoot,pszKey);
}
// -------------------------------------------------------------------
// Description:
// Destruction
// -------------------------------------------------------------------
CReg:: ~ CReg()
{
if (m_hKey)
{
RegCloseKey(m_hKey);
}
MyFree(m_lpbValue);
}
// -------------------------------------------------------------------
// Description:
// Thisfunctioncreatesthespecifiedkey.
// Ifthekeyalreadyexistsintheregistry,thefunctionopensit.
// Aftersucceedincallingthefuction,shouldcalltheReset()toreleasetheresource.
//
// Parameters:
// hkRoot:[in]Therootkey
// pszKey:[in]Thekeytocreate
// -------------------------------------------------------------------
BOOLCReg::Create(HKEYhkRoot,LPCTSTRpszKey)
{
DWORDdwDisp;
return ERROR_SUCCESS == RegCreateKeyEx(hkRoot,pszKey, 0 ,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL, & m_hKey, & dwDisp);
}
// -------------------------------------------------------------------
// Description:
// Thisfunctionopensthespecifiedkey.
// Aftersucceedincallingthefuction,shouldcalltheReset()toreleasetheresource.
//
// Parameters:
// hkRoot:[in]Therootkey
// pszKey:[in]Thekeytoopen
// sam:[in]Notsupported;setto0
// -------------------------------------------------------------------
BOOLCReg::Open(HKEYhkRoot,LPCTSTRpszKey,REGSAMsam)
{
return ERROR_SUCCESS == RegOpenKeyEx(hkRoot,pszKey, 0 ,sam, & m_hKey);
}
// -------------------------------------------------------------------
// Description:
// Resetthevalueandreleasetheresource.
// -------------------------------------------------------------------
void CReg::Reset()
{
if (m_hKey)
{
RegCloseKey(m_hKey);
}
MyFree(m_lpbValue);
m_hKey = NULL;
m_Index = 0 ;
m_lpbValue = NULL;
}
// -------------------------------------------------------------------
// Description:
// Operatoroverload
// -------------------------------------------------------------------
CReg:: operator HKEY()
{
return m_hKey;
}
// -------------------------------------------------------------------
// Description:
// Checkwhetherisreadyfornextoperate
//
// Returnvalue:
// TRUE:Readytodonextoperate
// FALSE:Notready.
// -------------------------------------------------------------------
BOOLCReg::IsOK()
{
return m_hKey != NULL;
}
// -------------------------------------------------------------------
// Description:
// Thisfunctionenumeratessubkeysofthespecifiedopenregistrykey.
//
// Parameters:
// psz:[out]Pointertoabufferthatreceivesthenameofthesubkey,includingtheterminatingnullcharacter.
// Thefunctioncopiesonlythenameofthesubkey,notthefullkeyhierarchy,tothebuffer.
// dwLen:[in]Specifiesthelengthofthebufferpointedtobythepszparameter.
// -------------------------------------------------------------------
BOOLCReg::EnumKey(LPTSTRpsz,DWORDdwLen)
{
if ( ! m_hKey)
{
return FALSE;
}
dwLen *= sizeof (TCHAR);
return ERROR_SUCCESS == RegEnumKeyEx(m_hKey,m_Index ++ ,psz, & dwLen,NULL,NULL,NULL,NULL);
}
// -------------------------------------------------------------------
// Description:
// Thisfunctionenumeratesthevaluesforthespecifiedopenregistrykey.
//
// Parameters:
// pszName:[out]Pointertoabufferthatreceivesthenameofthevalue,includingtheterminatingnullcharacter.
// dwLenName:[in]SpecifiesthelengthofthebufferpointedtobythepszNameparameter.
// lpValue:[out]Pointertoabufferthatreceivesthedataforthevalueentry.ThisparametercanbeNULLifthedataisnotrequired.
// dwLenValue:[in]SpecifiesthelengthofthebufferpointedtobythelpValueparameter.
// -------------------------------------------------------------------
BOOLCReg::EnumValue(LPTSTRpszName,DWORDdwLenName,LPBYTElpValue,DWORDdwLenValue)
{
DWORDdwType;
if ( ! m_hKey)
{
return FALSE;
}
dwLenValue *= sizeof (BYTE); // convertlengthinnumbertobytes
return ERROR_SUCCESS == RegEnumValue(m_hKey,m_Index ++ ,pszName, & dwLenName,NULL, & dwType,(LPBYTE)lpValue, & dwLenValue);
}
// --------------------------------------------------------------------------------------------------------------
// Description:
// Getthestringvalue
//
// Parameters:
// szName:[in]Pointertoastringcontainingthenameofthevaluetoquery
// szValue:[out]Pointertoabufferthatreceivesthevalue'sdata.
// Ifit'snull,itwillreturnthesizeofbufferneed.
// dwLen:[in]SpecifiesthelengthofthebufferpointedtobytheszValueparameter.
// IfszValueisnull,theparameterwouldignore.
//
// Returnvalue:
// Thenumberofthecharactershavebeenget.
// 0meansfailed.Othersmeanssucceed,andthevalueincludethenullcharacter
// ----------------------------------------------------------------------------------------------------------------------
DWORDCReg::GetValueSZ(LPCTSTRszName,LPTSTRszValue,DWORDdwLen)
{
if ( ! m_hKey)
{
return FALSE;
}
dwLen *= sizeof (TCHAR); // convertlengthincharstobytes
DWORDdwType;
if (ERROR_SUCCESS == RegQueryValueEx(m_hKey,szName,NULL, & dwType,(LPBYTE)szValue, & dwLen))
{
if (REG_SZ != dwType)
{
dwLen = 0 ;
return FALSE;
}
else
{
dwLen /= sizeof (TCHAR);
}
}
else
{
dwLen = 0 ;
}
return dwLen;
}
// -------------------------------------------------------------------
// Description:
// Getthebinaryvalue
//
// Parameters:
// szName:[in]Pointertoastringcontainingthenameofthevaluetoquery
// lpbValue:[out]Pointertoabufferthatreceivesthevalue'sdata.
// Ifit'snull,itwillreturnthesizeofbufferneed.
// dwLen:[in]SpecifiesthelengthofthebufferpointedtobythelpbValueparameter.
// IfszValueisnull,theparameterwouldignore.
//
// Returnvalue:
// Thenumberofthebinaryhavebeenget.
// 0meansfailed.Othersmeanssucceed,andthevalueincludethenullcharacter
// -------------------------------------------------------------------
DWORDCReg::GetValueBinary(LPCTSTRszName,LPBYTElpbValue,DWORDdwLen)
{
if ( ! m_hKey)
{
return FALSE;
}
DWORDdwType;
if (ERROR_SUCCESS == RegQueryValueEx(m_hKey,szName,NULL, & dwType,lpbValue, & dwLen))
{
if (dwType != REG_BINARY)
{
dwLen = 0 ;
}
}
else
{
dwLen = 0 ;
}
return dwLen;
}
// -------------------------------------------------------------------
// Description:
// GettheDWORDvalue
//
// Parameters:
// szName:[in]Thevalueofregistry
// dwDefault:[in]ThedefaultvaluereturnwhenfailedingettingtheDWORDvalue.
//
// Returnvalue:
// IfthereturnvaluejustbesameasthedwDefault,itmaymeanfailed.
// -------------------------------------------------------------------
DWORDCReg::GetValueDW(LPCTSTRszName,DWORDdwDefault)
{
if ( ! m_hKey)
{
return FALSE;
}
DWORDdwValue = dwDefault;
DWORDdwLen = sizeof (DWORD);
DWORDdwType;
if (ERROR_SUCCESS == RegQueryValueEx(m_hKey,szName,NULL, & dwType,(LPBYTE) & dwValue, & dwLen))
{
if (dwType != REG_DWORD)
{
dwValue = dwDefault;
}
}
else
{
dwValue = dwDefault;
}
return dwValue;
}
// -------------------------------------------------------------------
// Description:
// Setthestringvalue
//
// Parameters:
// szName:[in]Pointertoastringcontainingthenameofthevaluetoset
// szValue:[in]Thebufferincludethedatatobeset
// dwLen:[in]SpecifiesthelengthofthebufferpointedtobytheszValueparameter.
// -------------------------------------------------------------------
BOOLCReg::SetSZ(LPCTSTRszName,LPCTSTRszValue,DWORDdwLen)
{
// Prefix
if ( ! m_hKey)
{
return FALSE;
}
return ERROR_SUCCESS == RegSetValueEx(m_hKey,szName, 0 ,REG_SZ,(LPBYTE)szValue, sizeof (TCHAR) * dwLen);
}
// -------------------------------------------------------------------
// Description:
// Setthestringvalue
//
// Parameters:
// szName:[in]Pointertoastringcontainingthenameofthevaluetoset
// szValue:[in]Thestringbufferincludenullcharactertobeset
// -------------------------------------------------------------------
BOOLCReg::SetSZ(LPCTSTRszName,LPCTSTRszValue)
{
return SetSZ(szName,szValue, 1 + _tcslen(szValue));
}
// -------------------------------------------------------------------
// Description:
// GettheDWORDvalue
//
// Parameters:
// szName:[in]Pointertoastringcontainingthenameofthevaluetoset
// dwValue:[in]Thevaluetobeset
// -------------------------------------------------------------------
BOOLCReg::SetDW(LPCTSTRszName,DWORDdwValue)
{
// Prefix
if ( ! m_hKey)
{
return FALSE;
}
return ERROR_SUCCESS == RegSetValueEx(m_hKey,szName, 0 ,REG_DWORD,(LPBYTE) & dwValue, sizeof (DWORD));
}
// -------------------------------------------------------------------
// Description:
// Getthebinaryvalue
//
// Parameters:
// szName:[in]Pointertoastringcontainingthenameofthevaluetoset
// lpbValue:[in]Thebufferincludethedatatobeset
// dwLen:[in]SpecifiesthelengthofthebufferpointedtobythelpbValueparameter.
// -------------------------------------------------------------------
BOOLCReg::SetBinary(LPCTSTRszName,LPBYTElpbValue,DWORDdwLen)
{
// Prefix
if ( ! m_hKey)
{
return FALSE;
}
return ERROR_SUCCESS == RegSetValueEx(m_hKey,szName, 0 ,REG_BINARY,lpbValue, sizeof (BYTE) * dwLen);
}
// -------------------------------------------------------------------
// Description:
// SettheMultivalue
//
// Parameters:
// szName:[in]Pointertoastringcontainingthenameofthevaluetoset
// lpszValue:[in]Thebufferincludethedatatobeset
// dwLen:[in]SpecifiesthelengthofthebufferpointedtobythelpszValueparameter.
// -------------------------------------------------------------------
BOOLCReg::SetMultiSZ(LPCTSTRszName,LPCTSTRlpszValue,DWORDdwLen)
{
return ERROR_SUCCESS == RegSetValueEx(m_hKey,szName, 0 ,REG_MULTI_SZ,(LPBYTE)lpszValue, sizeof (TCHAR) * dwLen);
}
// -------------------------------------------------------------------
// Description:
// Thisfunctionremovesanamedvaluefromthespecifiedregistrykey.
//
// Parameters:
// szName:[in]Pointertoanull-terminatedstringthatnamesthevaluetoremove.
// -------------------------------------------------------------------
BOOLCReg::DeleteValue(LPCTSTRszName)
{
// Prefix
if ( ! m_hKey)
{
return FALSE;
}
//
return ERROR_SUCCESS == RegDeleteValue(m_hKey,szName);
}
// -------------------------------------------------------------------
// Description:
// Thisfunctionrecursivelydeletesallsubkeysofanamedsubkeyofaspecifiedregistrykey.
//
// Parameters:
// szName:[in]Pointertoanull-terminatedstringspecifyingthenameofthekeytodelete.ThisparametercannotbeNULL.
// -------------------------------------------------------------------
BOOLCReg::DeleteKey(LPCTSTRszName)
{
if ( ! m_hKey)
{
return FALSE;
}
return ERROR_SUCCESS == RegDeleteKey(m_hKey,szName);
}
// -------------------------------------------------------------------
// Description:
// Getthevaluetype
//
// Parameters:
// szName:[in]Pointertoanull-terminatedstringthatnamesthevalue
//
// ------------------------------------------------------------------------
ValueTypeCReg::GetValueType(LPCTSTRszName)
{
if (m_hKey == NULL)
{
return VAL_ERROR;
}
DWORDdwType;
if (ERROR_SUCCESS != RegQueryValueEx(m_hKey,szName,NULL, & dwType, 0 , 0 ))
{
return VAL_ERROR;
}
switch (dwType)
{
case REG_BINARY:
return VAL_BINARY;
case REG_DWORD:
case REG_DWORD_BIG_ENDIAN:
return VAL_DWORD;
case REG_EXPAND_SZ:
case REG_SZ:
return VAL_SZ;
case REG_MULTI_SZ:
return VAL_MULTISZ;
default :
return VAL_ERROR; // Thereareothertypes,butnotsupportedbyCReg
}
}
// -------------------------------------------------------------------
// Description:
// Checkthekeyexist.
//
// Parameters:
// pszKey:[in]Pointertoanull-terminatedstringthatnameskey
//
// ------------------------------------------------------------------------
BOOLCReg::CheckKeyExist(HKEYhkRoot,LPCTSTRpszKey)
{
HKEYhKey = 0 ;
if (ERROR_SUCCESS == RegOpenKeyEx(hkRoot,pszKey, 0 , 0 , & hKey))
{
RegCloseKey(hKey);
return TRUE;
}
else
{
return FALSE;
}
}