📄 regkey.cpp
字号:
return ERROR_SUCCESS;
}
//****************************************************************************
//
// CRegistryKey::Close
//
//****************************************************************************
LONG CRegistryKey::Close(BOOL bForce)
{
if (!g_bLostConnection) {
m_lResult = ERROR_SUCCESS;
if (!m_bOpen)
return ERROR_SUCCESS;
if (m_bDirty)
{
m_lResult = RegFlushKey(m_hkeyOpen);
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
if (!bForce && m_lResult != ERROR_SUCCESS)
return m_lResult;
}
if (!g_bLostConnection) {
m_lResult = RegCloseKey(m_hkeyOpen);
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
if (!bForce && m_lResult != ERROR_SUCCESS)
return m_lResult;
}
}
m_bDirty = FALSE;
m_hkeyOpen = NULL;
m_bOpen = FALSE;
m_sFullName.Empty();
m_sClass.Empty();
m_Sam = 0;
m_dwSubKeys = 0;
m_dwMaxSubKey = 0;
m_dwMaxClass = 0;
m_dwValues = 0;
m_dwMaxValueName = 0;
m_dwMaxValueData = 0;
m_dwSecurityDescriptor = 0;
m_ftLastWriteTime.dwLowDateTime = 0;
m_ftLastWriteTime.dwHighDateTime = 0;
if (g_bLostConnection) {
m_lResult = RPC_S_SERVER_UNAVAILABLE;
return RPC_S_SERVER_UNAVAILABLE;
}
else {
m_lResult = ERROR_SUCCESS;
return ERROR_SUCCESS;
}
}
//****************************************************************************
//
// CRegistryKey::EnumValues
//
// Returns NULL if unsuccessful, returns empty array if successful but open
// key has no values.
// NOTE: Caller is responsible for deleting returned string array.
//
//****************************************************************************
CStringArray* CRegistryKey::EnumValues()
{
if (g_bLostConnection) {
m_lResult = RPC_S_SERVER_UNAVAILABLE;
return NULL;
}
TCHAR szBuffer[1024 + 1];
DWORD dwLength;
CStringArray *pArr;
int i;
m_lResult = ERROR_SUCCESS;
if (!m_bOpen || g_bLostConnection)
return NULL;
// Enumerate all the values into a string array
pArr = new CStringArray;
i = 0;
m_lResult = ERROR_SUCCESS;
while (TRUE)
{
dwLength = 1024 + 1;
m_lResult = RegEnumValue(m_hkeyOpen, i, szBuffer, &dwLength, NULL,
NULL, NULL, NULL);
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
if (m_lResult != ERROR_SUCCESS)
break;
if (dwLength > 0)
pArr->Add(szBuffer);
i++;
}
// Did we find a normal end condition?
if (m_lResult == ERROR_NO_MORE_ITEMS)
return pArr;
delete pArr;
return NULL;
}
//****************************************************************************
//
// CRegistryKey::EnumSubKeys
//
// Returns NULL if unsuccessful, returns empty array if successful but open
// key has no values.
// NOTE: Caller is responsible for deleting returned string array.
//
//****************************************************************************
CStringArray* CRegistryKey::EnumSubKeys()
{
if (g_bLostConnection) {
m_lResult = RPC_S_SERVER_UNAVAILABLE;
return NULL;
}
TCHAR szBuffer[1024 + 1];
DWORD dwLength;
CStringArray *pArr;
int i;
m_lResult = ERROR_SUCCESS;
if (!m_bOpen)
return NULL;
// Enumerate all the subkeys into a string array
pArr = new CStringArray;
i = 0;
while (TRUE)
{
dwLength = 1024 + 1;
m_lResult = RegEnumKeyEx(m_hkeyOpen, i, szBuffer, &dwLength, NULL,
NULL, NULL, NULL);
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
if (m_lResult != ERROR_SUCCESS)
break;
if (dwLength > 0)
pArr->Add(szBuffer);
i++;
}
// Did we find a normal end condition?
if (m_lResult == ERROR_NO_MORE_ITEMS)
return pArr;
delete pArr;
return NULL;
}
//****************************************************************************
//
// CRegistryKey::GetValue
//
// Note: regval is always emptied regardless of success/failure
//
//****************************************************************************
BOOL CRegistryKey::GetValue(LPCTSTR pszValue, CRegistryValue ®val)
{
DWORD dwLength, dwType;
BYTE *pBuffer;
regval.Empty();
if (g_bLostConnection) {
m_lResult = RPC_S_SERVER_UNAVAILABLE;
return FALSE;
}
if (!m_bOpen)
{
m_lResult = ERROR_INVALID_FUNCTION;
return FALSE;
}
// Find out how big the data is
m_lResult = RegQueryValueEx(m_hkeyOpen, (LPTSTR)pszValue, NULL, NULL,
NULL, &dwLength);
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
if (m_lResult != ERROR_SUCCESS)
return FALSE;
if (dwLength == 0)
return TRUE;
// Now make a buffer big enough for it.
pBuffer = new BYTE[dwLength];
if (pBuffer == NULL)
return FALSE;
m_lResult = RegQueryValueEx(m_hkeyOpen, (LPTSTR)pszValue, NULL, &dwType,
pBuffer, &dwLength);
if (m_lResult == ERROR_SUCCESS)
regval.Set(pszValue, dwType, dwLength, pBuffer);
delete pBuffer;
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
if (m_lResult != ERROR_SUCCESS)
return FALSE;
return TRUE;
}
//****************************************************************************
//
// CRegistryKey::SetValue
//
//****************************************************************************
BOOL CRegistryKey::SetValue(CRegistryValue ®val)
{
if (g_bLostConnection) {
m_lResult = RPC_S_SERVER_UNAVAILABLE;
return FALSE;
}
if (!m_bOpen)
{
m_lResult = ERROR_INVALID_FUNCTION;
return FALSE;
}
if (regval.m_sName.IsEmpty())
{
m_lResult = ERROR_INVALID_DATA;
return FALSE;
}
m_lResult = RegSetValueEx(m_hkeyOpen, regval.m_sName, 0, regval.m_dwType,
regval.m_pData, regval.m_dwDataLength);
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
if (m_lResult != ERROR_SUCCESS)
return FALSE;
m_bDirty = TRUE;
return TRUE;
}
//****************************************************************************
//
// CRegistryKey::GetSubKey
//
// Note: If successful, regkey is returned connected and open on the
// specified key. If failure, regkey is returned disconnected.
//
//****************************************************************************
BOOL CRegistryKey::GetSubKey(LPCTSTR pszSubKey, CRegistryKey ®key)
{
if (g_bLostConnection) {
m_lResult = RPC_S_SERVER_UNAVAILABLE;
return FALSE;
}
CString sSubKey;
m_lResult = ERROR_SUCCESS;
if (!m_bOpen)
return FALSE;
m_lResult = regkey.Disconnect(TRUE);
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
if (m_lResult != ERROR_SUCCESS)
return FALSE;
// Try to connect and open same key
m_lResult = regkey.Connect(m_sComputer, m_hkeyConnect);
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
if (m_lResult != ERROR_SUCCESS)
return FALSE;
sSubKey = pszSubKey;
m_lResult = regkey.Open(m_sFullName + "\\" + sSubKey, m_Sam);
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
if (m_lResult != ERROR_SUCCESS)
{
regkey.Disconnect(TRUE);
return FALSE;
}
return TRUE;
}
//****************************************************************************
//
// CRegistryKey::CreateSubKey
//
// Note: If successful, regkey is returned connected and open on the
// new key; if the key already existed, it is simply opened. If failure,
// regkey is returned disconnected.
//
//****************************************************************************
BOOL CRegistryKey::CreateSubKey(
LPCTSTR pszSubKey,
CRegistryKey ®key,
LPCTSTR pszClass,
LPSECURITY_ATTRIBUTES lpSecAttr,
BOOL bIsVolatile)
{
if (g_bLostConnection) {
m_lResult = RPC_S_SERVER_UNAVAILABLE;
return FALSE;
}
CString sSubKey, sClass;
HKEY hkeyOpen;
DWORD dwDisposition;
m_lResult = ERROR_SUCCESS;
if (!m_bOpen)
return FALSE;
m_lResult = regkey.Disconnect(TRUE);
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
if (m_lResult != ERROR_SUCCESS)
return FALSE;
// Try to connect and open same key
m_lResult = regkey.Connect(m_sComputer, m_hkeyConnect);
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
if (m_lResult != ERROR_SUCCESS)
return FALSE;
sSubKey = pszSubKey;
sClass = pszClass;
DWORD dwRegOptions = bIsVolatile ? REG_OPTION_VOLATILE : REG_OPTION_NON_VOLATILE;
m_lResult = RegCreateKeyEx(m_hkeyOpen, sSubKey, 0, (LPTSTR)(LPCTSTR)sClass,
dwRegOptions, m_Sam, lpSecAttr, &hkeyOpen, &dwDisposition);
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
if (m_lResult != ERROR_SUCCESS)
{
regkey.Disconnect(TRUE);
return FALSE;
}
m_lResult = RegCloseKey(hkeyOpen);
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
if (m_lResult != ERROR_SUCCESS)
return FALSE;
m_lResult = regkey.Open(m_sFullName + "\\" + sSubKey, m_Sam);
if (m_lResult != ERROR_SUCCESS)
{
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
regkey.Disconnect(TRUE);
return FALSE;
}
m_bDirty = TRUE;
if (dwDisposition == REG_CREATED_NEW_KEY)
regkey.m_bDirty = TRUE;
return TRUE;
}
//****************************************************************************
//
// CRegistryKey::DeleteSubKey
//
//****************************************************************************
BOOL CRegistryKey::DeleteSubKey(LPCTSTR pszSubKey)
{
if (g_bLostConnection) {
return FALSE;
}
CString sSubKey;
CRegistryKey subkey;
int i;
m_lResult = ERROR_SUCCESS;
sSubKey = pszSubKey;
if (!m_bOpen)
return FALSE;
if (!GetSubKey(sSubKey, subkey))
return FALSE;
// Delete all subkeys of the specified subkey (RegDeleteKey limitation)
CStringArray *parr = subkey.EnumSubKeys();
for (i=0; i<parr->GetSize(); i++)
{
if (!subkey.DeleteSubKey(parr->GetAt(i)))
return FALSE;
}
delete parr;
subkey.Close(TRUE);
m_lResult = RegDeleteKey(m_hkeyOpen, sSubKey);
if (m_lResult != ERROR_SUCCESS) {
if ((m_lResult == RPC_S_SERVER_UNAVAILABLE) || (m_lResult == RPC_S_CALL_FAILED)) {
g_bLostConnection = TRUE;
}
return FALSE;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -