/** * @file XTPRegistryManager.h * * @copyright * (c) 1998-2025 Codejock Software, All Rights Reserved. * * This source file is the property of Codejock Software and must not be * redistributed by any means without the explicit written permission of * Codejock Software. * * The use of this source code is governed by the terms and conditions specified * in the Toolkit Pro license agreement. Codejock Software grants you, as a * single software developer, the limited right to use this software on one * computer only. * * Contact Information: * support@codejock.com * http://www.codejock.com * */ /** @cond */ #if !defined(__XTPREGISTRYMANAGER_H__) # define __XTPREGISTRYMANAGER_H__ /** @endcond */ # if _MSC_VER > 1000 # pragma once # endif // _MSC_VER > 1000 # include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" /** * @brief * CXTPRegistryManager is a standalone registry management class. You * can use it to read and write values from your system's registry or an * .INI file. Use CXTPRegistryManager to read and write string, integer, * and binary data to and from the registry. You can also specify an .INI * file name, and have these values stored there instead. */ class _XTP_EXT_CLASS CXTPRegistryManager { private: class CHKey; public: /** * @brief * Constructs a CXTPRegistryManager object. * @param hKeyBase Current key to be used in the registry. By default, * this parameter is HKEY_CURRENT_USER. */ CXTPRegistryManager(HKEY hKeyBase = HKEY_CURRENT_USER); /** * @brief * Destroys a CXTPRegistryManager object, handles cleanup and deallocation. */ virtual ~CXTPRegistryManager(); public: /** * @brief * This member function will set a registry key. * @param lpszRegistryKey Used to determine the full registry key for storing * application profile settings. * @param lpszProfileName The application's .INI filename. * @details * Calling SetRegistryKey will initialize m_pszProfileName and * m_pszRegistryKey for CWinApp. */ void SetRegistryKey(LPCTSTR lpszRegistryKey, LPCTSTR lpszProfileName); protected: HKEY m_hKeyBase; /**< Handle to the registry key to use. The default is HKEY_CURRENT_USER. */ CString m_strRegistryKey; /**< Used to determine the full registry key for storing application profile settings. */ CString m_strProfileName; /**< The application's .INI filename. */ static CString m_strINIFileName; /**< Name of an .INI file for registry settings. */ public: /** * @brief * This member function will set the .INI file name for the registry * manager to use instead of the system registry. * @param strINIFileName Full path to the .INI file. * @return */ static void AFX_CDECL SetINIFileName(LPCTSTR strINIFileName); /** * @brief * This member function will return the file name of the .INI file * used by the registry manager. * @return * A reference to the CString object that contains the .INI file path. */ static CString& AFX_CDECL GetINIFileName(); /** * @brief * Call this member function to write the specified int value into * the specified section of the application's registry or .INI file. * @param lpszSection Pointer to a NULL-terminated string that specifies * the section containing the entry. If the section * does not exist, then it is created. The name of the * section is case independent. The string may be * any combination of uppercase and lowercase letters. * @param lpszEntry Pointer to a NULL-terminated string that contains * the entry into which the value is to be written. If * the entry does not exist in the specified section, * then it is created. * @param nValue Contains the value to be written. * @return * Nonzero if successful, otherwise zero. If the user does not have * access rights to modify the system registry, then this function * will return -1. */ BOOL WriteProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue); /** * @brief * Call this member function to write the specified binary value into * the specified section of the application's registry or .INI file. * @param lpszSection Pointer to a NULL-terminated string that specifies * the section containing the entry. If the section * does not exist, then it is created. The name of the * section is case independent. The string may be * any combination of uppercase and lowercase letters. * @param lpszEntry Pointer to a NULL-terminated string that contains * the entry into which the value is to be written. If * the entry does not exist in the specified section, * then it is created. * @param pData Contains the value to be written. * @param nBytes Contains the size of the value to be written. * @return * Nonzero if successful, otherwise zero. If the user does not have * access rights to modify the system registry, then this function * will return -1. */ BOOL WriteProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPBYTE pData, UINT nBytes); /** * @brief * Call this member function to write the specified string value into * the specified section of the application's registry or .INI file. * @param lpszSection Pointer to a NULL-terminated string that specifies * the section containing the entry. If the section * does not exist, then it is created. The name of the * section is case independent. The string may be * any combination of uppercase and lowercase letters. * @param lpszEntry Pointer to a NULL-terminated string that contains * the entry into which the value is to be written. If * the entry does not exist in the specified section, * then it is created. * @param lpszValue Contains the value to be written. * @return * Nonzero if successful, otherwise zero. If the user does not have * access rights to modify the system registry, then this function * will return -1. */ BOOL WriteProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue); /** * @brief * Call this member function to write the specified CPoint value into * the specified section of the application's registry or .INI file. * @param lpszSection Pointer to a NULL-terminated string that specifies * the section containing the entry. If the section * does not exist, then it is created. The name of the * section is case independent. The string may be * any combination of uppercase and lowercase letters. * @param lpszEntry Pointer to a NULL-terminated string that contains * the entry into which the value is to be written. If * the entry does not exist in the specified section, * then it is created. * @param pValue Contains the value to be written. * @return * Nonzero if successful, otherwise zero. If the user does not have * access rights to modify the system registry, then this function * will return -1. */ BOOL WriteProfilePoint(LPCTSTR lpszSection, LPCTSTR lpszEntry, CPoint* pValue); /** * @brief * Call this member function to write the specified CRect value into * the specified section of the application's registry or .INI file. * @param lpszSection Pointer to a NULL-terminated string that specifies * the section containing the entry. If the section * does not exist, then it is created. The name of the * section is case independent. The string may be * any combination of uppercase and lowercase letters. * @param lpszEntry Pointer to a NULL-terminated string that contains * the entry into which the value is to be written. If * the entry does not exist in the specified section, * then it is created. * @param pValue Contains the value to be written. * @return * Nonzero if successful, otherwise zero. If the user does not have * access rights to modify the system registry, then this function * will return -1. */ BOOL WriteProfileRect(LPCTSTR lpszSection, LPCTSTR lpszEntry, CRect* pValue); /** * @brief * Call this member function to write the specified CSize value into * the specified section of the application's registry or .INI file. * @param lpszSection Pointer to a NULL-terminated string that specifies * the section containing the entry. If the section * does not exist, then it is created. The name of the * section is case independent. The string may be * any combination of uppercase and lowercase letters. * @param lpszEntry Pointer to a NULL-terminated string that contains * the entry into which the value is to be written. If * the entry does not exist in the specified section, * then it is created. * @param pValue Contains the value to be written. * @return * Nonzero if successful, otherwise zero. If the user does not have * access rights to modify the system registry, then this function * will return -1. */ BOOL WriteProfileSize(LPCTSTR lpszSection, LPCTSTR lpszEntry, CSize* pValue); /** * @brief * Call this member function to write the specified double/float value * into the specified section of the application's registry or .INI file. * @param lpszSection Pointer to a NULL-terminated string that specifies * the section containing the entry. If the section * does not exist, then it is created. The name of the * section is case independent. The string may be * any combination of uppercase and lowercase letters. * @param lpszEntry Pointer to a NULL-terminated string that contains * the entry into which the value is to be written. If * the entry does not exist in the specified section, * then it is created. * @param pValue Contains the value to be written. * @return * Nonzero if successful, otherwise zero. If the user does not have * access rights to modify the system registry, then this function * will return -1. */ BOOL WriteProfileDouble(LPCTSTR lpszSection, LPCTSTR lpszEntry, double* pValue); /** * @brief * Call this member function to write the specified DWORD value into * the specified section of the application's registry or .INI file. * @param lpszSection Pointer to a NULL-terminated string that specifies * the section containing the entry. If the section * does not exist, then it is created. The name of the * section is case independent. The string may be * any combination of uppercase and lowercase letters. * @param lpszEntry Pointer to a NULL-terminated string that contains * the entry into which the value is to be written. If * the entry does not exist in the specified section, * then it is created. * @param pValue Contains the value to be written. * @details * Since COLORREF is a typedef for a DWORD, WriteProfileColor calls * this function. * @return * Nonzero if successful, otherwise zero. If the user does not have * access rights to modify the system registry, then this function * will return -1. */ BOOL WriteProfileDword(LPCTSTR lpszSection, LPCTSTR lpszEntry, DWORD* pValue); /** * @brief * Call this member function to write the specified COLORREF value * into the specified section of the application's registry or .INI file. * @param lpszSection Pointer to a NULL-terminated string that specifies * the section containing the entry. If the section * does not exist, then it is created. The name of the * section is case independent. The string may be * any combination of uppercase and lowercase letters. * @param lpszEntry Pointer to a NULL-terminated string that contains * the entry into which the value is to be written. If * the entry does not exist in the specified section, * then it is created. * @param pValue Contains the value to be written. * @details * Since COLORREF is a typedef for a DWORD, this function only * calls WriteProfileDword. * @return * Nonzero if successful, otherwise zero. If the user does not have * access rights to modify the system registry, then this function * will return -1. */ BOOL WriteProfileColor(LPCTSTR lpszSection, LPCTSTR lpszEntry, COLORREF* pValue); /** * @brief * Call this member function to retrieve the value of an integer from * an entry within a specified section of the application's registry or * .INI file. * @param lpszSection Pointer to a NULL-terminated string that specifies the * section containing the entry. * @param lpszEntry Pointer to a NULL-terminated string that contains the * entry whose value is to be retrieved. * @param nDefault Specifies the default value to return if the framework * cannot find the entry. This value can be an unsigned * value in the range 0 through 65, 535 or a signed value * in the range -32, 768 through 32, 767. * @details * This member function supports hexadecimal notation for the value in * the .INI file. When you retrieve a signed integer, you should cast * the value into an int. * @return * The integer value of the string that follows the specified entry if * the function is successful, the value of the 'nDefault' parameter * if the function does not find the entry, or zero if the value that * corresponds to the specified entry is not an integer. */ UINT GetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault); /** * @brief * Call this member function to retrieve the binary value from an entry * within a specified section of the application's registry or .INI file. * @param lpszSection Pointer to a NULL-terminated string that specifies the * section containing the entry. * @param lpszEntry Pointer to a NULL-terminated string that contains the * entry whose value is to be retrieved. * @param pBytes Address of a UINT to receive the size of 'ppData'. * @param ppData Address of a LPBYTE data member to receive the value. * It is the caller's responsibility to free the memory * allocated for 'ppData'. * Example: * An example of this would be: *
	 * void CXTDockWindow::LoadBarSize(CString strSection)
	 * {
	 *    // restore the previous bar style.
	 *    if (::IsWindow(m_hWnd))
	 *    {
	 *       UINT nSize = 0;
	 *       LPBYTE pbtData = 0;
	 *       CString string;
	 *       string.LoadString(XTP_IDS_REG_BARPOS);
	 *
	 *       CXTPRegistryManager regManager;
	 *       if (regManager.GetProfileBinary(strSection,
	 *          string, &pbtData, &nSize))
	 *       {
	 *          XT_BARPLACEMENT* pBP =
	 *          reinterpret_cast(pbtData);
	 *
	 *          SetBarPlacement(pBP);
	 *          delete [] pbtData;
	 *       }
	 *    }
	 * }
* @return * Nonzero if successful, otherwise zero. */ BOOL GetProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, BYTE** ppData, UINT* pBytes); /** * @brief * Call this member function to retrieve the string associated with * an entry within the specified section in the application's registry * or .INI file. The return value is the string from the application's * .INI file, or 'lpszDefault' if the string cannot be found. The maximum * string length supported by the framework is _MAX_PATH. If 'lpszDefault' * is NULL, then the return value is an empty string. * @param lpszSection Pointer to a NULL-terminated string that specifies the * section containing the entry. * @param lpszEntry Pointer to a NULL-terminated string that contains the * entry whose value is to be retrieved. * @param lpszDefault Pointer to the default string value for the given entry * if the entry cannot be found in the initialization file. * @return * The string associated with an entry within the specified section in the * application's registry or .INI file. The return value is the string from the * application's .INI file, or 'lpszDefault' if the string cannot be found. */ CString GetProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault); /** * @brief * Call this member function to retrieve the value of a CPoint from * an entry within a specified section of the application's registry or * .INI file. Retrieves the CPoint value that follows the specified entry * if the function is successful. * @param lpszSection Pointer to a NULL-terminated string that specifies the * section containing the entry. * @param lpszEntry Pointer to a NULL-terminated string that contains the * entry whose value is to be retrieved. * @param ptResult Pointer to the variable to be filled from the registry. * @return * TRUE if successful, otherwise FALSE. */ BOOL GetProfilePoint(LPCTSTR lpszSection, LPCTSTR lpszEntry, CPoint* ptResult); /** * @brief * Call this member function to retrieve the value of a CRect from * an entry within a specified section of the application's registry or * .INI file. Retrieves the CRect value that follows the specified entry * if the function is successful. * @param lpszSection Pointer to a NULL-terminated string that specifies the * section containing the entry. * @param lpszEntry Pointer to a NULL-terminated string that contains the * entry whose value is to be retrieved. * @param rcResult Pointer to the variable to be filled from the registry. * @return * TRUE if successful, otherwise FALSE. */ BOOL GetProfileRect(LPCTSTR lpszSection, LPCTSTR lpszEntry, CRect* rcResult); /** * @brief * Call this member function to retrieve the value of a CSize from * an entry within a specified section of the application's registry or * .INI file. Retrieves the CSize value that follows the specified entry * if the function is successful. * @param lpszSection Pointer to a NULL-terminated string that specifies the * section containing the entry. * @param lpszEntry Pointer to a NULL-terminated string that contains the * entry whose value is to be retrieved. * @param rcResult Pointer to the variable to be filled from the registry. * @return * TRUE if successful, otherwise FALSE. */ BOOL GetProfileSize(LPCTSTR lpszSection, LPCTSTR lpszEntry, CSize* rcResult); /** * @brief * Call this member function to retrieve the value of a double from * an entry within a specified section of the application's registry or * .INI file. Retrieves the value that follows the specified entry if the * function is successful. * @param lpszSection Pointer to a NULL-terminated string that specifies the * section containing the entry. * @param lpszEntry Pointer to a NULL-terminated string that contains the * entry whose value is to be retrieved. * @param rcResult Pointer to the variable to be filled from the registry. * @return * TRUE if successful, otherwise FALSE. */ BOOL GetProfileDouble(LPCTSTR lpszSection, LPCTSTR lpszEntry, double* rcResult); /** * @brief * Call this member function to retrieve the value of a DWORD from * an entry within a specified section of the application's registry or * .INI file. Retrieves the value that follows the specified entry if the * function is successful. * @param lpszSection Pointer to a NULL-terminated string that specifies the * section containing the entry. * @param lpszEntry Pointer to a NULL-terminated string that contains the * entry whose value is to be retrieved. * @param rcResult Pointer to the variable to be filled from the registry. * @details * Since COLORREF is a typedef for a DWORD, GetProfileColor calls * this function. * @return * TRUE if successful, otherwise FALSE. */ BOOL GetProfileDword(LPCTSTR lpszSection, LPCTSTR lpszEntry, DWORD* rcResult); /** * @brief * Call this member function to retrieve the value of a COLORREF from * an entry within a specified section of the application's registry or * .INI file. Retrieves the value that follows the specified entry if the * function is successful. * @param lpszSection Pointer to a NULL-terminated string that specifies the * section containing the entry. * @param lpszEntry Pointer to a NULL-terminated string that contains the * entry whose value is to be retrieved. * @param rcResult Pointer to the variable to be filled from the registry. * @details * Since COLORREF is a typedef for a DWORD, this function only * calls GetProfileDword. * @return * TRUE if successful, otherwise FALSE. */ BOOL GetProfileColor(LPCTSTR lpszSection, LPCTSTR lpszEntry, COLORREF* rcResult); /** * @brief * Call this function to fill a CMap (CString to DWORD) with the * names and value types under a specific registry section. * @param lpszSection Pointer to a NULL-terminated string that specifies the * section containing the entry. * @param mapItems A reference to a CMap, which will contain the items in the * registry under this section. Can be NULL. Does not include * subsection names. * @param arrayNames A reference to a CStringArray which, if not NULL, will * contain an array of names in the order they were read from * the registry. * @details * The function also takes a pointer to a CStringArray because CMap * is a sorted container. Therefore, it is not possible to get the * items in the order they appeared from it. * * Pass NULL for the CStringArray* if you don't want to use it, and * NULL into the CMap if you only require a list of the item names. * * The DWORD values returned are as follows: * * REG_BINARY: Binary data in any form. * REG_DWORD: A 32-bit number. * REG_DWORD_LITTLE_ENDIAN: A 32-bit number in little-endian format. * This is equivalent to REG_DWORD. In * little-endian format, a multi-byte value is * stored in memory from the lowest byte * (the "little end") to the highest byte. For * example, the value 0x12345678 is stored * as (0x78 0x56 0x34 0x12) in little-endian * format. Windows NT/Windows 2000, Windows 95, * and Windows 98 are designed to run on * little-endian computer architectures. A user * may connect to computers that have * big-endian architectures, such as some UNIX * systems. * REG_DWORD_BIG_ENDIAN: A 32-bit number in big-endian format. In * big-endian format, a multi-byte value is stored * in memory from the highest byte (the "big end") * to the lowest byte. For example, the value * 0x12345678 is stored as (0x12 0x34 0x56 0x78) * in big-endian format. * REG_EXPAND_SZ: A NULL-terminated string that contains unexpanded * references to environment variables (for example, * "\%PATH\%"). It will be either a Unicode or ANSI * string depending on whether you use the Unicode or ANSI * functions. To expand the environment variable * references, use the ExpandEnvironmentStrings function. * REG_LINK: A Unicode symbolic link. Used internally. * Applications should not use this type. * REG_MULTI_SZ: An array of NULL-terminated strings, * terminated by two null characters. * REG_NONE: No defined value type. * REG_QWORD: A 64-bit number. * REG_QWORD_LITTLE_ENDIAN: A 64-bit number in little-endian format. * This is equivalent to REG_QWORD. * REG_RESOURCE_LIST: A device-driver resource list. * REG_SZ: A NULL-terminated string. It will be either a Unicode or * ANSI string, depending on whether or not you use the Unicode * or ANSI functions. * @return * Zero if the section is empty. Otherwise it returns the number of values present. */ int EnumValues(LPCTSTR lpszSection, CMap* mapItems, CStringArray* arrayNames); /** * @brief * Call this function to fill a CStringArray with the names of keys below * the key specified. The return value indicates the number of keys found * by the function, which can be used to check if any were found. * CStringArray::GetSize will give the same result if needed, assuming * the array is zeroed when it goes in. The function does not zero it, * leaving the possibility of getting a list of subkeys of more than one * key for whatever reason. * @param lpszSection Pointer to a NULL-terminated string that specifies the * section containing the entry. * @param arrayKeys A reference to a CStringArray, which will contain the keys * in the registry under the above key. * @return A number of enumerated keys. */ int EnumKeys(LPCTSTR lpszSection, CStringArray& arrayKeys); /** * @brief * Call this function to delete a registry key and all of its contents. * @param lpszSection Pointer to a NULL-terminated string that specifies the * section containing the entry. * @param lpszKey Pointer to a NULL-terminated string that specifies the * subkey to delete. * @return * true if successful, otherwise false. */ bool DeleteKey(LPCTSTR lpszSection, LPCTSTR lpszKey); /** * @brief * Call this function to delete a registry value. * @param lpszSection Pointer to a NULL-terminated string that specifies the * section containing the entry. * @param lpszKey Pointer to a NULL-terminated string that specifies the * value to delete. * @return * true if successful, otherwise false. */ bool DeleteValue(LPCTSTR lpszSection, LPCTSTR lpszKey); /** * @brief * Call this member function to retrieve that last error (if any) that * was returned by Windows when accessing the system registry. * @return * If the last operation was successful, then the return value is ERROR_SUCCESS. * Otherwise a nonzero error code is returned as defined in Winerror.h. You * can call GetErrorMessage() to get a generic description of the error. */ LONG GetErrorCode() const; /** * @brief * Call this member function to retrieve the description text for the * last error (if any) that occurred while accessing the system registry. * @return * A CString object that contains the error message that describes the * last error that occurred while accessing the system registry. */ CString GetErrorMessage() const; protected: /** * @brief * Called to get a handle to the CWinApp's registry key. * @param samDesired An access mask that specifies the desired access rights * to the key. * @return * An HKEY handle to the application's registry key. */ virtual HKEY GetAppRegistryKey(REGSAM samDesired = KEY_WRITE | KEY_READ); /** * @brief * Called to get a handle to the specified section from the application's * registry key. * @param lpszSection Section to get the key for. * @param samDesired An access mask that specifies the desired access rights * to the key. * @return * An HKEY handle to the specified section from the application's registry key. */ virtual HKEY GetSectionKey(LPCTSTR lpszSection, REGSAM samDesired = KEY_WRITE | KEY_READ); private: BOOL GetProfileInfo(); LONG RecurseDeleteKey(HKEY hKey, LPCTSTR lpszKey); HKEY GetFullPathKey(LPCTSTR lpszSection) const; HKEY RecurseOpenKey(LPCTSTR lpszSection, REGSAM samDesired); LONG m_lResult; }; ////////////////////////////////////////////////////////////////////// /** @cond */ AFX_INLINE void CXTPRegistryManager::SetINIFileName(LPCTSTR strINIFileName) { m_strINIFileName = strINIFileName; } AFX_INLINE CString& CXTPRegistryManager::GetINIFileName() { return m_strINIFileName; } AFX_INLINE LONG CXTPRegistryManager::GetErrorCode() const { return m_lResult; } /** @endcond */ # include "Common/Base/Diagnostic/XTPEnableNoisyWarnings.h" /** @cond */ #endif // __XTPREGISTRYMANAGER_H__ /** @endcond */