
AppSettings
v1.08
A set of MFC classes to encapsulate Application Settings
Welcome to
AppSettings, a collection of freeware MFC classes
to encapsulate reading and writing application configuration settings. The
supported output formats are:
- Per user in the Registry (HKEY_CURRENT_USER\Software\"Company
Name"\"Product Name"\"Product Version") via the class CHKCUAppSettings
- Per machine in the Registry (HKEY_LOCAL_MACHINE\Software\"Company
Name"\"Product Name"\"Product Version") via the class CHKLMAppSettings
- Service parameters in the registry
(HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\"Service
Name"\Parameters) via the class CServicesAppSettings. This means that you
can use this class in conjunction with the author's own
CNTService class framework
in preference to the functions for configuration settings provided by that
framework.
- A standard windows ini file via the class CIniAppSettings
- An XML file via the class CXMLAppSettings. Internally this class uses
the MSXML parser to create and parse the XML.
The ini and xml file locations can be:
- A per user roaming file stored in "\Documents and Settings\<User
Name>\Application Data\"Company Name"\"Product Name"\"Product Version" via
the method CIniAppSettings::GetRoamingDataFileLocation. Note that this
method will only work if IE4 integrated shell is installed on Windows 95.
Otherwise out of the box, this will work on Windows 98 or later.
- A per user non-roaming file stored in "\Documents and Settings\<User
Name>\Local Settings\Application Data\"Company Name"\"Product Name"\"Product
Version" via the method CIniAppSettings::GetNonRoamingDataFileLocation. Note
that this method will only work on Windows 2000 or later.
- A per computer (non-user specific and non-roaming) file stored in
"\Documents and Settings\All Users\Application Data\"Company Name"\"Product
Name"\"Product Version" via the method
CIniAppSettings::GetPerComputerDataFileLocation. Note that this method will
only work on Windows 2000 or later.
- A file in the same directory as the current process via the method
CIniAppSettings::GetCurrentProcessIniFileLocation. Note, that using this
location for an ini or xml file is not a good idea for security reasons.
- Any other location via a call to CIniAppSettings::SetIniFile or
CXMLAppSettings::SetXMLFile.
Features
- 5 different output formats are supported as mentioned above.
- To keep the API's as consistent as possible between each of the 5 main
classes, they all implement a COM like interface called "IAppSettings". This
concept is similar to a COM or .NET interface and allows you to easily swap
the output formats without having to rewrite your Get... / Write... code.
The API consists of the functions defined in the IAppSettings virtual base
class in AppSettings.h"
- The API's are loosely based on the persistence functions built into
MFC's CWinApp class which support the HKCU registry and simple ini files.
Changing your code to support AppSettings should be relatively simply. You
then quickly gain output format independence and easy access to the
locations which MS considers best practice for application settings.
- The classes hides the low level details of manipulating an XML DOM if
all you want in your XML file is some simple application settings.
- Works equally well in Unicode as well as ASCII builds.
- The classes make use of exceptions to report errors to help client code
which is developed to be as robust as possible.
- The registry and xml output format can also be optionally flushed upon a
write or left to be lazy written.
- The xml output format can be optionally "pretty printed".
- Unlike the MFC CWinApp persistence functions, these classes throw
exceptions when a value is not located. This allows you to easily
distinguish from when a configuration setting is present and is the default
value and when it is not present. Your client code should expect these
unexpected conditions and handle them by catching the "CAppSettingsException*"
exceptions which are throw in these cases.
The enclosed
zip file contains the
AppSettings source code and a simple console based test app to exercise all the
classes.
Copyright
- You are allowed to include the source code in
any product (commercial, shareware, freeware or otherwise) when your product
is released in binary form.
- You are allowed to modify the source code in
any way you want except you cannot modify the copyright details at the top
of each module.
- If you want to distribute source code with
your application, then you are only allowed to distribute versions released
by the author. This is to maintain a single distribution point for the
source code.
Updates
V1.0 (11
June 2006)
V1.01 (29 July 2006)
- All AppSettings classes now support "GetSection" and "WriteSection"
methods. These new functions are modelled on the "GetPrivateProfileSection"
and "WritePrivateProfileSection" Win32 API calls. Thanks to Brad Bruce for
suggesting these updates.
- Made the AfxThrowWin32AppSettingsException function a member of
IAppSettings and renamed it to ThrowWin32AppSettingsException
- Made the AfxThrowCOMAppSettingsException function a member of
IAppSettings and renamed it to ThrowCOMAppSettingsException
- Code now uses newer C++ style casts instead of C style casts.
- Made all the WriteBinary methods now accept a const BYTE* parameter
instead of LPBYTE
- Updated the code to clean compile on VC 2005
- Fixed a bug in CXMLAppSettings::GetBinary where returned binary data was
being returned incorrectly.
V1.02 (19 August 2006)
- Fixed compile problems when code is compiled in a mixed MFC / ATL project.
- Addition of "WriteProfile..." functions to IAppSettings which provides
similar functionality to the MFC CWinApp functions of the same name. These
new functions allow you to ignore errors reading a value and instead return
a default value.
- Removed the MFC C++ runtime macros from CAppSettingsException as they
were not really required.
V1.03 (6 January 2007)
- Updated copyright details.
- Fixed a bug in CRegistryAppSettings::SetStringArrayIntoRegistry related
to use of _tcscpy_s
- Fixed a bug in CIniAppSettings::WriteStringArray related to use of _tcscpy_s
- Fixed a bug in CXMLAppSettings::WriteStringArray related to use of _tcscpy_s
V1.04 (5 July 2007)
- Added support for "Pretty Printing" to the CXMLAppSettings class. This
is achieved through an additional parameter to the constructor.
V1.05 (24 December 2007)
V1.06 (28 December 2007)
- CAppSettingsException::GetErrorMessage now uses Checked::tcsncpy_s if
compiled using VC 2005 or later.
V1.07 (6 June 2008)
- Updated copyright details.
- Updated sample app to clean compile on VC 2008
- The code has now been updated to support VC 2005 or later only.
- Code now compiles cleanly using Code Analysis (/analyze)
V1.08 (19 March 2009)
- Updated copyright details.
- Updated the sample app solution settings to use more reasonable defaults
- The IAppSettings interface now supports a new GetSections method. This
returns all the root sections which a configuration file has. Thanks to Rolf
Kristensen for prompting this nice addition to the classes.
- Updated the IAppSettings::GetSection method to support a boolean "bWithValues"
parameter which determines if the returned string array should or should
name include the values. This allows client code to easily get all the
sections names. Thanks to Rolf Kristensen for providing this nice addition
to the classes.
- Reworked most of the internal code which does heap allocations using
"new" to instead now use ATL::CHeapPtr.