### Open Registry Key and Get Values (C++) Source: https://github.com/giovannidicanio/winreg/blob/master/README.md Demonstrates opening a registry key and retrieving DWORD and string values using the RegKey class. Exceptions are thrown on error. ```cpp RegKey key{ HKEY_CURRENT_USER, L"SOFTWARE\\SomeKey" }; DWORD dw = key.GetDwordValue (L"SomeDwordValue"); wstring s = key.GetStringValue(L"SomeStringValue"); ``` -------------------------------- ### Manage Application Settings with WinReg C++ Source: https://context7.com/giovannidicanio/winreg/llms.txt This example demonstrates loading and saving application settings to the Windows Registry using WinReg. It includes handling missing registry keys and values by providing default settings. Use this pattern for persistent application configuration. ```cpp #include "WinReg.hpp" #include #include #include using namespace winreg; struct AppSettings { std::wstring username; std::wstring theme; DWORD windowWidth; DWORD windowHeight; bool darkMode; std::vector recentFiles; }; class SettingsManager { public: SettingsManager() : m_keyPath(L"SOFTWARE\MyCompany\MyApplication") {} AppSettings Load() { AppSettings settings; RegKey key; if (!key.TryOpen(HKEY_CURRENT_USER, m_keyPath, KEY_READ)) { // Return defaults if key doesn't exist return GetDefaults(); } // Load with fallback to defaults settings.username = TryGetString(key, L"Username", L"User"); settings.theme = TryGetString(key, L"Theme", L"Modern"); settings.windowWidth = TryGetDword(key, L"WindowWidth", 1280); settings.windowHeight = TryGetDword(key, L"WindowHeight", 720); settings.darkMode = TryGetDword(key, L"DarkMode", 0) != 0; auto multiResult = key.TryGetMultiStringValue(L"RecentFiles"); if (multiResult.IsValid()) { settings.recentFiles = multiResult.GetValue(); } return settings; } void Save(const AppSettings& settings) { RegKey key{ HKEY_CURRENT_USER, m_keyPath }; key.SetStringValue(L"Username", settings.username); key.SetStringValue(L"Theme", settings.theme); key.SetDwordValue(L"WindowWidth", settings.windowWidth); key.SetDwordValue(L"WindowHeight", settings.windowHeight); key.SetDwordValue(L"DarkMode", settings.darkMode ? 1 : 0); key.SetMultiStringValue(L"RecentFiles", settings.recentFiles); } private: std::wstring m_keyPath; AppSettings GetDefaults() { return { L"User", L"Modern", 1280, 720, false, {} }; } std::wstring TryGetString(RegKey& key, const std::wstring& name, const std::wstring& defaultValue) { auto result = key.TryGetStringValue(name); return result.IsValid() ? result.GetValue() : defaultValue; } DWORD TryGetDword(RegKey& key, const std::wstring& name, DWORD defaultValue) { auto result = key.TryGetDwordValue(name); return result.IsValid() ? result.GetValue() : defaultValue; } }; int main() { try { SettingsManager manager; // Load settings (with defaults for missing values) AppSettings settings = manager.Load(); std::wcout << L"Current settings:\n"; std::wcout << L" Username: " << settings.username << L"\n"; std::wcout << L" Theme: " << settings.theme << L"\n"; std::wcout << L" Window: " << settings.windowWidth << L"x" << settings.windowHeight << L"\n"; std::wcout << L" Dark mode: " << (settings.darkMode ? L"Yes" : L"No") << L"\n"; // Modify and save settings.windowWidth = 1920; settings.windowHeight = 1080; settings.darkMode = true; settings.recentFiles = { L"file1.txt", L"file2.txt", L"file3.txt" }; manager.Save(settings); std::wcout << L"\nSettings saved successfully!\n"; } catch (const RegException& e) { std::wcout << L"Registry error: " << e.what() << L"\n"; return 1; } return 0; } ``` -------------------------------- ### Set and Get String Values (REG_SZ) Source: https://context7.com/giovannidicanio/winreg/llms.txt Use SetStringValue and GetStringValue to manage standard Unicode string values. Strings are handled as std::wstring and automatically null-terminated. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey key{ HKEY_CURRENT_USER, L"SOFTWARE\MyApplication" }; // Write string values key.SetStringValue(L"AppName", L"My Awesome Application"); key.SetStringValue(L"Version", L"2.5.1"); key.SetStringValue(L"InstallPath", L"C:\\Program Files\\MyApp"); // Read string values std::wstring appName = key.GetStringValue(L"AppName"); std::wstring version = key.GetStringValue(L"Version"); std::wstring path = key.GetStringValue(L"InstallPath"); std::wcout << L"Application: " << appName << L"\n"; std::wcout << L"Version: " << version << L"\n"; std::wcout << L"Install path: " << path << L"\n"; } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### Set and Get Expandable String Values (REG_EXPAND_SZ) Source: https://context7.com/giovannidicanio/winreg/llms.txt Use SetExpandStringValue and GetExpandStringValue for strings containing environment variables. Variables can be expanded upon retrieval using the ExpandStringOption. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey key{ HKEY_CURRENT_USER, L"SOFTWARE\MyApplication" }; // Write expandable string with environment variable references key.SetExpandStringValue(L"LogPath", L"%USERPROFILE%\AppData\Local\MyApp\Logs"); key.SetExpandStringValue(L"TempPath", L"%TEMP%\MyApp"); // Read without expanding (get the raw string with %VAR% intact) std::wstring logPathRaw = key.GetExpandStringValue( L"LogPath", RegKey::ExpandStringOption::DontExpand ); // Read with expansion (environment variables are substituted) std::wstring logPathExpanded = key.GetExpandStringValue( L"LogPath", RegKey::ExpandStringOption::Expand ); std::wcout << L"Raw path: " << logPathRaw << L"\n"; std::wcout << L"Expanded path: " << logPathExpanded << L"\n"; } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### Determine Registry Value Type with Winreg Source: https://context7.com/giovannidicanio/winreg/llms.txt Explains how to get the registry type (e.g., REG_DWORD, REG_SZ) of a specific value without reading its data. This is useful for type-safe data retrieval, allowing you to use the appropriate Get method based on the queried type. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey key{ HKEY_CURRENT_USER, L"SOFTWARE\MyApplication" }; // Set some values of different types key.SetDwordValue(L"IntValue", 42); key.SetStringValue(L"StringValue", L"Hello"); // Query value types DWORD intType = key.QueryValueType(L"IntValue"); DWORD strType = key.QueryValueType(L"StringValue"); std::wcout << L"IntValue type: " << RegKey::RegTypeToString(intType) << L"\n"; std::wcout << L"StringValue type: " << RegKey::RegTypeToString(strType) << L"\n"; // Type-safe reading based on discovered type if (intType == REG_DWORD) { DWORD value = key.GetDwordValue(L"IntValue"); std::wcout << L"Integer: " << value << L"\n"; } } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### Set and Get Multi-String Values (REG_MULTI_SZ) Source: https://context7.com/giovannidicanio/winreg/llms.txt Use SetMultiStringValue and GetMultiStringValue to manage arrays of strings. WinReg handles the internal double-null-termination using std::vector. ```cpp #include "WinReg.hpp" #include #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey key{ HKEY_CURRENT_USER, L"SOFTWARE\MyApplication" }; // Write multi-string value (array of strings) std::vector recentFiles = { L"C:\\Documents\\Report.docx", L"C:\\Projects\\main.cpp", L"C:\\Data\\config.json" }; key.SetMultiStringValue(L"RecentFiles", recentFiles); // Write supported languages std::vector languages = { L"English", L"Spanish", L"French", L"German" }; key.SetMultiStringValue(L"SupportedLanguages", languages); // Read multi-string values back std::vector readFiles = key.GetMultiStringValue(L"RecentFiles"); std::wcout << L"Recent files:\n"; for (const auto& file : readFiles) { std::wcout << L" - " << file << L"\n"; } } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### Try Get DWORD Value with RegExpected (C++) Source: https://github.com/giovannidicanio/winreg/blob/master/README.md Illustrates using RegKey::TryGetDwordValue to retrieve a DWORD registry value, returning a RegExpected. This allows checking for validity or accessing the value. ```cpp // // RegKey::TryGetDwordValue() returns a RegExpected; // the returned RegExpected contains a DWORD on success, // or a RegResult instance on error. // // 'res' is a RegExpected in this case: // const auto res = key.TryGetDwordValue(L"SomeDwordValue"); if (res.IsValid()) // or simply: if (res) { // // All right: Process the returned value ... ``` -------------------------------- ### Set and Get Binary Registry Values Source: https://context7.com/giovannidicanio/winreg/llms.txt Use SetBinaryValue to write raw binary data as std::vector or from a raw pointer and size. GetBinaryValue retrieves this data. Handles empty binary values correctly. ```cpp #include "WinReg.hpp" #include #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey key{ HKEY_CURRENT_USER, L"SOFTWARE\MyApplication" }; // Write binary data from vector std::vector configData = { 0x01, 0x02, 0x03, 0xAA, 0xBB, 0xCC, 0xFF }; key.SetBinaryValue(L"ConfigBlob", configData); // Write binary data from raw pointer and size BYTE rawData[] = { 0x10, 0x20, 0x30, 0x40 }; key.SetBinaryValue(L"RawConfig", rawData, sizeof(rawData)); // Read binary data std::vector readData = key.GetBinaryValue(L"ConfigBlob"); std::wcout << L"Binary data (" << readData.size() << L" bytes): "; for (BYTE b : readData) { std::wcout << std::hex << static_cast(b) << L" "; } std::wcout << L"\n"; // Handle empty binary values std::vector emptyData; key.SetBinaryValue(L"EmptyBlob", emptyData); std::vector readEmpty = key.GetBinaryValue(L"EmptyBlob"); std::wcout << L"Empty blob size: " << readEmpty.size() << L"\n"; } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### Open Registry Key (Two-Step Construction) (C++) Source: https://github.com/giovannidicanio/winreg/blob/master/README.md Shows how to open a registry key using a two-step construction process with the RegKey class. This method throws an exception on error. ```cpp RegKey key; key.Open(HKEY_CURRENT_USER, L"SOFTWARE\\SomeKey"); ``` -------------------------------- ### Create or Open Registry Key with WinReg Source: https://context7.com/giovannidicanio/winreg/llms.txt Use RegKey::Create to initialize a registry key with specific access rights and disposition tracking. Requires the WinReg.hpp header. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey key; // Simple create with default options key.Create(HKEY_CURRENT_USER, L"SOFTWARE\\MyApplication\\Settings"); // Create with specific access and get disposition info RegKey key2; DWORD disposition = 0; key2.Create( HKEY_CURRENT_USER, L"SOFTWARE\\MyApplication\\Config", KEY_READ | KEY_WRITE | KEY_WOW64_64KEY, // desiredAccess REG_OPTION_NON_VOLATILE, // options nullptr, // securityAttributes &disposition // disposition output ); if (disposition == REG_CREATED_NEW_KEY) { std::wcout << L"New key was created\n"; } else if (disposition == REG_OPENED_EXISTING_KEY) { std::wcout << L"Existing key was opened\n"; } } catch (const RegException& e) { std::wcout << L"Registry operation failed: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### Save and Load Registry Keys to/from Files Source: https://context7.com/giovannidicanio/winreg/llms.txt Saves a registry key to a file and loads it back. Requires backup/restore privileges and is typically used for registry backup operations. The saved file format is binary and only readable by LoadKey. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey key{ HKEY_CURRENT_USER, L"SOFTWARE\MyApplication" }; // Save the key to a file (requires SE_BACKUP_NAME privilege) // The file will be in a special binary format that only LoadKey can read key.SaveKey(L"C:\\Backups\MyAppRegistry.dat", nullptr); std::wcout << L"Registry key saved to file\n"; // Load a previously saved key (requires SE_RESTORE_NAME privilege) // This mounts the key as a subkey of HKEY_LOCAL_MACHINE or HKEY_USERS RegKey rootKey{ HKEY_LOCAL_MACHINE }; rootKey.LoadKey(L"TempLoadedKey", L"C:\\Backups\MyAppRegistry.dat"); std::wcout << L"Registry key loaded from file\n"; } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; std::wcout << L"Note: SaveKey/LoadKey require special privileges\n"; } return 0; } ``` -------------------------------- ### Construct RegKey: Open or Create Registry Key Source: https://context7.com/giovannidicanio/winreg/llms.txt Use the RegKey constructor for a one-step process to open an existing registry key or create a new one. It supports default or explicit access rights and automatically closes the key via RAII. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; int main() { // One-step construction: opens existing key or creates new one // Uses default access: KEY_READ | KEY_WRITE | KEY_WOW64_64KEY RegKey key{ HKEY_CURRENT_USER, L"SOFTWARE\MyApplication" }; // Constructor with explicit access rights (read-only access) RegKey readOnlyKey{ HKEY_LOCAL_MACHINE, L"SOFTWARE\Microsoft", KEY_READ }; // Check if key is valid if (key.IsValid()) { std::wcout << L"Registry key opened successfully\n"; } // Key is automatically closed when it goes out of scope (RAII) return 0; } ``` -------------------------------- ### Enumerate Registry Values with Structured Bindings (C++) Source: https://github.com/giovannidicanio/winreg/blob/master/README.md Shows how to use C++17 structured bindings with RegKey::EnumValues for a more concise way to iterate through registry values, accessing name and type directly. ```cpp auto values = key.EnumValues(); for (const auto & [valueName, valueType] : values) { // // Use valueName and valueType // ... } ``` -------------------------------- ### Open Registry Key with Error Checking (C++) Source: https://github.com/giovannidicanio/winreg/blob/master/README.md Illustrates opening a registry key while checking the return code for errors using the RegKey::TryOpen method. The RegResult object provides details on failure. ```cpp RegKey key; RegResult result = key.TryOpen(HKEY_CURRENT_USER, L"SOFTWARE\\SomeKey"); if (! result) { // // Open failed. // // You can invoke the RegResult::Code and RegResult::ErrorMessage methods // for further details. // ... } ``` -------------------------------- ### RegKey::Open: Open Existing Registry Key Source: https://context7.com/giovannidicanio/winreg/llms.txt Open an existing registry key using the RegKey::Open method for two-step construction. This method throws RegException on failure and supports specific access rights. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegException; int main() { RegKey key; try { // Open with default access (KEY_READ | KEY_WRITE | KEY_WOW64_64KEY) key.Open(HKEY_CURRENT_USER, L"SOFTWARE\MyApplication"); // Or open with specific access rights RegKey readKey; readKey.Open(HKEY_LOCAL_MACHINE, L"SOFTWARE\Microsoft\Windows\CurrentVersion", KEY_READ); std::wcout << L"Keys opened successfully\n"; } catch (const RegException& e) { std::wcout << L"Failed to open key: " << e.what() << L"\n"; std::wcout << L"Error code: " << e.code() << L"\n"; } return 0; } ``` -------------------------------- ### Attach, Detach, and Close HKEYs with WinReg Source: https://context7.com/giovannidicanio/winreg/llms.txt Demonstrates attaching an existing HKEY to a RegKey for RAII management, detaching it to transfer ownership back, and explicitly closing handles. Use Attach to let RegKey manage an existing handle and Detach to regain manual control. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; int main() { // Get raw HKEY from another API HKEY hKeyFromApi = nullptr; LSTATUS result = ::RegOpenKeyExW( HKEY_CURRENT_USER, L"SOFTWARE", 0, KEY_READ, &hKeyFromApi ); if (result == ERROR_SUCCESS) { // Attach existing HKEY to RegKey for RAII management RegKey key; key.Attach(hKeyFromApi); // RegKey now owns the handle // Use the key normally auto subkeys = key.EnumSubKeys(); std::wcout << L"Found " << subkeys.size() << L" subkeys\n"; // Detach to transfer ownership back to caller HKEY detachedKey = key.Detach(); // key is now empty, caller must close detachedKey manually ::RegCloseKey(detachedKey); } // Check validity and close explicitly RegKey key2{ HKEY_CURRENT_USER, L"SOFTWARE\MyApplication" }; if (key2.IsValid()) { std::wcout << L"Key is valid\n"; // Get raw handle for use with other APIs HKEY rawHandle = key2.Get(); // Use rawHandle with other Windows APIs... // Explicitly close (not usually needed due to RAII) key2.Close(); } // Check if handle is predefined (like HKEY_CURRENT_USER) RegKey predefinedKey{ HKEY_LOCAL_MACHINE }; if (predefinedKey.IsPredefined()) { std::wcout << L"This is a predefined key\n"; } return 0; } ``` -------------------------------- ### Enumerate Registry Values with Winreg Source: https://context7.com/giovannidicanio/winreg/llms.txt Demonstrates how to enumerate all values under a registry key, retrieving both their names and types. Use RegKey::RegTypeToString() to convert type IDs to human-readable strings. ```cpp #include "WinReg.hpp" #include #include #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey key; key.Open(HKEY_LOCAL_MACHINE, L"SOFTWARE\Microsoft\Windows\CurrentVersion", KEY_READ); // Get all values (name and type pairs) std::vector> values = key.EnumValues(); std::wcout << L"Values:\n"; for (const auto& [valueName, valueType] : values) { std::wcout << L" [" << valueName << L"] = " << RegKey::RegTypeToString(valueType) << L"\n"; } std::wcout << L"\nTotal: " << values.size() << L" values\n"; } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### RegKey::Open Source: https://context7.com/giovannidicanio/winreg/llms.txt Opens an existing registry key using exception-based error handling. ```APIDOC ## RegKey::Open ### Description Opens an existing registry key with specified access rights. Throws RegException on failure. ### Parameters #### Path Parameters - **hKey** (HKEY) - Required - Parent key handle. - **subKey** (std::wstring) - Required - Path to the subkey. - **access** (REGSAM) - Optional - Access rights. ``` -------------------------------- ### Enumerate Registry Values (C++) Source: https://github.com/giovannidicanio/winreg/blob/master/README.md Demonstrates iterating through all values under a registry key using RegKey::EnumValues. Each value is represented by a pair of name (wstring) and type (DWORD). ```cpp auto values = key.EnumValues(); for (const auto & v : values) { // // Process current value: // // - v.first (wstring) is the value name // - v.second (DWORD) is the value type // ... } ``` -------------------------------- ### Query Registry Key Information with Winreg Source: https://context7.com/giovannidicanio/winreg/llms.txt Shows how to retrieve metadata about a registry key, including the count of subkeys and values, and the last modification time. The LastWriteTime is provided as a FILETIME structure, which can be converted to a more readable format. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey key; key.Open(HKEY_LOCAL_MACHINE, L"SOFTWARE\Microsoft", KEY_READ); // Get key information RegKey::InfoKey info = key.QueryInfoKey(); std::wcout << L"Key information:\n"; std::wcout << L" Number of subkeys: " << info.NumberOfSubKeys << L"\n"; std::wcout << L" Number of values: " << info.NumberOfValues << L"\n"; // Convert FILETIME to readable format if needed SYSTEMTIME st; FileTimeToSystemTime(&info.LastWriteTime, &st); std::wcout << L" Last write: " << st.wYear << L"-" << st.wMonth << L"-" << st.wDay << L"\n"; } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### RegKey::TryOpen Source: https://context7.com/giovannidicanio/winreg/llms.txt Attempts to open a registry key and returns a result object instead of throwing an exception. ```APIDOC ## RegKey::TryOpen ### Description Attempts to open a registry key without throwing exceptions. Returns a RegResult object. ### Parameters #### Path Parameters - **hKey** (HKEY) - Required - Parent key handle. - **subKey** (std::wstring) - Required - Path to the subkey. ### Response #### Success Response (RegResult) - **result** (RegResult) - Object containing success status and error information. ``` -------------------------------- ### RegKey::Create Source: https://context7.com/giovannidicanio/winreg/llms.txt Creates a new registry key or opens an existing one with configurable access and security attributes. ```APIDOC ## RegKey::Create ### Description Creates a new registry key, or opens it if it already exists. Provides control over access rights, options, and security attributes. ### Method C++ Method ### Parameters - **hKey** (HKEY) - Required - Handle to an open registry key. - **subKey** (std::wstring) - Required - Name of the subkey to create or open. - **desiredAccess** (REGSAM) - Optional - Access mask for the key. - **options** (DWORD) - Optional - Registry options (e.g., REG_OPTION_NON_VOLATILE). - **securityAttributes** (LPSECURITY_ATTRIBUTES) - Optional - Security attributes for the key. - **disposition** (DWORD*) - Optional - Pointer to receive the disposition value (REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY). ``` -------------------------------- ### Handle Registry Method Results Source: https://github.com/giovannidicanio/winreg/blob/master/README.md Demonstrates checking the result of a registry operation using GetValue and GetError. ```cpp // // Use res.GetValue() to access the stored DWORD. // } else { // // The method has failed: // // The returned RegExpected contains a RegResult with an error code. // Use res.GetError() to access the RegResult object. // } ``` -------------------------------- ### RegKey Constructor Source: https://context7.com/giovannidicanio/winreg/llms.txt Constructs a RegKey object by opening or creating a registry key. ```APIDOC ## RegKey Constructor ### Description Initializes a RegKey object by opening an existing registry key or creating a new one if it does not exist. ### Parameters #### Path Parameters - **hKey** (HKEY) - Required - Parent key handle (e.g., HKEY_CURRENT_USER). - **subKey** (std::wstring) - Required - Path to the subkey. - **access** (REGSAM) - Optional - Access rights (defaults to KEY_READ | KEY_WRITE | KEY_WOW64_64KEY). ``` -------------------------------- ### Read and Write QWORD Registry Values Source: https://context7.com/giovannidicanio/winreg/llms.txt SetQwordValue and GetQwordValue manage 64-bit ULONGLONG registry data. Suitable for large integers or timestamps. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey key{ HKEY_CURRENT_USER, L"SOFTWARE\\MyApplication" }; // Write QWORD values ULONGLONG timestamp = 0xAABBCCDD11223344ULL; ULONGLONG largeCounter = 9876543210ULL; key.SetQwordValue(L"LastAccessTime", timestamp); key.SetQwordValue(L"TotalBytesProcessed", largeCounter); // Read QWORD values ULONGLONG readTimestamp = key.GetQwordValue(L"LastAccessTime"); ULONGLONG readCounter = key.GetQwordValue(L"TotalBytesProcessed"); std::wcout << L"Timestamp: 0x" << std::hex << readTimestamp << L"\n"; std::wcout << L"Bytes processed: " << std::dec << readCounter << L"\n"; } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### Check Registry Key/Value Existence with Winreg Source: https://context7.com/giovannidicanio/winreg/llms.txt Illustrates how to check if a specific registry value or subkey exists without causing exceptions. The TryContainsValue method provides a non-throwing way to check for value existence and retrieve its data if present. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey key{ HKEY_CURRENT_USER, L"SOFTWARE\MyApplication" }; // Check if a value exists before reading if (key.ContainsValue(L"WindowWidth")) { DWORD width = key.GetDwordValue(L"WindowWidth"); std::wcout << L"Window width: " << width << L"\n"; } else { std::wcout << L"WindowWidth value not found, using default\n"; } // Check if a subkey exists if (key.ContainsSubKey(L"Settings")) { std::wcout << L"Settings subkey exists\n"; RegKey settingsKey{ HKEY_CURRENT_USER, L"SOFTWARE\MyApplication\Settings" }; // Process settings... } // Non-throwing versions auto valueExists = key.TryContainsValue(L"SomeValue"); if (valueExists.IsValid() && valueExists.GetValue()) { std::wcout << L"Value exists\n"; } } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### QWORD Value Operations Source: https://context7.com/giovannidicanio/winreg/llms.txt Methods for setting and retrieving 64-bit QWORD registry values. ```APIDOC ## SetQwordValue / GetQwordValue ### Description Sets and retrieves 64-bit ULONGLONG values (REG_QWORD type) in the registry. ### Parameters - **name** (std::wstring) - Required - The name of the registry value. - **value** (ULONGLONG) - Required (for Set) - The 64-bit integer value to store. ``` -------------------------------- ### Copy Registry Subtree Source: https://context7.com/giovannidicanio/winreg/llms.txt Copies a source subkey and all its values and subkeys to a destination key. Useful for backing up or duplicating registry structures. Non-throwing versions are available. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegException; int main() { try { // Open source key RegKey sourceKey{ HKEY_CURRENT_USER, L"SOFTWARE\MyApplication\Settings" }; // Create/open destination key RegKey destKey{ HKEY_CURRENT_USER, L"SOFTWARE\MyApplication\SettingsBackup" }; // Copy entire subtree from source to destination sourceKey.CopyTree(L"", destKey); // Empty string copies from root of sourceKey std::wcout << L"Registry subtree copied successfully\n"; // Non-throwing version auto result = sourceKey.TryCopyTree(L"SubSection", destKey); if (result.Failed()) { std::wcout << L"Copy failed: " << result.ErrorMessage() << L"\n"; } } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### Enumerate Registry Subkeys Source: https://context7.com/giovannidicanio/winreg/llms.txt EnumSubKeys returns a vector of all subkey names under the current registry key. The non-throwing TryEnumSubKeys attempts enumeration and returns a valid result object on success. ```cpp #include "WinReg.hpp" #include #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey key; key.Open(HKEY_CURRENT_USER, L"SOFTWARE", KEY_READ); // Get all subkey names std::vector subkeys = key.EnumSubKeys(); std::wcout << L"Subkeys under HKCU\SOFTWARE:\n"; for (const auto& subkey : subkeys) { std::wcout << L" [" << subkey << L"]\n"; } std::wcout << L"\nTotal: " << subkeys.size() << L" subkeys\n"; // Non-throwing version auto result = key.TryEnumSubKeys(); if (result.IsValid()) { std::wcout << L"Found " << result.GetValue().size() << L" subkeys\n"; } } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### RegKey::TryOpen: Non-Throwing Open Source: https://context7.com/giovannidicanio/winreg/llms.txt Attempt to open a registry key without exceptions using RegKey::TryOpen. It returns a RegResult object for error-code based checking, providing the error code and message on failure. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegResult; int main() { RegKey key; RegResult result = key.TryOpen(HKEY_CURRENT_USER, L"SOFTWARE\MyApplication"); if (result.IsOk()) // or simply: if (result) { std::wcout << L"Key opened successfully\n"; } else { std::wcout << L"Failed to open key\n"; std::wcout << L"Error code: " << result.Code() << L"\n"; std::wcout << L"Error message: " << result.ErrorMessage() << L"\n"; } return 0; } ``` -------------------------------- ### Check for Registry Value Existence (C++) Source: https://github.com/giovannidicanio/winreg/blob/master/README.md Demonstrates checking if a registry key contains a specific value using the RegKey::ContainsValue method. ```cpp if (key.ContainsValue(L"Connie")) { // The key contains the value named "Connie" ... } ``` -------------------------------- ### Read and Write DWORD Registry Values Source: https://context7.com/giovannidicanio/winreg/llms.txt SetDwordValue and GetDwordValue handle 32-bit integer registry entries. These methods throw RegException on failure. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey key{ HKEY_CURRENT_USER, L"SOFTWARE\\MyApplication" }; // Write DWORD values key.SetDwordValue(L"WindowWidth", 1920); key.SetDwordValue(L"WindowHeight", 1080); key.SetDwordValue(L"Flags", 0x0000ABCD); // Read DWORD values DWORD width = key.GetDwordValue(L"WindowWidth"); DWORD height = key.GetDwordValue(L"WindowHeight"); DWORD flags = key.GetDwordValue(L"Flags"); std::wcout << L"Window size: " << width << L"x" << height << L"\n"; std::wcout << L"Flags: 0x" << std::hex << flags << L"\n"; } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### Connect to Remote Registry Source: https://context7.com/giovannidicanio/winreg/llms.txt Connects to the registry of a remote computer. Requires appropriate network permissions and the Remote Registry service running on the target machine. Operations can then be performed as if the registry were local. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey remoteKey; // Connect to remote computer's registry // Requires Remote Registry service running on target machine remoteKey.ConnectRegistry(L"\\REMOTE-PC-NAME", HKEY_LOCAL_MACHINE); // Now use the key as if it were local remoteKey.Open(HKEY_LOCAL_MACHINE, L"SOFTWARE\Microsoft\Windows\CurrentVersion", KEY_READ); std::wstring productName = remoteKey.GetStringValue(L"ProductName"); std::wcout << L"Remote Windows version: " << productName << L"\n"; } catch (const RegException& e) { std::wcout << L"Error connecting to remote registry: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### Delete Registry Values, Keys, and Subtrees Source: https://context7.com/giovannidicanio/winreg/llms.txt Removes individual values, empty subkeys, or entire subtrees. Use with caution as these operations are not reversible. Non-throwing versions are available. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegException; int main() { try { RegKey key{ HKEY_CURRENT_USER, L"SOFTWARE\MyApplication" }; // Create some test data key.SetDwordValue(L"TempValue", 123); key.Create(HKEY_CURRENT_USER, L"SOFTWARE\MyApplication\TempSubKey"); // Delete a single value key.DeleteValue(L"TempValue"); std::wcout << L"Value deleted\n"; // Delete an empty subkey (must specify access rights) key.DeleteKey(L"TempSubKey", KEY_WOW64_64KEY); std::wcout << L"Subkey deleted\n"; // DeleteTree removes a subkey and ALL its contents recursively // WARNING: This is a destructive operation! // key.DeleteTree(L"SubKeyToRemove"); // Non-throwing versions auto result = key.TryDeleteValue(L"NonExistentValue"); if (result.Failed()) { std::wcout << L"Delete failed (value may not exist): " << result.ErrorMessage() << L"\n"; } } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### Read Raw Binary Registry Data Source: https://context7.com/giovannidicanio/winreg/llms.txt Use GetRawValue to read binary data of any registry type, such as REG_RESOURCE_LIST. The non-throwing TryGetRawValue provides a safe way to attempt reading, returning a valid result object on success. ```cpp #include "WinReg.hpp" #include #include using winreg::RegKey; using winreg::RegException; int main() { try { // Open a system key that may contain special registry types RegKey key; key.Open(HKEY_LOCAL_MACHINE, L"HARDWARE\RESOURCEMAP\System Resources\Loader Reserved", KEY_READ); // GetRawValue can read any registry type as raw bytes // This is useful for REG_RESOURCE_LIST and similar types std::vector rawData = key.GetRawValue(L".Raw"); std::wcout << L"Raw data size: " << rawData.size() << L" bytes\n"; // Non-throwing version auto result = key.TryGetRawValue(L".Raw"); if (result.IsValid()) { const auto& data = result.GetValue(); std::wcout << L"Successfully read " << data.size() << L" bytes\n"; } } catch (const RegException& e) { std::wcout << L"Error: " << e.what() << L"\n"; } return 0; } ``` -------------------------------- ### DWORD Value Operations Source: https://context7.com/giovannidicanio/winreg/llms.txt Methods for setting and retrieving 32-bit DWORD registry values. ```APIDOC ## SetDwordValue / GetDwordValue ### Description Sets and retrieves 32-bit DWORD values (REG_DWORD type) in the registry. ### Parameters - **name** (std::wstring) - Required - The name of the registry value. - **value** (DWORD) - Required (for Set) - The 32-bit integer value to store. ``` -------------------------------- ### Perform Non-Throwing DWORD Registry Reads Source: https://context7.com/giovannidicanio/winreg/llms.txt TryGetDwordValue returns a RegExpected object, allowing for error handling without exceptions. Check validity using IsValid() before accessing the value. ```cpp #include "WinReg.hpp" #include using winreg::RegKey; using winreg::RegExpected; int main() { RegKey key; if (!key.TryOpen(HKEY_CURRENT_USER, L"SOFTWARE\\MyApplication")) { std::wcout << L"Failed to open key\n"; return 1; } // TryGetDwordValue returns RegExpected RegExpected result = key.TryGetDwordValue(L"WindowWidth"); if (result.IsValid()) // or simply: if (result) { DWORD width = result.GetValue(); std::wcout << L"Window width: " << width << L"\n"; } else { // Get error information auto error = result.GetError(); std::wcout << L"Failed to read value\n"; std::wcout << L"Error code: " << error.Code() << L"\n"; std::wcout << L"Error message: " << error.ErrorMessage() << L"\n"; } return 0; } ``` -------------------------------- ### TryGetDwordValue Source: https://context7.com/giovannidicanio/winreg/llms.txt Attempts to read a DWORD value without throwing exceptions, returning a result object. ```APIDOC ## TryGetDwordValue ### Description Attempts to read a DWORD value without throwing exceptions. Returns a RegExpected object containing the value or error information. ### Response - **RegExpected** - An object containing the value if successful, or error details if failed. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.