📄 cnregistry.cpp
字号:
//---------------------------------------------------------------------------
#pragma hdrstop
#include "CNRegistry.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
bool IsRelative(const TCNString Value)
{
if( !Value.IsEmpty())
{
if( Value[0] == '\\')
{
return false;
}
}
return true;
// return !( (Value != "") && (Value[0]== '\\') );
}
DWORD RegDataToDataType(TCNRegDataType Value)
{
DWORD Result;
switch( Value)
{
case rdString: Result = REG_SZ;break;
case rdExpandString: Result = REG_EXPAND_SZ;break;
case rdInteger: Result = REG_DWORD;break;
case rdBinary: Result = REG_BINARY;break;
default: Result = REG_NONE;break;
}
return Result;
}
TCNRegDataType DataTypeToRegData(DWORD Value)
{
TCNRegDataType Result;
if (Value == REG_SZ) Result = rdString;
else if (Value == REG_EXPAND_SZ) Result = rdExpandString;
else if (Value == REG_DWORD) Result = rdInteger;
else if (Value == REG_BINARY) Result = rdBinary;
else Result = rdUnknown;
return Result;
}
void ReadError(const TCNString Name)
{
}
__fastcall TCNRegistry::TCNRegistry(void)
{
//TODO: Add your source code here
FRootKey = HKEY_CURRENT_USER;
FAccess = KEY_ALL_ACCESS;
FLazyWrite = true;
FCurrentKey = NULL;
FRootKey = NULL;
}
__fastcall TCNRegistry::~TCNRegistry(void)
{
//TODO: Add your source code here
CloseKey();
}
void __fastcall TCNRegistry::ChangeKey(HKEY Value, const TCNString Path)
{
CloseKey();
FCurrentKey = Value;
FCurrentPath = Path;
}
HKEY __fastcall TCNRegistry::GetBaseKey(bool Relative)
{
if( FCurrentKey == NULL || !Relative)
{
return FRootKey; //
}
else
{
return FCurrentKey;
}
}
DWORD __fastcall TCNRegistry::GetData(const TCNString Name, void * Buffer, DWORD BufSize, TCNRegDataType &RegData)
{
DWORD DataType;
DataType = REG_NONE;
DWORD Result;
if (RegQueryValueEx(FCurrentKey,
Name.c_str(),
NULL,
&DataType,
(char *)Buffer,
(u_long*)&BufSize) != ERROR_SUCCESS)
{
//error
}
else
{
Result = BufSize;
RegData = DataTypeToRegData(DataType);
}
return Result;
}
HKEY __fastcall TCNRegistry::GetKey(const TCNString Key)
{
TCNString S;
bool Relative;
HKEY Result = NULL;
S = Key;
Relative = IsRelative(S);
if( !Relative ) S.Delete(0,1);
RegOpenKeyEx(GetBaseKey(Relative), S.c_str(), 0, FAccess, &Result);
return Result;
}
void __fastcall TCNRegistry::PutData(const TCNString Name, void * Buffer, DWORD BufSize, TCNRegDataType RegData)
{
DWORD DataType;
DataType = RegDataToDataType(RegData);
if (RegSetValueEx(FCurrentKey,
Name.c_str(), 0,
DataType,
(char *)Buffer,
BufSize) != ERROR_SUCCESS )
{
//error
}
}
void __fastcall TCNRegistry::SetCurrentKey(HKEY Value)
{
FCurrentKey = Value;
}
void __fastcall TCNRegistry::SetRootKey(HKEY Value)
{
if( FRootKey != Value)
{
if( FCloseRootKey)
{
RegCloseKey(FRootKey);
FCloseRootKey = false;
}
FRootKey = Value;
CloseKey();
}
}
void __fastcall TCNRegistry::CloseKey(void)
{
if( FCurrentKey != NULL)
{
if( FLazyWrite)
{
RegCloseKey(FCurrentKey);
RegFlushKey(FCurrentKey);
FCurrentKey = NULL;
FCurrentPath = "";
}
}
}
bool __fastcall TCNRegistry::CreateKey(const TCNString Key)
{
HKEY TempKey = NULL;
TCNString S;
u_long Disposition;
bool Relative;
bool Success;
S = Key;
Relative = IsRelative(S);
if( !Relative ) S.Delete(0,1);
Success = (RegCreateKeyEx(GetBaseKey(Relative),
S.c_str(),
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&TempKey,
&Disposition)) == ERROR_SUCCESS;
if( Success )
{
RegCloseKey(TempKey);
}
else
{
}
return Success;
}
bool __fastcall TCNRegistry::DeleteKey(const TCNString Key)
{
DWORD Len;
DWORD I;
bool Relative;
TCNString S,KeyName;
HKEY OldKey,DeleteKey;
TCNRegKeyInfo Info;
S = Key;
Relative = IsRelative(S);
if( !Relative) S.Delete(0,1);
OldKey = FCurrentKey;
DeleteKey = GetKey(Key);
if( DeleteKey != NULL)
{
__try{
SetCurrentKey(DeleteKey);
if (GetKeyInfo(Info))
{
KeyName.SetLength(Info.MaxSubKeyLen + 1);
for(I = 0; I < Info.NumSubKeys; I ++)
{
Len = Info.MaxSubKeyLen + 1;
if( RegEnumKeyEx(DeleteKey,
I,
KeyName.c_str(),
&Len,
NULL,NULL,NULL,NULL) == ERROR_SUCCESS)
{
this->DeleteKey(KeyName);
}
}
}
}
__finally{
SetCurrentKey(OldKey);
RegCloseKey(DeleteKey);
}
}
return (RegDeleteKey(GetBaseKey(Relative), S.c_str()) == ERROR_SUCCESS);
}
bool __fastcall TCNRegistry::DeleteValue(const TCNString Name)
{
return (RegDeleteValue(FCurrentKey, Name.c_str()) == ERROR_SUCCESS);
}
bool __fastcall TCNRegistry::GetDataInfo(const TCNString ValueName, TCNRegDataInfo &Value)
{
DWORD DataType;
bool Success;
memset((char *)&Value,0,sizeof(TCNRegDataInfo));
Success = RegQueryValueEx(FCurrentKey,
ValueName.c_str(),
NULL,
&DataType, NULL,
&Value.DataSize) == ERROR_SUCCESS;
Value.RegData = DataTypeToRegData(DataType);
return Success;
}
DWORD __fastcall TCNRegistry::GetDataSize(const TCNString ValueName)
{
TCNRegDataInfo Info;
DWORD Result;
if (GetDataInfo(ValueName, Info))
Result = Info.DataSize;
else
Result = -1;
return Result;
}
TCNRegDataType __fastcall TCNRegistry::GetDataType(const TCNString ValueName)
{
TCNRegDataInfo Info;
TCNRegDataType Result;
if (GetDataInfo(ValueName, Info))
Result = Info.RegData;
else
Result = rdUnknown;
return Result;
}
bool __fastcall TCNRegistry::GetKeyInfo(TCNRegKeyInfo &Value)
{
bool Success;
memset((char *)&Value,0,sizeof(TCNRegKeyInfo));
Success = RegQueryInfoKey(FCurrentKey,
NULL,
NULL,
NULL,
&Value.NumSubKeys,
&Value.MaxSubKeyLen,
NULL,
&Value.NumValues,
&Value.MaxValueLen,
&Value.MaxDataLen,
NULL,
&Value.FileTime) == ERROR_SUCCESS;
return Success;
}
void __fastcall TCNRegistry::GetKeyNames(TCNStringList* Strings)
{
DWORD Len;
DWORD I;
TCNRegKeyInfo Info;
TCNString S;
Strings->Clear();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -