📄 cnregistry.cpp
字号:
if (GetKeyInfo(Info))
{
S.SetLength(Info.MaxSubKeyLen + 1);
for(I = 0; I < Info.NumSubKeys; I++)
{
Len = Info.MaxSubKeyLen + 1;
RegEnumKeyEx(FCurrentKey,
I,
S.c_str(),
&Len,
NULL, NULL, NULL, NULL);
Strings->Add(S);
}
}
}
void __fastcall TCNRegistry::GetValueNames(TCNStringList* Strings)
{
DWORD Len;
DWORD I;
TCNRegKeyInfo Info;
TCNString S;
Strings->Clear();
if (GetKeyInfo(Info))
{
S.SetLength(Info.MaxValueLen + 1);
for(I = 0; I < Info.NumValues; I++)
{
Len = Info.MaxValueLen + 1;
RegEnumValue(FCurrentKey,
I,
S.c_str(),
&Len,
NULL, NULL, NULL, NULL);
Strings->Add(S);
}
}
}
bool __fastcall TCNRegistry::HasSubKeys(void)
{
TCNRegKeyInfo Info;
return (GetKeyInfo(Info) && (Info.NumSubKeys > 0));
}
bool __fastcall TCNRegistry::KeyExists(const TCNString Key)
{
TCNRegKeyInfo Info;
return GetKeyInfo(Info);
}
bool __fastcall TCNRegistry::LoadKey(const TCNString Key, const TCNString FileName)
{
TCNString S = Key;
if( IsRelative(S) ) S.Delete(0,1);
return (RegLoadKey(FRootKey, S.c_str(), FileName.c_str()) == ERROR_SUCCESS);
}
void __fastcall TCNRegistry::MoveKey(const TCNString OldName, const TCNString NewName, bool Delete)
{
}
bool __fastcall TCNRegistry::OpenKey(const TCNString Key, bool CanCreate)
{
HKEY TempKey = NULL;
TCNString S;
u_long Disposition;
bool Relative;
bool Success;
S = Key;
Relative = IsRelative(S); //是否是相对路径
if( !Relative ) S.Delete(0,1);//是绝对路径则去掉'\'
if( !CanCreate || S == "")
{
Success = RegOpenKeyEx( GetBaseKey(Relative),
S.c_str(), 0,
FAccess,
&TempKey) == ERROR_SUCCESS;
}
else
{
Success = RegCreateKeyEx(GetBaseKey(Relative),
S.c_str(), 0,
NULL,
REG_OPTION_NON_VOLATILE,
FAccess,
NULL,
&TempKey,
&Disposition) == ERROR_SUCCESS;
}
if( Success)
{
if( FCurrentKey != NULL && Relative)
S = FCurrentPath + "\\" + S;
ChangeKey(TempKey, S);
}
return Success;
}
bool __fastcall TCNRegistry::OpenKeyReadOnly(const TCNString Key)
{
HKEY TempKey = NULL;
TCNString S;
bool Relative;
bool Success;
S = Key;
Relative = IsRelative(S);
if (!Relative) S.Delete(0, 1);
Success = RegOpenKeyEx( GetBaseKey(Relative),
S.c_str(), 0,
KEY_READ,
&TempKey) == ERROR_SUCCESS;
if( Success)
{
FAccess = KEY_READ;
if( FCurrentKey != NULL && Relative ) S = FCurrentPath + "\\" + S;
ChangeKey(TempKey,S);
}
else
{
Success = RegOpenKeyEx( GetBaseKey(Relative),
S.c_str(), 0,
STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS,
&TempKey) == ERROR_SUCCESS;
if( Success)
{
FAccess = STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS;
if( FCurrentKey != NULL && Relative ) S = FCurrentPath + "\\" + S;
ChangeKey(TempKey,S);
}
else
{
Success = RegOpenKeyEx( GetBaseKey(Relative),
S.c_str(), 0,
KEY_QUERY_VALUE,
&TempKey) == ERROR_SUCCESS;
if( Success)
{
FAccess = KEY_QUERY_VALUE;
if( FCurrentKey != NULL && Relative ) S = FCurrentPath + "\\" + S;
ChangeKey(TempKey,S);
}
}
}
return Success;
}
DWORD __fastcall TCNRegistry::ReadBinaryData(const TCNString Name, void *Buffer, DWORD BufSize)
{
TCNRegDataType RegData;
TCNRegDataInfo Info;
DWORD Result;
if( GetDataInfo(Name,Info))
{
Result = Info.DataSize;
RegData = Info.RegData;
if (((RegData == rdBinary) || (RegData == rdUnknown)) && (Result <= BufSize))
{
GetData(Name, Buffer, Result, RegData);
}
else
{
ReadError(Name);
}
}
else
Result = 0;
return Result;
}
bool __fastcall TCNRegistry::ReadBool(const TCNString Name)
{
return (ReadInteger(Name) != 0);
}
double __fastcall TCNRegistry::ReadFloat(const TCNString Name)
{
DWORD Len;
TCNRegDataType RegData;
double Result;
Len = GetData(Name, &Result, sizeof(double), RegData);
if ((RegData != rdBinary) || (Len != sizeof(double)))
{
ReadError(Name);
}
return Result;
}
DWORD __fastcall TCNRegistry::ReadInteger(const TCNString Name)
{
DWORD Result;
TCNRegDataType RegData;
GetData(Name, &Result, sizeof(DWORD), RegData);
if (RegData != rdInteger)
{
ReadError(Name);
}
return Result;
}
TCNString __fastcall TCNRegistry::ReadString(const TCNString Name)
{
DWORD Len;
TCNRegDataType RegData;
TCNString Result;
Len = GetDataSize(Name);
if( Len > 0)
{
Result.SetLength( Len );
GetData(Name, Result.c_str(), Len, RegData);
if ((RegData == rdString) || (RegData == rdExpandString))
{
Result.SetLength( Result.Length());
}
else ReadError(Name);
}
else Result = "";
return Result;
}
bool __fastcall TCNRegistry::RegistryConnect(const TCNString UNCName)
{
HKEY TempKey;
bool Success;
Success = (RegConnectRegistry(UNCName.c_str(), FRootKey, &TempKey) == ERROR_SUCCESS);
if( Success )
{
FRootKey = TempKey;
FCloseRootKey = true;
}
return Success;
}
void __fastcall TCNRegistry::RenameValue(const TCNString OldName, const TCNString NewName)
{
DWORD Len;
TCNRegDataType RegData;
char *Buffer;
if (ValueExists(OldName) && !ValueExists(NewName))
{
Len = GetDataSize(OldName);
if( Len > 0)
{
Buffer = new char[Len];
__try{
Len = GetData(OldName,Buffer,Len,RegData);
DeleteValue(OldName);
PutData(NewName, Buffer, Len, RegData);
}
__finally{
delete Buffer;
}
}
}
}
bool __fastcall TCNRegistry::ReplaceKey(const TCNString Key, const TCNString FileName, const TCNString BackUpFileName)
{
TCNString S;
bool Relative;
S = Key;
Relative = IsRelative(S);
if( !Relative ) S.Delete(0,1);
return (RegReplaceKey(GetBaseKey(Relative),
S.c_str(),
FileName.c_str(),
BackUpFileName.c_str()) == ERROR_SUCCESS);
}
bool __fastcall TCNRegistry::ValueExists(const TCNString Name)
{
TCNRegDataInfo Info;
return GetDataInfo(Name, Info);
}
void __fastcall TCNRegistry::WriteBinaryData(const TCNString Name, void *Buffer, DWORD BufSize)
{
PutData(Name, (char *)Buffer, BufSize, rdBinary);
}
void __fastcall TCNRegistry::WriteBool(const TCNString Name, bool Value)
{
WriteInteger(Name, (DWORD)Value);
}
void __fastcall TCNRegistry::WriteFloat(const TCNString Name, double Value)
{
PutData(Name, &Value, sizeof(double), rdBinary);
}
void __fastcall TCNRegistry::WriteInteger(const TCNString Name, DWORD Value)
{
PutData(Name, &Value, sizeof(DWORD), rdInteger);
}
void __fastcall TCNRegistry::WriteString(const TCNString Name, const TCNString Value)
{
PutData(Name, Value.c_str(), Value.Length()+1, rdString);
}
void __fastcall TCNRegistry::WriteExpandString(const TCNString Name, const TCNString Value)
{
PutData(Name, Value.c_str(), Value.Length()+1, rdExpandString);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -