📄 registry.cpp
字号:
/* Copyright (c) 1998 Sasha Djolich */
/* For questions or comments, contact the author at djolic@netinfo.ubc.ca */
#include "stdafx.h"
#include <string.h>
#include <math.h>
#include "Registry.h"
/*******************************************************************/
/* Construction/Destruction */
/*
CRegistry(const char* ApplicationName, int Mode, int Access);
ARGUMENTS
ApplicationName - pass in the path uniqely identifying your application.
This will be typically be "CompanyName\\ApplicationName\\ApplicationVersion".
An appropriate entry in the registry is created if it does not
already exist.
Mode - one of the predefined modes:
modeCurrentUserPrefs - indicates that you will be accessing
preferences for the currently logged on user
modeDefUserPrefs - indicates that you will be accessing
preferences for the default users. This information is usually
supplemented with the overriden preferences for the current user.
modeLocalMachineProps - indicates that you will be accessing
the information about the local machine.
Access - can be a combination of accessRead and accessWrite.
It must contain at least one of the two.
DESCRIPTION
You would typically want to use this constructor to do most of
your work. However should you need more control over where your
data gets put in the registry use the second form of the constructor.
NOTE
The following sections are created in the registry depending on
the value of 'Mode':
modeCurrentUserPrefs - "HKEY_CURRENT_USER\\ApplicationName"
modeDefUserPrefs - "HKEY_USERS\\.DEFAULT\\Software\\ApplicationName"
modeLocalMachineProps - "HKEY_LOCAL_MACHINE\\SOFTWARE\\ApplicationName"
*/
CRegistry::CRegistry(const char* ApplicationName, int Mode, int Access) :
mRootArea(NULL),
mRootSection(""),
mActiveSectionKey(NULL),
mActiveSection(""),
mAccess(0)
{
ASSERT(ApplicationName != NULL);
ASSERT(strlen(ApplicationName) > 0);
ASSERT((Mode == modeCurrentUserPrefs) ||
(Mode == modeDefUserPrefs) ||
(Mode == modeLocalMachineProps));
HKEY key;
CString section;
// Choose an appropriate place in the registry to store
// application preferences based on the 'Mode' argument.
if (Mode == modeCurrentUserPrefs)
{
key = HKEY_CURRENT_USER;
section = "Software\\";
section += ApplicationName;
}
else if (Mode == modeDefUserPrefs)
{
key = HKEY_USERS;
section = ".DEFAULT\\Software\\";
section += ApplicationName;
}
else if (Mode == modeLocalMachineProps)
{
key = HKEY_LOCAL_MACHINE;
section = "SOFTWARE\\";
section += ApplicationName;
}
OpenRoot(key, section, Access);
}
/*
CRegistry(HKEY Area, const char* RootSection, int Access)
ARGUMENTS
Area - pass in an already open registry key or one of the
predefined ones (HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE,
HKEY_CLASSES_ROOT, HKEY_USERS, HKEY_CURRENT_CONFIG,
HKEY_DYN_DATA).
RootSection - pass in an exact path to the section you will
be working with. The section is created if it does not
already exist.
Access - can be a combination of accessRead and accessWrite.
It must contain at least one of the two.
*/
CRegistry::CRegistry(HKEY Area, const char* RootSection, int Access)
{
OpenRoot(Area, RootSection, Access);
}
/*
~CRegistry()
DESCRIPTION
Automatically closes any open links to the registry.
*/
CRegistry::~CRegistry()
{
Close();
}
/*******************************************************************/
/* Attributes */
/*
void SetRootSection(const char* ApplicationName, int Mode)
ARGUMENTS
ApplicationName - pass in the path uniqely identifying your application.
This will be typically be "CompanyName\\ApplicationName\\ApplicationVersion".
An appropriate entry in the registry is created if it does not
already exist.
Mode - one of the predefined modes:
modeCurrentUserPrefs - indicates that you will be accessing
preferences for the currently logged on user
modeDefUserPrefs - indicates that you will be accessing
preferences for the default users. This information is usually
supplemented with the overriden preferences for the current user.
modeLocalMachineProps - indicates that you will be accessing
the information about the local machine.
DESCRIPTION
Works the same as the first form of the constructor.
Access priviliges are not changed. Use SetAccess() to specify
a different access privilege level.
*/
void CRegistry::SetRootSection(const char* ApplicationName, int Mode)
{
ASSERT(ApplicationName != NULL);
ASSERT(strlen(ApplicationName) > 0);
ASSERT((Mode == modeCurrentUserPrefs) ||
(Mode == modeDefUserPrefs) ||
(Mode == modeLocalMachineProps));
HKEY key;
CString section;
// Choose an appropriate place in the registry to store
// application preferences based on the 'Mode' argument.
if (Mode == modeCurrentUserPrefs)
{
key = HKEY_CURRENT_USER;
section = "Software\\";
section += ApplicationName;
}
else if (Mode == modeDefUserPrefs)
{
key = HKEY_USERS;
section = ".DEFAULT\\Software\\";
section += ApplicationName;
}
else if (Mode == modeLocalMachineProps)
{
key = HKEY_LOCAL_MACHINE;
section = "SOFTWARE\\";
section += ApplicationName;
}
Close();
OpenRoot(key, section, mAccess);
}
/*
void SetRootSection(HKEY Area, const char* RootSection)
ARGUMENTS
Area - pass in an already open registry key or one of the
predefined ones (HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE,
HKEY_CLASSES_ROOT, HKEY_USERS, HKEY_CURRENT_CONFIG,
HKEY_DYN_DATA).
RootSection - pass in an exact path to the section you will
be working with. The section is created if it does not
already exist.
DESCRIPTION
Works the same as the second form of the constructor.
Access priviliges are not changed. Use SetAccess() to specify
a different access privilege level.
*/
void CRegistry::SetRootSection(HKEY Area, const char* RootSection)
{
Close();
OpenRoot(Area, RootSection, mAccess);
}
/*
void SetAccess(int Access)
ARGUMENTS
Access - can be a combination of accessRead and accessWrite.
It must contain at least one of the two.
DESCRIPTION
Alters the access priviliges of an already open registry section.
*/
void CRegistry::SetAccess(int Access)
{
Close();
OpenRoot(mRootArea, mRootSection, Access);
}
/*******************************************************************/
/* Iteration */
/*
POSITION GetFirstKeyPos()
DESCRIPTION
Returns a handle to the first key in the current section;
NULL if no keys exist in the section.
Use this function in conjunction with GetNextKey() to
enumerate all the key in a section.
*/
POSITION CRegistry::GetFirstKeyPos()
{
// Determine how much memory to reserve for the value name.
DWORD maxValueNameLen = 0;
RegQueryInfoKey(mActiveSectionKey, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, &maxValueNameLen, NULL, NULL, NULL);
// Retrieve the name of the first available key.
DWORD keyNameLen = maxValueNameLen + 1;
char* keyName = new char[keyNameLen];
LONG result = RegEnumValue(mActiveSectionKey, 0, keyName, &keyNameLen, NULL, NULL, NULL, NULL);
delete[] keyName;
// If we could not successfully retrieve the name then
// this key does not exist.
if (result == ERROR_NO_MORE_ITEMS)
return NULL;
return (POSITION) 1;
}
/*
CString GetNextKey(POSITION& Pos)
ARGUMENTS
Pos - handle to the key whose name is to be retrieved
DESCRIPTION
You must pass a valid handle to a key; NULL values are not
accepted.
A string containing the name of the key referred to by 'Pos'
is returned and 'Pos' set to the next key in the section.
If there are no more keys available, 'Pos' is set to NULL.
*/
CString CRegistry::GetNextKey(POSITION& Pos)
{
ASSERT(Pos != NULL);
// Determine how much memory to reserve for the value name.
DWORD maxValueNameLen = 0;
RegQueryInfoKey(mActiveSectionKey, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, &maxValueNameLen, NULL, NULL, NULL);
// Retrieve the name of the key whose index is given in 'Pos'.
DWORD keyNameLen = maxValueNameLen + 1;
char* keyName = new char[keyNameLen];
DWORD index = ((DWORD)Pos) - 1;
LONG result = RegEnumValue(mActiveSectionKey, index, keyName, &keyNameLen, NULL, NULL, NULL, NULL);
ASSERT(result != ERROR_NO_MORE_ITEMS);
CString name = keyName;
// Determine whether there are more keys available.
result = RegEnumValue(mActiveSectionKey, index + 1, keyName, &keyNameLen, NULL, NULL, NULL, NULL);
if (result == ERROR_NO_MORE_ITEMS)
Pos = NULL;
else
Pos = (POSITION) (index + 2);
delete[] keyName;
return name;
}
/*
POSITION GetFirstSectionPos()
DESCRIPTION
Returns a handle to the first subsection in the current section;
NULL if no subsections exist in the section.
Use this function in conjunction with GetNextSection() to
enumerate all the subsections in a section.
*/
POSITION CRegistry::GetFirstSectionPos()
{
// Determine how much memory to reserve for the section name.
DWORD maxSectionNameLen = 0;
RegQueryInfoKey(mActiveSectionKey, NULL, NULL, NULL, NULL,
&maxSectionNameLen, NULL, NULL, NULL, NULL, NULL, NULL);
// Retrieve the name of the first available section.
DWORD sectionNameLen = maxSectionNameLen + 1;
char* sectionName = new char[sectionNameLen];
FILETIME lastWriteToTime;
LONG result = RegEnumKeyEx(mActiveSectionKey, 0, sectionName, §ionNameLen, NULL, NULL, NULL, &lastWriteToTime);
delete[] sectionName;
// If we could not successfully retrieve the name then
// this section does not exist.
if (result == ERROR_NO_MORE_ITEMS)
return NULL;
return (POSITION) 1;
}
/*
CString GetNextSection(POSITION& Pos)
ARGUMENTS
Pos - handle to the section whose name is to be retrieved
DESCRIPTION
You must pass a valid handle to a section; NULL values are not
accepted.
A string containing the name of the section referred to by 'Pos'
is returned and 'Pos' is set to the next available section.
If there are no more sections, 'Pos' is set to NULL.
*/
CString CRegistry::GetNextSection(POSITION& Pos)
{
ASSERT(Pos != NULL);
// Determine how much memory to reserve for the section name.
DWORD maxSectionNameLen = 0;
RegQueryInfoKey(mActiveSectionKey, NULL, NULL, NULL, NULL,
&maxSectionNameLen, NULL, NULL, NULL, NULL, NULL, NULL);
// Retrieve the name of the section whose index is given in 'Pos'.
DWORD sectionNameLen = maxSectionNameLen + 1;
char* sectionName = new char[sectionNameLen];
FILETIME lastWriteToTime;
DWORD index = ((DWORD)Pos) - 1;
LONG result = RegEnumKeyEx(mActiveSectionKey, index, sectionName, §ionNameLen, NULL, NULL, NULL, &lastWriteToTime);
ASSERT(result != ERROR_NO_MORE_ITEMS);
CString name = sectionName;
// Determine whether there are more sections available.
result = RegEnumKeyEx(mActiveSectionKey, index + 1, sectionName, §ionNameLen, NULL, NULL, NULL, &lastWriteToTime);
if (result == ERROR_NO_MORE_ITEMS)
Pos = NULL;
else
Pos = (POSITION) (index + 2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -