From f80d119614b2aefa06bdff5238daaf79fa7f1dc9 Mon Sep 17 00:00:00 2001 From: jules Date: Thu, 29 Mar 2012 17:43:49 +0100 Subject: [PATCH] Added a few methods to WindowsRegistry. --- modules/juce_core/misc/juce_WindowsRegistry.h | 28 +++++-- .../juce_core/native/juce_win32_Registry.cpp | 79 ++++++++++++++----- 2 files changed, 84 insertions(+), 23 deletions(-) diff --git a/modules/juce_core/misc/juce_WindowsRegistry.h b/modules/juce_core/misc/juce_WindowsRegistry.h index 8528f875ab..1674692837 100644 --- a/modules/juce_core/misc/juce_WindowsRegistry.h +++ b/modules/juce_core/misc/juce_WindowsRegistry.h @@ -37,20 +37,38 @@ class WindowsRegistry public: //============================================================================== /** Returns a string from the registry. - The path is a string for the entire path of a value in the registry, e.g. "HKEY_CURRENT_USER\Software\foo\bar" */ static String getValue (const String& regValuePath, const String& defaultValue = String::empty); + /** Reads a binary block from the registry. + The path is a string for the entire path of a value in the registry, + e.g. "HKEY_CURRENT_USER\Software\foo\bar" + @returns a DWORD indicating the type of the key. + */ + static uint32 getBinaryValue (const String& regValuePath, MemoryBlock& resultData); + /** Sets a registry value as a string. + This will take care of creating any groups needed to get to the given registry value. + */ + static bool setValue (const String& regValuePath, const String& value); + + /** Sets a registry value as a DWORD. + This will take care of creating any groups needed to get to the given registry value. + */ + static bool setValue (const String& regValuePath, uint32 value); + + /** Sets a registry value as a QWORD. + This will take care of creating any groups needed to get to the given registry value. + */ + static bool setValue (const String& regValuePath, uint64 value); - This will take care of creating any groups needed to get to the given - registry value. + /** Sets a registry value as a binary block. + This will take care of creating any groups needed to get to the given registry value. */ - static bool setValue (const String& regValuePath, - const String& value); + static bool setValue (const String& regValuePath, const MemoryBlock& value); /** Returns true if the given value exists in the registry. */ static bool valueExists (const String& regValuePath); diff --git a/modules/juce_core/native/juce_win32_Registry.cpp b/modules/juce_core/native/juce_win32_Registry.cpp index b315481bfc..432052aa00 100644 --- a/modules/juce_core/native/juce_win32_Registry.cpp +++ b/modules/juce_core/native/juce_win32_Registry.cpp @@ -60,6 +60,17 @@ struct RegistryKeyWrapper RegCloseKey (key); } + static bool setValue (const String& regValuePath, const DWORD type, + const void* data, size_t dataSize) + { + const RegistryKeyWrapper key (regValuePath, true); + + return key.key != 0 + && RegSetValueEx (key.key, key.wideCharValueName, 0, type, + reinterpret_cast (data), + (DWORD) dataSize) == ERROR_SUCCESS; + } + HKEY key; const wchar_t* wideCharValueName; String valueName; @@ -67,36 +78,66 @@ struct RegistryKeyWrapper JUCE_DECLARE_NON_COPYABLE (RegistryKeyWrapper); }; -String WindowsRegistry::getValue (const String& regValuePath, const String& defaultValue) +uint32 WindowsRegistry::getBinaryValue (const String& regValuePath, MemoryBlock& result) { const RegistryKeyWrapper key (regValuePath, false); if (key.key != 0) { - WCHAR buffer [2048]; - unsigned long bufferSize = sizeof (buffer); - DWORD type = REG_SZ; - - if (RegQueryValueEx (key.key, key.wideCharValueName, 0, &type, (LPBYTE) buffer, &bufferSize) == ERROR_SUCCESS) + for (unsigned long bufferSize = 1024; ; bufferSize *= 2) { - if (type == REG_SZ) - return buffer; - else if (type == REG_DWORD) - return String ((int) *(DWORD*) buffer); + result.setSize (bufferSize, false); + DWORD type = REG_NONE; + + const LONG err = RegQueryValueEx (key.key, key.wideCharValueName, 0, &type, + (LPBYTE) result.getData(), &bufferSize); + + if (err == ERROR_SUCCESS) + { + result.setSize (bufferSize, false); + return type; + } + + if (err != ERROR_MORE_DATA) + break; } } + return REG_NONE; +} + +String WindowsRegistry::getValue (const String& regValuePath, const String& defaultValue) +{ + MemoryBlock buffer; + switch (getBinaryValue (regValuePath, buffer)) + { + case REG_SZ: return static_cast (buffer.getData()); + case REG_DWORD: return String ((int) *reinterpret_cast (buffer.getData())); + default: break; + } + return defaultValue; } bool WindowsRegistry::setValue (const String& regValuePath, const String& value) { - const RegistryKeyWrapper key (regValuePath, true); + return RegistryKeyWrapper::setValue (regValuePath, REG_SZ, value.toWideCharPointer(), + CharPointer_UTF16::getBytesRequiredFor (value.getCharPointer())); +} - return key.key != 0 - && RegSetValueEx (key.key, key.wideCharValueName, 0, REG_SZ, - (const BYTE*) value.toWideCharPointer(), - (DWORD) CharPointer_UTF16::getBytesRequiredFor (value.getCharPointer())) == ERROR_SUCCESS; +bool WindowsRegistry::setValue (const String& regValuePath, const uint32 value) +{ + return RegistryKeyWrapper::setValue (regValuePath, REG_DWORD, &value, sizeof (value)); +} + +bool WindowsRegistry::setValue (const String& regValuePath, const uint64 value) +{ + return RegistryKeyWrapper::setValue (regValuePath, REG_QWORD, &value, sizeof (value)); +} + +bool WindowsRegistry::setValue (const String& regValuePath, const MemoryBlock& value) +{ + return RegistryKeyWrapper::setValue (regValuePath, REG_BINARY, value.getData(), value.getSize()); } bool WindowsRegistry::valueExists (const String& regValuePath) @@ -106,12 +147,14 @@ bool WindowsRegistry::valueExists (const String& regValuePath) if (key.key == 0) return false; - unsigned char buffer [2048]; + unsigned char buffer [512]; unsigned long bufferSize = sizeof (buffer); DWORD type = 0; - return RegQueryValueEx (key.key, key.wideCharValueName, - 0, &type, buffer, &bufferSize) == ERROR_SUCCESS; + const LONG result = RegQueryValueEx (key.key, key.wideCharValueName, + 0, &type, buffer, &bufferSize); + + return result == ERROR_SUCCESS || result == ERROR_MORE_DATA; } void WindowsRegistry::deleteValue (const String& regValuePath)