registry.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 1,421 行 · 第 1/3 页
CPP
1,421 行
///////////////////////////////////////////////////////////////////////////////
// Name: msw/registry.cpp
// Purpose: implementation of registry classes and functions
// Author: Vadim Zeitlin
// Modified by:
// Created: 03.04.98
// RCS-ID: $Id: registry.cpp,v 1.77.2.1 2005/12/06 19:07:12 JS Exp $
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
// Licence: wxWindows licence
// TODO: - parsing of registry key names
// - support of other (than REG_SZ/REG_DWORD) registry types
// - add high level functions (RegisterOleServer, ...)
///////////////////////////////////////////////////////////////////////////////
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "registry.h"
#endif
// for compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// other wxWidgets headers
#include "wx/string.h"
#include "wx/intl.h"
#include "wx/log.h"
#include "wx/file.h"
#include "wx/wfstream.h"
// Windows headers
#include "wx/msw/wrapwin.h"
#ifdef __WXWINCE__
#include "wx/msw/private.h"
#include <winbase.h>
#include <winreg.h>
#endif
// other std headers
#include <stdlib.h> // for _MAX_PATH
#ifndef _MAX_PATH
#define _MAX_PATH 512
#endif
// our header
#define HKEY_DEFINED // already defined in windows.h
#include "wx/msw/registry.h"
// some registry functions don't like signed chars
typedef unsigned char *RegString;
typedef BYTE* RegBinary;
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// the standard key names, short names and handles all bundled together for
// convenient access
static struct
{
HKEY hkey;
const wxChar *szName;
const wxChar *szShortName;
}
aStdKeys[] =
{
{ HKEY_CLASSES_ROOT, wxT("HKEY_CLASSES_ROOT"), wxT("HKCR") },
{ HKEY_CURRENT_USER, wxT("HKEY_CURRENT_USER"), wxT("HKCU") },
{ HKEY_LOCAL_MACHINE, wxT("HKEY_LOCAL_MACHINE"), wxT("HKLM") },
{ HKEY_USERS, wxT("HKEY_USERS"), wxT("HKU") }, // short name?
#ifndef __WXWINCE__
{ HKEY_PERFORMANCE_DATA, wxT("HKEY_PERFORMANCE_DATA"), wxT("HKPD") },
#endif
#ifdef HKEY_CURRENT_CONFIG
{ HKEY_CURRENT_CONFIG, wxT("HKEY_CURRENT_CONFIG"), wxT("HKCC") },
#endif
#ifdef HKEY_DYN_DATA
{ HKEY_DYN_DATA, wxT("HKEY_DYN_DATA"), wxT("HKDD") }, // short name?
#endif
};
// the registry name separator (perhaps one day MS will change it to '/' ;-)
#define REG_SEPARATOR wxT('\\')
// useful for Windows programmers: makes somewhat more clear all these zeroes
// being passed to Windows APIs
#define RESERVED (0)
// ----------------------------------------------------------------------------
// macros
// ----------------------------------------------------------------------------
// const_cast<> is not yet supported by all compilers
#define CONST_CAST ((wxRegKey *)this)->
// and neither is mutable which m_dwLastError should be
#define m_dwLastError CONST_CAST m_dwLastError
// ----------------------------------------------------------------------------
// non member functions
// ----------------------------------------------------------------------------
// removes the trailing backslash from the string if it has one
static inline void RemoveTrailingSeparator(wxString& str);
// returns true if given registry key exists
static bool KeyExists(WXHKEY hRootKey, const wxChar *szKey);
// combines value and key name (uses static buffer!)
static const wxChar *GetFullName(const wxRegKey *pKey,
const wxChar *szValue = NULL);
// ============================================================================
// implementation of wxRegKey class
// ============================================================================
// ----------------------------------------------------------------------------
// static functions and variables
// ----------------------------------------------------------------------------
const size_t wxRegKey::nStdKeys = WXSIZEOF(aStdKeys);
// @@ should take a `StdKey key', but as it's often going to be used in loops
// it would require casts in user code.
const wxChar *wxRegKey::GetStdKeyName(size_t key)
{
// return empty string if key is invalid
wxCHECK_MSG( key < nStdKeys, wxEmptyString, wxT("invalid key in wxRegKey::GetStdKeyName") );
return aStdKeys[key].szName;
}
const wxChar *wxRegKey::GetStdKeyShortName(size_t key)
{
// return empty string if key is invalid
wxCHECK( key < nStdKeys, wxEmptyString );
return aStdKeys[key].szShortName;
}
wxRegKey::StdKey wxRegKey::ExtractKeyName(wxString& strKey)
{
wxString strRoot = strKey.BeforeFirst(REG_SEPARATOR);
HKEY hRootKey = 0;
size_t ui;
for ( ui = 0; ui < nStdKeys; ui++ ) {
if ( strRoot.CmpNoCase(aStdKeys[ui].szName) == 0 ||
strRoot.CmpNoCase(aStdKeys[ui].szShortName) == 0 ) {
hRootKey = aStdKeys[ui].hkey;
break;
}
}
if ( ui == nStdKeys ) {
wxFAIL_MSG(wxT("invalid key prefix in wxRegKey::ExtractKeyName."));
hRootKey = HKEY_CLASSES_ROOT;
}
else {
strKey = strKey.After(REG_SEPARATOR);
if ( !strKey.empty() && strKey.Last() == REG_SEPARATOR )
strKey.Truncate(strKey.Len() - 1);
}
return (wxRegKey::StdKey)(int)hRootKey;
}
wxRegKey::StdKey wxRegKey::GetStdKeyFromHkey(WXHKEY hkey)
{
for ( size_t ui = 0; ui < nStdKeys; ui++ ) {
if ( (int) aStdKeys[ui].hkey == (int) hkey )
return (StdKey)ui;
}
wxFAIL_MSG(wxT("non root hkey passed to wxRegKey::GetStdKeyFromHkey."));
return HKCR;
}
// ----------------------------------------------------------------------------
// ctors and dtor
// ----------------------------------------------------------------------------
wxRegKey::wxRegKey()
{
m_hRootKey = (WXHKEY) aStdKeys[HKCR].hkey;
Init();
}
wxRegKey::wxRegKey(const wxString& strKey) : m_strKey(strKey)
{
m_hRootKey = (WXHKEY) aStdKeys[ExtractKeyName(m_strKey)].hkey;
Init();
}
// parent is a predefined (and preopened) key
wxRegKey::wxRegKey(StdKey keyParent, const wxString& strKey) : m_strKey(strKey)
{
RemoveTrailingSeparator(m_strKey);
m_hRootKey = (WXHKEY) aStdKeys[keyParent].hkey;
Init();
}
// parent is a normal regkey
wxRegKey::wxRegKey(const wxRegKey& keyParent, const wxString& strKey)
: m_strKey(keyParent.m_strKey)
{
// combine our name with parent's to get the full name
if ( !m_strKey.empty() &&
(strKey.empty() || strKey[0] != REG_SEPARATOR) ) {
m_strKey += REG_SEPARATOR;
}
m_strKey += strKey;
RemoveTrailingSeparator(m_strKey);
m_hRootKey = keyParent.m_hRootKey;
Init();
}
// dtor closes the key releasing system resource
wxRegKey::~wxRegKey()
{
Close();
}
// ----------------------------------------------------------------------------
// change the key name/hkey
// ----------------------------------------------------------------------------
// set the full key name
void wxRegKey::SetName(const wxString& strKey)
{
Close();
m_strKey = strKey;
m_hRootKey = (WXHKEY) aStdKeys[ExtractKeyName(m_strKey)].hkey;
}
// the name is relative to the parent key
void wxRegKey::SetName(StdKey keyParent, const wxString& strKey)
{
Close();
m_strKey = strKey;
RemoveTrailingSeparator(m_strKey);
m_hRootKey = (WXHKEY) aStdKeys[keyParent].hkey;
}
// the name is relative to the parent key
void wxRegKey::SetName(const wxRegKey& keyParent, const wxString& strKey)
{
Close();
// combine our name with parent's to get the full name
// NB: this method is called by wxRegConfig::SetPath() which is a performance
// critical function and so it preallocates space for our m_strKey to
// gain some speed - this is why we only use += here and not = which
// would just free the prealloc'd buffer and would have to realloc it the
// next line!
m_strKey.clear();
m_strKey += keyParent.m_strKey;
if ( !strKey.empty() && strKey[0] != REG_SEPARATOR )
m_strKey += REG_SEPARATOR;
m_strKey += strKey;
RemoveTrailingSeparator(m_strKey);
m_hRootKey = keyParent.m_hRootKey;
}
// hKey should be opened and will be closed in wxRegKey dtor
void wxRegKey::SetHkey(WXHKEY hKey)
{
Close();
m_hKey = hKey;
}
// ----------------------------------------------------------------------------
// info about the key
// ----------------------------------------------------------------------------
// returns true if the key exists
bool wxRegKey::Exists() const
{
// opened key has to exist, try to open it if not done yet
return IsOpened() ? true : KeyExists(m_hRootKey, m_strKey);
}
// returns the full name of the key (prefix is abbreviated if bShortPrefix)
wxString wxRegKey::GetName(bool bShortPrefix) const
{
StdKey key = GetStdKeyFromHkey((WXHKEY) m_hRootKey);
wxString str = bShortPrefix ? aStdKeys[key].szShortName
: aStdKeys[key].szName;
if ( !m_strKey.empty() )
str << _T("\\") << m_strKey;
return str;
}
bool wxRegKey::GetKeyInfo(size_t *pnSubKeys,
size_t *pnMaxKeyLen,
size_t *pnValues,
size_t *pnMaxValueLen) const
{
// old gcc headers incorrectly prototype RegQueryInfoKey()
#if defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)
#define REG_PARAM (size_t *)
#else
#define REG_PARAM (LPDWORD)
#endif
// it might be unexpected to some that this function doesn't open the key
wxASSERT_MSG( IsOpened(), _T("key should be opened in GetKeyInfo") );
m_dwLastError = ::RegQueryInfoKey
(
(HKEY) m_hKey,
NULL, // class name
NULL, // (ptr to) size of class name buffer
RESERVED,
REG_PARAM
pnSubKeys, // [out] number of subkeys
REG_PARAM
pnMaxKeyLen, // [out] max length of a subkey name
NULL, // longest subkey class name
REG_PARAM
pnValues, // [out] number of values
REG_PARAM
pnMaxValueLen, // [out] max length of a value name
NULL, // longest value data
NULL, // security descriptor
NULL // time of last modification
);
#undef REG_PARAM
if ( m_dwLastError != ERROR_SUCCESS ) {
wxLogSysError(m_dwLastError, _("Can't get info about registry key '%s'"),
GetName().c_str());
return false;
}
return true;
}
// ----------------------------------------------------------------------------
// operations
// ----------------------------------------------------------------------------
// opens key (it's not an error to call Open() on an already opened key)
bool wxRegKey::Open(AccessMode mode)
{
if ( IsOpened() )
{
if ( mode <= m_mode )
return true;
// we had been opened in read mode but now must be reopened in write
Close();
}
HKEY tmpKey;
m_dwLastError = ::RegOpenKeyEx
(
(HKEY) m_hRootKey,
m_strKey,
RESERVED,
mode == Read ? KEY_READ : KEY_ALL_ACCESS,
&tmpKey
);
if ( m_dwLastError != ERROR_SUCCESS )
{
wxLogSysError(m_dwLastError, _("Can't open registry key '%s'"),
GetName().c_str());
return false;
}
m_hKey = (WXHKEY) tmpKey;
m_mode = mode;
return true;
}
// creates key, failing if it exists and !bOkIfExists
bool wxRegKey::Create(bool bOkIfExists)
{
// check for existence only if asked (i.e. order is important!)
if ( !bOkIfExists && Exists() )
return false;
if ( IsOpened() )
return true;
HKEY tmpKey;
#ifdef __WXWINCE__
DWORD disposition;
m_dwLastError = RegCreateKeyEx((HKEY) m_hRootKey, m_strKey,
NULL, // reserved
NULL, // class string
0,
0,
NULL,
&tmpKey,
&disposition);
#else
m_dwLastError = RegCreateKey((HKEY) m_hRootKey, m_strKey, &tmpKey);
#endif
if ( m_dwLastError != ERROR_SUCCESS ) {
wxLogSysError(m_dwLastError, _("Can't create registry key '%s'"),
GetName().c_str());
return false;
}
else
{
m_hKey = (WXHKEY) tmpKey;
return true;
}
}
// close the key, it's not an error to call it when not opened
bool wxRegKey::Close()
{
if ( IsOpened() ) {
m_dwLastError = RegCloseKey((HKEY) m_hKey);
m_hKey = 0;
if ( m_dwLastError != ERROR_SUCCESS ) {
wxLogSysError(m_dwLastError, _("Can't close registry key '%s'"),
GetName().c_str());
return false;
}
}
return true;
}
bool wxRegKey::RenameValue(const wxChar *szValueOld, const wxChar *szValueNew)
{
bool ok = true;
if ( HasValue(szValueNew) ) {
wxLogError(_("Registry value '%s' already exists."), szValueNew);
ok = false;
}
if ( !ok ||
!CopyValue(szValueOld, *this, szValueNew) ||
!DeleteValue(szValueOld) ) {
wxLogError(_("Failed to rename registry value '%s' to '%s'."),
szValueOld, szValueNew);
return false;
}
return true;
}
bool wxRegKey::CopyValue(const wxChar *szValue,
wxRegKey& keyDst,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?