//========================================================================
//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
#defineREG_H
//---------------------------------------------------------------------
enumValueType
{
VAL_ERROR,
VAL_DWORD,
VAL_MULTISZ,
VAL_SZ,
VAL_BINARY
};
//---------------------------------------------------------------------
classCReg
{
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();
operatorHKEY();
voidReset();
CReg(HKEYhkRoot,LPCTSTRpszKey);
BOOLOpen(HKEYhkRoot,LPCTSTRpszKey,REGSAMsam=KEY_READ);
BOOLCreate(HKEYhkRoot,LPCTSTRpszKey);
CReg();
virtual~CReg();
private:
HKEYm_hKey;
intm_Index;
LPBYTEm_lpbValue;//lastvalueread,ifany
};
#endif//#ifndefREG_H
//////////////////////////////////////////////////////////////////////
//Reg.cpp:implementationoftheCRegclass.
//
//////////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include"Reg.h"
//=======================================================================
//Macrodefine
#defineMyFree(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;
returnERROR_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)
{
returnERROR_SUCCESS==RegOpenKeyEx(hkRoot,pszKey,0,sam,&m_hKey);
}
//-------------------------------------------------------------------
//Description:
//Resetthevalueandreleasetheresource.
//-------------------------------------------------------------------
voidCReg::Reset()
{
if(m_hKey)
{
RegCloseKey(m_hKey);
}
MyFree(m_lpbValue);
m_hKey=NULL;
m_Index=0;
m_lpbValue=NULL;
}
//-------------------------------------------------------------------
//Description:
//Operatoroverload
//-------------------------------------------------------------------
CReg::operatorHKEY()
{
returnm_hKey;
}
//-------------------------------------------------------------------
//Description:
//Checkwhetherisreadyfornextoperate
//
//Returnvalue:
//TRUE:Readytodonextoperate
//FALSE:Notready.
//-------------------------------------------------------------------
BOOLCReg::IsOK()
{
returnm_hKey!=NULL;
}
//-------------------------------------------------------------------
//Description:
//Thisfunctionenumeratessubkeysofthespecifiedopenregistrykey.
//
//Parameters:
//psz:[out]Pointertoabufferthatreceivesthenameofthesubkey,includingtheterminatingnullcharacter.
//Thefunctioncopiesonlythenameofthesubkey,notthefullkeyhierarchy,tothebuffer.
//dwLen:[in]Specifiesthelengthofthebufferpointedtobythepszparameter.
//-------------------------------------------------------------------
BOOLCReg::EnumKey(LPTSTRpsz,DWORDdwLen)
{
if(!m_hKey)
{
returnFALSE;
}
dwLen*=sizeof(TCHAR);
returnERROR_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)
{
returnFALSE;
}
dwLenValue*=sizeof(BYTE);//convertlengthinnumbertobytes
returnERROR_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)
{
returnFALSE;
}
dwLen*=sizeof(TCHAR);//convertlengthincharstobytes
DWORDdwType;
if(ERROR_SUCCESS==RegQueryValueEx(m_hKey,szName,NULL,&dwType,(LPBYTE)szValue,&dwLen))
{
if(REG_SZ!=dwType)
{
dwLen=0;
returnFALSE;
}
else
{
dwLen/=sizeof(TCHAR);
}
}
else
{
dwLen=0;
}
returndwLen;
}
//-------------------------------------------------------------------
//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)
{
returnFALSE;
}
DWORDdwType;
if(ERROR_SUCCESS==RegQueryValueEx(m_hKey,szName,NULL,&dwType,lpbValue,&dwLen))
{
if(dwType!=REG_BINARY)
{
dwLen=0;
}
}
else
{
dwLen=0;
}
returndwLen;
}
//-------------------------------------------------------------------
//Description:
//GettheDWORDvalue
//
//Parameters:
//szName:[in]Thevalueofregistry
//dwDefault:[in]ThedefaultvaluereturnwhenfailedingettingtheDWORDvalue.
//
//Returnvalue:
//IfthereturnvaluejustbesameasthedwDefault,itmaymeanfailed.
//-------------------------------------------------------------------
DWORDCReg::GetValueDW(LPCTSTRszName,DWORDdwDefault)
{
if(!m_hKey)
{
returnFALSE;
}
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;
}
returndwValue;
}
//-------------------------------------------------------------------
//Description:
//Setthestringvalue
//
//Parameters:
//szName:[in]Pointertoastringcontainingthenameofthevaluetoset
//szValue:[in]Thebufferincludethedatatobeset
//dwLen:[in]SpecifiesthelengthofthebufferpointedtobytheszValueparameter.
//-------------------------------------------------------------------
BOOLCReg::SetSZ(LPCTSTRszName,LPCTSTRszValue,DWORDdwLen)
{
//Prefix
if(!m_hKey)
{
returnFALSE;
}
returnERROR_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)
{
returnSetSZ(szName,szValue,1+_tcslen(szValue));
}
//-------------------------------------------------------------------
//Description:
//GettheDWORDvalue
//
//Parameters:
//szName:[in]Pointertoastringcontainingthenameofthevaluetoset
//dwValue:[in]Thevaluetobeset
//-------------------------------------------------------------------
BOOLCReg::SetDW(LPCTSTRszName,DWORDdwValue)
{
//Prefix
if(!m_hKey)
{
returnFALSE;
}
returnERROR_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)
{
returnFALSE;
}
returnERROR_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)
{
returnERROR_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)
{
returnFALSE;
}
//
returnERROR_SUCCESS==RegDeleteValue(m_hKey,szName);
}
//-------------------------------------------------------------------
//Description:
//Thisfunctionrecursivelydeletesallsubkeysofanamedsubkeyofaspecifiedregistrykey.
//
//Parameters:
//szName:[in]Pointertoanull-terminatedstringspecifyingthenameofthekeytodelete.ThisparametercannotbeNULL.
//-------------------------------------------------------------------
BOOLCReg::DeleteKey(LPCTSTRszName)
{
if(!m_hKey)
{
returnFALSE;
}
returnERROR_SUCCESS==RegDeleteKey(m_hKey,szName);
}
//-------------------------------------------------------------------
//Description:
//Getthevaluetype
//
//Parameters:
//szName:[in]Pointertoanull-terminatedstringthatnamesthevalue
//
//------------------------------------------------------------------------
ValueTypeCReg::GetValueType(LPCTSTRszName)
{
if(m_hKey==NULL)
{
returnVAL_ERROR;
}
DWORDdwType;
if(ERROR_SUCCESS!=RegQueryValueEx(m_hKey,szName,NULL,&dwType,0,0))
{
returnVAL_ERROR;
}
switch(dwType)
{
caseREG_BINARY:
returnVAL_BINARY;
caseREG_DWORD:
caseREG_DWORD_BIG_ENDIAN:
returnVAL_DWORD;
caseREG_EXPAND_SZ:
caseREG_SZ:
returnVAL_SZ;
caseREG_MULTI_SZ:
returnVAL_MULTISZ;
default:
returnVAL_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);
returnTRUE;
}
else
{
returnFALSE;
}
}
//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
#defineREG_H
//---------------------------------------------------------------------
enumValueType
{
VAL_ERROR,
VAL_DWORD,
VAL_MULTISZ,
VAL_SZ,
VAL_BINARY
};
//---------------------------------------------------------------------
classCReg
{
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();
operatorHKEY();
voidReset();
CReg(HKEYhkRoot,LPCTSTRpszKey);
BOOLOpen(HKEYhkRoot,LPCTSTRpszKey,REGSAMsam=KEY_READ);
BOOLCreate(HKEYhkRoot,LPCTSTRpszKey);
CReg();
virtual~CReg();
private:
HKEYm_hKey;
intm_Index;
LPBYTEm_lpbValue;//lastvalueread,ifany
};
#endif//#ifndefREG_H
//////////////////////////////////////////////////////////////////////
//Reg.cpp:implementationoftheCRegclass.
//
//////////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include"Reg.h"
//=======================================================================
//Macrodefine
#defineMyFree(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;
returnERROR_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)
{
returnERROR_SUCCESS==RegOpenKeyEx(hkRoot,pszKey,0,sam,&m_hKey);
}
//-------------------------------------------------------------------
//Description:
//Resetthevalueandreleasetheresource.
//-------------------------------------------------------------------
voidCReg::Reset()
{
if(m_hKey)
{
RegCloseKey(m_hKey);
}
MyFree(m_lpbValue);
m_hKey=NULL;
m_Index=0;
m_lpbValue=NULL;
}
//-------------------------------------------------------------------
//Description:
//Operatoroverload
//-------------------------------------------------------------------
CReg::operatorHKEY()
{
returnm_hKey;
}
//-------------------------------------------------------------------
//Description:
//Checkwhetherisreadyfornextoperate
//
//Returnvalue:
//TRUE:Readytodonextoperate
//FALSE:Notready.
//-------------------------------------------------------------------
BOOLCReg::IsOK()
{
returnm_hKey!=NULL;
}
//-------------------------------------------------------------------
//Description:
//Thisfunctionenumeratessubkeysofthespecifiedopenregistrykey.
//
//Parameters:
//psz:[out]Pointertoabufferthatreceivesthenameofthesubkey,includingtheterminatingnullcharacter.
//Thefunctioncopiesonlythenameofthesubkey,notthefullkeyhierarchy,tothebuffer.
//dwLen:[in]Specifiesthelengthofthebufferpointedtobythepszparameter.
//-------------------------------------------------------------------
BOOLCReg::EnumKey(LPTSTRpsz,DWORDdwLen)
{
if(!m_hKey)
{
returnFALSE;
}
dwLen*=sizeof(TCHAR);
returnERROR_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)
{
returnFALSE;
}
dwLenValue*=sizeof(BYTE);//convertlengthinnumbertobytes
returnERROR_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)
{
returnFALSE;
}
dwLen*=sizeof(TCHAR);//convertlengthincharstobytes
DWORDdwType;
if(ERROR_SUCCESS==RegQueryValueEx(m_hKey,szName,NULL,&dwType,(LPBYTE)szValue,&dwLen))
{
if(REG_SZ!=dwType)
{
dwLen=0;
returnFALSE;
}
else
{
dwLen/=sizeof(TCHAR);
}
}
else
{
dwLen=0;
}
returndwLen;
}
//-------------------------------------------------------------------
//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)
{
returnFALSE;
}
DWORDdwType;
if(ERROR_SUCCESS==RegQueryValueEx(m_hKey,szName,NULL,&dwType,lpbValue,&dwLen))
{
if(dwType!=REG_BINARY)
{
dwLen=0;
}
}
else
{
dwLen=0;
}
returndwLen;
}
//-------------------------------------------------------------------
//Description:
//GettheDWORDvalue
//
//Parameters:
//szName:[in]Thevalueofregistry
//dwDefault:[in]ThedefaultvaluereturnwhenfailedingettingtheDWORDvalue.
//
//Returnvalue:
//IfthereturnvaluejustbesameasthedwDefault,itmaymeanfailed.
//-------------------------------------------------------------------
DWORDCReg::GetValueDW(LPCTSTRszName,DWORDdwDefault)
{
if(!m_hKey)
{
returnFALSE;
}
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;
}
returndwValue;
}
//-------------------------------------------------------------------
//Description:
//Setthestringvalue
//
//Parameters:
//szName:[in]Pointertoastringcontainingthenameofthevaluetoset
//szValue:[in]Thebufferincludethedatatobeset
//dwLen:[in]SpecifiesthelengthofthebufferpointedtobytheszValueparameter.
//-------------------------------------------------------------------
BOOLCReg::SetSZ(LPCTSTRszName,LPCTSTRszValue,DWORDdwLen)
{
//Prefix
if(!m_hKey)
{
returnFALSE;
}
returnERROR_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)
{
returnSetSZ(szName,szValue,1+_tcslen(szValue));
}
//-------------------------------------------------------------------
//Description:
//GettheDWORDvalue
//
//Parameters:
//szName:[in]Pointertoastringcontainingthenameofthevaluetoset
//dwValue:[in]Thevaluetobeset
//-------------------------------------------------------------------
BOOLCReg::SetDW(LPCTSTRszName,DWORDdwValue)
{
//Prefix
if(!m_hKey)
{
returnFALSE;
}
returnERROR_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)
{
returnFALSE;
}
returnERROR_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)
{
returnERROR_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)
{
returnFALSE;
}
//
returnERROR_SUCCESS==RegDeleteValue(m_hKey,szName);
}
//-------------------------------------------------------------------
//Description:
//Thisfunctionrecursivelydeletesallsubkeysofanamedsubkeyofaspecifiedregistrykey.
//
//Parameters:
//szName:[in]Pointertoanull-terminatedstringspecifyingthenameofthekeytodelete.ThisparametercannotbeNULL.
//-------------------------------------------------------------------
BOOLCReg::DeleteKey(LPCTSTRszName)
{
if(!m_hKey)
{
returnFALSE;
}
returnERROR_SUCCESS==RegDeleteKey(m_hKey,szName);
}
//-------------------------------------------------------------------
//Description:
//Getthevaluetype
//
//Parameters:
//szName:[in]Pointertoanull-terminatedstringthatnamesthevalue
//
//------------------------------------------------------------------------
ValueTypeCReg::GetValueType(LPCTSTRszName)
{
if(m_hKey==NULL)
{
returnVAL_ERROR;
}
DWORDdwType;
if(ERROR_SUCCESS!=RegQueryValueEx(m_hKey,szName,NULL,&dwType,0,0))
{
returnVAL_ERROR;
}
switch(dwType)
{
caseREG_BINARY:
returnVAL_BINARY;
caseREG_DWORD:
caseREG_DWORD_BIG_ENDIAN:
returnVAL_DWORD;
caseREG_EXPAND_SZ:
caseREG_SZ:
returnVAL_SZ;
caseREG_MULTI_SZ:
returnVAL_MULTISZ;
default:
returnVAL_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);
returnTRUE;
}
else
{
returnFALSE;
}
}
8911

被折叠的 条评论
为什么被折叠?



