📄 registrykey.cpp
字号:
0,
&dwType,
pBuffer,
&bufSize);
if (ERROR_SUCCESS == result)
{
ok = true;
*ppBytes = pBuffer;
}
else
{
free(pBuffer); //crap
throw Exception(_T("CRegistryKey::QueryValue()"), result);
}
}
return ok;
}
bool CRegistryKey::QueryValue(LPCTSTR pValueName, LPCTSTR *ppString) const
{
DWORD dwType;
LPBYTE pBuffer = 0;
DWORD bufSize = 0;
bool ok = false;
LONG result = RegQueryValueEx(
m_pKey->GetCounted(),
pValueName,
0,
&dwType,
pBuffer,
&bufSize);
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::QueryValue()"), result);
}
if (dwType == REG_EXPAND_SZ || dwType == REG_SZ)
{
// bufSize should now tell us how much space we need...
pBuffer = (LPBYTE)malloc(bufSize);
result = RegQueryValueEx(
m_pKey->GetCounted(),
pValueName,
0,
&dwType,
pBuffer,
&bufSize);
if (ERROR_SUCCESS == result)
{
ok = true;
*ppString = (LPCTSTR)pBuffer;
}
else
{
free(pBuffer); //crap
throw Exception(_T("CRegistryKey::QueryValue()"), result);
}
}
return ok;
}
bool CRegistryKey::QueryValue(
LPCTSTR pValueName,
DWORD &dwValue) const
{
DWORD dwType;
LPBYTE pBuffer = 0;
DWORD bufSize = 0;
bool ok = false;
LONG result = RegQueryValueEx(
m_pKey->GetCounted(),
pValueName,
0,
&dwType,
pBuffer,
&bufSize);
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::QueryValue()"), result);
}
if (dwType == REG_DWORD ||
dwType == REG_DWORD_LITTLE_ENDIAN ||
dwType == REG_DWORD_BIG_ENDIAN)
{
// bufSize should be sizeof(DWORD)
if (bufSize != sizeof(DWORD))
{
throw Exception(_T("CRegistryKey::QueryValue()"), result);
}
result = RegQueryValueEx(
m_pKey->GetCounted(),
pValueName,
0,
&dwType,
(LPBYTE)&dwValue,
&bufSize);
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::QueryValue()"), result);
}
}
return ok;
}
// QueryValue for other types
// What about multiple values?
CRegistryKey::Value CRegistryKey::QueryValue(LPCTSTR pValueName /* = 0 */) const
{
DWORD dwType;
LPBYTE pBuffer = 0;
DWORD bufSize = 0;
LONG result = RegQueryValueEx(
m_pKey->GetCounted(),
pValueName,
0,
&dwType,
pBuffer,
&bufSize);
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::QueryValue()"), result);
}
// bufSize should now tell us how much space we need...
pBuffer = new unsigned char[bufSize];
result = RegQueryValueEx(
m_pKey->GetCounted(),
pValueName,
0,
&dwType,
pBuffer,
&bufSize);
if (ERROR_SUCCESS != result)
{
delete[] pBuffer; //crap
throw Exception(_T("CRegistryKey::QueryValue()"), result);
}
return Value(pValueName, pBuffer, bufSize, dwType);
}
void CRegistryKey::SetValue(
LPCTSTR pValueName,
LPBYTE pBytes,
DWORD cbBytes) const
{
LONG result = RegSetValueEx(
m_pKey->GetCounted(),
pValueName,
0,
REG_BINARY,
pBytes,
cbBytes);
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::SetValue()"), result);
}
}
void CRegistryKey::SetValue(
LPCTSTR pValueName,
LPCTSTR pValue,
DWORD dwType /*= REG_SZ*/) const
{
if (dwType != REG_SZ &&
//dwType != REG_LINK &&
dwType != REG_EXPAND_SZ)
// Handle MULTI_SZ and do the strlen ourselves?
{
throw Exception(_T("CRegistryKey::SetValue()"), ERROR_INVALID_FLAGS);
}
LONG result = RegSetValueEx(
m_pKey->GetCounted(),
pValueName,
0,
dwType,
(LPBYTE)pValue,
_tcslen(pValue) + sizeof(TCHAR)); // length of string plus null
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::SetValue()"), result);
}
}
void CRegistryKey::SetValue(
LPCTSTR pValueName,
DWORD value,
DWORD dwType /*= REG_DWORD*/) const
{
if (dwType != REG_DWORD &&
dwType != REG_DWORD_LITTLE_ENDIAN &&
dwType != REG_DWORD_BIG_ENDIAN)
{
throw Exception(_T("CRegistryKey::SetValue()"), ERROR_INVALID_FLAGS);
}
LONG result = RegSetValueEx(
m_pKey->GetCounted(),
pValueName,
0,
dwType,
(LPBYTE)&value,
sizeof(DWORD));
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::SetValue()"), result);
}
}
void CRegistryKey::SetValue(
const Value &value) const
{
// Should the value store the name too?
LONG result = RegSetValueEx(
m_pKey->GetCounted(),
value.m_pName,
0,
value.m_dwType,
value.m_pBuffer,
value.m_bufSize);
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::SetValue()"), result);
}
}
void CRegistryKey::LoadKey(LPCTSTR pSubkeyName, LPCTSTR pFile) const
{
LONG result = RegLoadKey(m_pKey->GetCounted(), pSubkeyName, pFile);
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::LoadKey()"), result);
}
}
void CRegistryKey::UnLoadKey(LPCTSTR pSubkeyName) const
{
LONG result = RegUnLoadKey(m_pKey->GetCounted(), pSubkeyName);
if (ERROR_SUCCESS == result)
{
throw Exception(_T("CRegistryKey::UnLoadKey()"), result);
}
}
void CRegistryKey::SaveKey(
LPCTSTR pFile,
LPSECURITY_ATTRIBUTES pSecurityAttributes /* = NULL */) const
{
LONG result = RegSaveKey(m_pKey->GetCounted(), pFile, pSecurityAttributes);
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::SaveKey()"), result);
}
}
void CRegistryKey::RestoreKey(LPCTSTR pFile, DWORD flags /* = 0 */) const
{
LONG result = RegRestoreKey(m_pKey->GetCounted(), pFile, flags);
if (ERROR_SUCCESS == result)
{
throw Exception(_T("CRegistryKey::RestoreKey()"), result);
}
}
void CRegistryKey::ReplaceKey(
LPCTSTR pNewFile,
LPCTSTR pOldFile,
LPCTSTR pSubkeyName /* = 0 */) const
{
LONG result = RegReplaceKey(m_pKey->GetCounted(), pSubkeyName, pNewFile, pOldFile);
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::ReplaceKey()"), result);
}
}
PSECURITY_DESCRIPTOR CRegistryKey::GetKeySecurity(
SECURITY_INFORMATION securityInformation) const
{
TExpandableBuffer<unsigned char> descriptor;
DWORD dwSize = 0;
LONG result = RegGetKeySecurity(m_pKey->GetCounted(),
securityInformation,
0,
&dwSize);
if (ERROR_INSUFFICIENT_BUFFER == result)
{
descriptor.Resize(dwSize);
result = RegGetKeySecurity(m_pKey->GetCounted(),
securityInformation,
(PSECURITY_DESCRIPTOR)descriptor.GetBuffer(),
&dwSize);
}
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::GetKeySecurity()"), result);
}
return descriptor.ReleaseBuffer();
}
void CRegistryKey::SetKeySecurity(
SECURITY_INFORMATION securityInformation,
PSECURITY_DESCRIPTOR pSecurityDescriptor) const
{
LONG result = RegSetKeySecurity(m_pKey->GetCounted(),
securityInformation,
pSecurityDescriptor);
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::SetKeySecurity()"), result);
}
}
void CRegistryKey::NotifyChangeKeyValue(
HANDLE hEvent,
bool bSubKeys /* = false */,
DWORD dwNotifyFilter /* = REG_NOTIFY_CHANGE_LAST_SET */) const
{
LONG result = RegNotifyChangeKeyValue(m_pKey->GetCounted(),
bSubKeys,
dwNotifyFilter,
hEvent,
true);
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::RegNotifyChangeKeyValue()"), result);
}
}
void CRegistryKey::NotifyChangeKeyValue(
bool bSubKeys /* = false */,
DWORD dwNotifyFilter /* = REG_NOTIFY_CHANGE_LAST_SET */) const
{
LONG result = RegNotifyChangeKeyValue(m_pKey->GetCounted(),
bSubKeys,
dwNotifyFilter,
0,
false);
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::RegNotifyChangeKeyValue()"), result);
}
}
///////////////////////////////////////////////////////////////////////////////
// CRegistryKey::SubkeyIterator
///////////////////////////////////////////////////////////////////////////////
CRegistryKey::SubkeyIteratorImpl::SubkeyIteratorImpl(CCountedRegKey *pKey)
: CRegKeyIterator(pKey),
m_Name(0),
m_Class(0)
{
if (pKey)
{
DWORD dwNumSubKeys = 0;
DWORD dwMaxNameLen = 0;
DWORD dwMaxClassLen = 0;
LONG result = RegQueryInfoKey(
pKey->GetCounted(),
NULL, // Not interested in this key's class
NULL, // ditto
NULL, // Reserved
&dwNumSubKeys,
&dwMaxNameLen,
&dwMaxClassLen,
NULL, // Not interested in number of values
NULL, // Not interested in max length of value name
NULL, // Not interested in max length of value buffer
NULL, // Not interested in length of security descriptor
NULL); // Not interested in last write time
if (ERROR_SUCCESS != result)
{
throw Exception(_T("CRegistryKey::SubkeyIterator::SubkeyIterator()"),
result);
}
if (0 != dwNumSubKeys)
{
// Allow for NULL character...
m_Name.Resize(dwMaxNameLen + 1);
m_Class.Resize(dwMaxClassLen + 1);
}
}
}
bool CRegistryKey::SubkeyIteratorImpl::operator==(const SubkeyIteratorImpl &rhs) const
{
return CRegKeyIterator::operator==(rhs);
}
LPCTSTR CRegistryKey::SubkeyIteratorImpl::GetName() const
{
return m_Name;
}
LPCTSTR CRegistryKey::SubkeyIteratorImpl::GetClass() const
{
return m_Class;
}
CRegistryKey CRegistryKey::SubkeyIteratorImpl::OpenKey(
REGSAM samDesired /*= KEY_ALL_ACCESS*/) const
{
return CRegistryKey(m_pKey->GetCounted(), m_Name, samDesired);
}
bool CRegistryKey::SubkeyIteratorImpl::GetItem()
{
FILETIME m_lastWriteTime;
bool ok = true;
bool done = false;
// Could initialise with a call to GetKeyInfo to get longest key name etc
while (!done && ok)
{
DWORD nameLen = m_Name.GetSize();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -