📄 registrykey.cpp
字号:
/* $Id: RegistryKey.cpp 12803 2005-01-04 21:40:25Z narnaoud $
*
* regexpl - Console Registry Explorer
*
* Copyright (C) 2000-2005 Nedko Arnaudov <nedko@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
// RegistryKey.cpp: implementation of the CRegistryKey class.
//
//////////////////////////////////////////////////////////////////////
#include "ph.h"
#include "RegistryKey.h"
static TCHAR *g_ppszHiveNames[] =
{
_T("HKEY_CLASSES_ROOT"),
_T("HKEY_CURRENT_USER"),
_T("HKEY_LOCAL_MACHINE"),
_T("HKEY_USERS"),
_T("HKEY_PERFORMANCE_DATA"),
_T("HKEY_CURRENT_CONFIG"),
_T("HKEY_DYN_DATA")
};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CRegistryKey::CRegistryKey()
{
m_pszKeyName = NULL;
m_pszMachineName = NULL;
m_CurrentAccess = 0;
m_hKey = NULL;
}
HRESULT CRegistryKey::InitRoot(const TCHAR *pszMachineName)
{
if ((pszMachineName)&&
((_tcslen(pszMachineName) < 3)||
(pszMachineName[0] != _T('\\'))||
(pszMachineName[1] != _T('\\'))
)
)
{
return E_INVALIDARG;
}
HRESULT hr = Uninit();
if (FAILED(hr))
return hr;
if (pszMachineName)
{ // copy machine name
size_t size = _tcslen(pszMachineName);
m_pszMachineName = new TCHAR [size+2];
if (!m_pszMachineName)
return E_OUTOFMEMORY;
_tcscpy(m_pszMachineName,pszMachineName);
m_pszMachineName[size] = _T('\\');
m_pszMachineName[size+1] = 0;
}
else
{
m_pszMachineName = NULL; // local registry
}
ASSERT(m_pszKeyName == NULL);
m_CurrentAccess = 0;
ASSERT(m_hKey == NULL);
return S_OK;
}
HRESULT CRegistryKey::Init(HKEY hKey, const TCHAR *pszPath, const TCHAR *pszKeyName, REGSAM CurrentAccess)
{
HRESULT hr = Uninit();
if (FAILED(hr))
return hr;
if (!pszKeyName || !hKey)
return E_INVALIDARG;
// copy key name name
size_t size = _tcslen(pszKeyName);
if (pszPath)
size += _tcslen(pszPath);
m_pszKeyName = new TCHAR [size+2];
if (!m_pszKeyName)
return E_OUTOFMEMORY;
_stprintf(m_pszKeyName,_T("%s%s\\"),pszPath?pszPath:_T(""),pszKeyName);
m_CurrentAccess = CurrentAccess;
m_hKey = hKey;
ASSERT(m_hKey);
return S_OK;
}
HRESULT CRegistryKey::Uninit()
{
if (m_pszKeyName)
{
delete [] m_pszKeyName;
m_pszKeyName = NULL;
}
if (m_pszMachineName)
{
delete [] m_pszMachineName;
m_pszMachineName = NULL;
}
LONG nError = ERROR_SUCCESS;
if((m_hKey != NULL)&&(!IsHive(m_hKey)))
nError = RegCloseKey(m_hKey);
m_hKey = NULL;
return (nError == ERROR_SUCCESS)?S_OK:E_FAIL;
}
BOOL CRegistryKey::IsHive(HKEY hKey)
{
return ((hKey == HKEY_CLASSES_ROOT)||
(hKey == HKEY_CURRENT_USER)||
(hKey == HKEY_LOCAL_MACHINE)||
(hKey == HKEY_USERS)||
(hKey == HKEY_PERFORMANCE_DATA)||
(hKey == HKEY_CURRENT_CONFIG)||
(hKey == HKEY_DYN_DATA));
}
CRegistryKey::~CRegistryKey()
{
Uninit();
}
const TCHAR * CRegistryKey::GetKeyName()
{
return m_pszKeyName?m_pszKeyName:(m_pszMachineName?m_pszMachineName:_T("\\"));
}
BOOL CRegistryKey::IsRoot()
{
return m_hKey == NULL;
}
LONG CRegistryKey::OpenSubkey(REGSAM samDesired, const TCHAR *pszSubkeyName, HKEY &rhKey)
{
if (m_hKey == NULL)
{ // subkey of root key is hive root key.
if ((_tcsicmp(pszSubkeyName,_T("HKCR")) == 0)||
(_tcsicmp(pszSubkeyName,_T("HKEY_CLASSES_ROOT")) == 0))
{
rhKey = HKEY_CLASSES_ROOT;
if (m_pszMachineName)
return ERROR_FILE_NOT_FOUND;
return ERROR_SUCCESS;
}
else if ((_tcsicmp(pszSubkeyName,_T("HKCU")) == 0)||
(_tcsicmp(pszSubkeyName,_T("HKEY_CURRENT_USER")) == 0))
{
rhKey = HKEY_CURRENT_USER;
if (m_pszMachineName)
return ERROR_FILE_NOT_FOUND;
return ERROR_SUCCESS;
}
else if ((_tcsicmp(pszSubkeyName,_T("HKLM")) == 0)||
(_tcsicmp(pszSubkeyName,_T("HKEY_LOCAL_MACHINE")) == 0))
{
rhKey = HKEY_LOCAL_MACHINE;
if (m_pszMachineName)
return RegConnectRegistry(m_pszMachineName,rhKey,&rhKey);
return ERROR_SUCCESS;
}
else if ((_tcsicmp(pszSubkeyName,_T("HKU")) == 0)||
(_tcsicmp(pszSubkeyName,_T("HKEY_USERS")) == 0))
{
rhKey = HKEY_USERS;
if (m_pszMachineName)
return RegConnectRegistry(m_pszMachineName,rhKey,&rhKey);
return ERROR_SUCCESS;
}
else if ((_tcsicmp(pszSubkeyName,_T("HKPD")) == 0)||
(_tcsicmp(pszSubkeyName,_T("HKEY_PERFORMANCE_DATA")) == 0))
{
rhKey = HKEY_PERFORMANCE_DATA;
if (m_pszMachineName)
return RegConnectRegistry(m_pszMachineName,rhKey,&rhKey);
return ERROR_SUCCESS;
}
else if ((_tcsicmp(pszSubkeyName,_T("HKDD")) == 0)||
(_tcsicmp(pszSubkeyName,_T("HKEY_DYN_DATA")) == 0))
{
rhKey = HKEY_DYN_DATA;
if (m_pszMachineName)
return RegConnectRegistry(m_pszMachineName,rhKey,&rhKey);
return ERROR_SUCCESS;
}
else if ((_tcsicmp(pszSubkeyName,_T("HKCC")) == 0)||
(_tcsicmp(pszSubkeyName,_T("HKEY_CURRENT_CONFIG")) == 0))
{
rhKey = HKEY_CURRENT_CONFIG;
if (m_pszMachineName)
{
TCHAR *pch = m_pszMachineName;
while (*pch)
pch++;
pch--;
ASSERT(*pch == _T('\\'));
if (*pch != _T('\\'))
return ERROR_INTERNAL_ERROR;
*pch = 0;
LONG nError = RegConnectRegistry(m_pszMachineName,rhKey,&rhKey);
*pch = _T('\\');
return nError;
}
return ERROR_SUCCESS;
}
else
{
return ERROR_FILE_NOT_FOUND;
}
}
return RegOpenKeyEx(m_hKey,pszSubkeyName,0,samDesired,&rhKey);
}
LONG CRegistryKey::OpenSubkey(REGSAM samDesired, const TCHAR *pszSubkeyName, CRegistryKey &rKey)
{
HKEY hKey;
LONG nError = OpenSubkey(samDesired, pszSubkeyName, hKey);
if (nError == ERROR_SUCCESS)
{
const TCHAR *pszKeyName = GetKeyName();
size_t size = _tcslen(pszKeyName) + _tcslen(pszSubkeyName) + 1;
TCHAR *pszSubkeyFullName = new TCHAR [size];
if (!pszSubkeyFullName)
{
nError = RegCloseKey(hKey);
ASSERT(nError == ERROR_SUCCESS);
return ERROR_OUTOFMEMORY;
}
_tcscpy(pszSubkeyFullName,pszKeyName);
_tcscat(pszSubkeyFullName,pszSubkeyName);
HRESULT hr = rKey.Init(hKey,GetKeyName(),pszSubkeyName,samDesired);
delete pszSubkeyName;
if (FAILED(hr))
{
nError = RegCloseKey(hKey);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -