Commit 73faa990 authored by captainwong's avatar captainwong

update for unicode; re-format

parent e91a1a4a
...@@ -60,11 +60,11 @@ class RegKey; ...@@ -60,11 +60,11 @@ class RegKey;
// Wrappers for getting registry values // Wrappers for getting registry values
// //
DWORD RegGetDword (HKEY hKey, const std::wstring& subKey, const std::wstring& value); DWORD RegGetDword(HKEY hKey, const std::wstring& subKey, const std::wstring& value);
ULONGLONG RegGetQword (HKEY hKey, const std::wstring& subKey, const std::wstring& value); ULONGLONG RegGetQword(HKEY hKey, const std::wstring& subKey, const std::wstring& value);
std::wstring RegGetString (HKEY hKey, const std::wstring& subKey, const std::wstring& value); std::wstring RegGetString(HKEY hKey, const std::wstring& subKey, const std::wstring& value);
std::vector<std::wstring> RegGetMultiString (HKEY hKey, const std::wstring& subKey, const std::wstring& value); std::vector<std::wstring> RegGetMultiString(HKEY hKey, const std::wstring& subKey, const std::wstring& value);
std::vector<BYTE> RegGetBinary (HKEY hKey, const std::wstring& subKey, const std::wstring& value); std::vector<BYTE> RegGetBinary(HKEY hKey, const std::wstring& subKey, const std::wstring& value);
// //
...@@ -162,8 +162,7 @@ public: ...@@ -162,8 +162,7 @@ public:
// //
// Prevent self-move-assign // Prevent self-move-assign
// //
if ((this != &other) && (m_hKey != other.m_hKey)) if ((this != &other) && (m_hKey != other.m_hKey)) {
{
Close(); Close();
// //
...@@ -203,8 +202,7 @@ public: ...@@ -203,8 +202,7 @@ public:
// //
void Close() noexcept void Close() noexcept
{ {
if (IsValid()) if (IsValid()) {
{
::RegCloseKey(m_hKey); ::RegCloseKey(m_hKey);
m_hKey = nullptr; m_hKey = nullptr;
} }
...@@ -255,8 +253,7 @@ public: ...@@ -255,8 +253,7 @@ public:
// //
// Prevent self-attach // Prevent self-attach
// //
if (m_hKey != hKey) if (m_hKey != hKey) {
{
Close(); Close();
// //
...@@ -358,7 +355,7 @@ inline DWORD RegGetDword(HKEY hKey, const std::wstring& subKey, const std::wstri ...@@ -358,7 +355,7 @@ inline DWORD RegGetDword(HKEY hKey, const std::wstring& subKey, const std::wstri
// //
DWORD data{}; DWORD data{};
DWORD dataSize = sizeof(data); DWORD dataSize = sizeof(data);
LONG retCode = ::RegGetValue( LONG retCode = ::RegGetValueW(
hKey, hKey,
subKey.c_str(), subKey.c_str(),
value.c_str(), value.c_str(),
...@@ -366,8 +363,7 @@ inline DWORD RegGetDword(HKEY hKey, const std::wstring& subKey, const std::wstri ...@@ -366,8 +363,7 @@ inline DWORD RegGetDword(HKEY hKey, const std::wstring& subKey, const std::wstri
nullptr, nullptr,
&data, &data,
&dataSize); &dataSize);
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS) {
{
throw RegistryError{ "Cannot read DWORD from registry.", retCode }; throw RegistryError{ "Cannot read DWORD from registry.", retCode };
} }
...@@ -382,7 +378,7 @@ inline ULONGLONG RegGetQword(HKEY hKey, const std::wstring& subKey, const std::w ...@@ -382,7 +378,7 @@ inline ULONGLONG RegGetQword(HKEY hKey, const std::wstring& subKey, const std::w
// //
ULONGLONG data{}; ULONGLONG data{};
DWORD dataSize = sizeof(data); DWORD dataSize = sizeof(data);
LONG retCode = ::RegGetValue( LONG retCode = ::RegGetValueW(
hKey, hKey,
subKey.c_str(), subKey.c_str(),
value.c_str(), value.c_str(),
...@@ -390,8 +386,7 @@ inline ULONGLONG RegGetQword(HKEY hKey, const std::wstring& subKey, const std::w ...@@ -390,8 +386,7 @@ inline ULONGLONG RegGetQword(HKEY hKey, const std::wstring& subKey, const std::w
nullptr, nullptr,
&data, &data,
&dataSize); &dataSize);
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS) {
{
throw RegistryError{ "Cannot read QWORD from registry.", retCode }; throw RegistryError{ "Cannot read QWORD from registry.", retCode };
} }
...@@ -405,7 +400,7 @@ inline std::wstring RegGetString(HKEY hKey, const std::wstring& subKey, const st ...@@ -405,7 +400,7 @@ inline std::wstring RegGetString(HKEY hKey, const std::wstring& subKey, const st
// Request the size of the string, in bytes // Request the size of the string, in bytes
// //
DWORD dataSize{}; DWORD dataSize{};
LONG retCode = ::RegGetValue( LONG retCode = ::RegGetValueW(
hKey, hKey,
subKey.c_str(), subKey.c_str(),
value.c_str(), value.c_str(),
...@@ -413,8 +408,7 @@ inline std::wstring RegGetString(HKEY hKey, const std::wstring& subKey, const st ...@@ -413,8 +408,7 @@ inline std::wstring RegGetString(HKEY hKey, const std::wstring& subKey, const st
nullptr, nullptr,
nullptr, nullptr,
&dataSize); &dataSize);
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS) {
{
throw RegistryError{ "Cannot read string from registry", retCode }; throw RegistryError{ "Cannot read string from registry", retCode };
} }
...@@ -430,7 +424,7 @@ inline std::wstring RegGetString(HKEY hKey, const std::wstring& subKey, const st ...@@ -430,7 +424,7 @@ inline std::wstring RegGetString(HKEY hKey, const std::wstring& subKey, const st
// //
// Read the string from the registry into local wstring object // Read the string from the registry into local wstring object
// //
retCode = ::RegGetValue( retCode = ::RegGetValueW(
hKey, hKey,
subKey.c_str(), subKey.c_str(),
value.c_str(), value.c_str(),
...@@ -438,8 +432,7 @@ inline std::wstring RegGetString(HKEY hKey, const std::wstring& subKey, const st ...@@ -438,8 +432,7 @@ inline std::wstring RegGetString(HKEY hKey, const std::wstring& subKey, const st
nullptr, nullptr,
&data[0], &data[0],
&dataSize); &dataSize);
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS) {
{
throw RegistryError{ "Cannot read string from registry", retCode }; throw RegistryError{ "Cannot read string from registry", retCode };
} }
...@@ -464,7 +457,7 @@ RegGetMultiString(HKEY hKey, const std::wstring& subKey, const std::wstring& val ...@@ -464,7 +457,7 @@ RegGetMultiString(HKEY hKey, const std::wstring& subKey, const std::wstring& val
// Request the size of the multi-string, in bytes // Request the size of the multi-string, in bytes
// //
DWORD dataSize{}; DWORD dataSize{};
LONG retCode = ::RegGetValue( LONG retCode = ::RegGetValueW(
hKey, hKey,
subKey.c_str(), subKey.c_str(),
value.c_str(), value.c_str(),
...@@ -472,8 +465,7 @@ RegGetMultiString(HKEY hKey, const std::wstring& subKey, const std::wstring& val ...@@ -472,8 +465,7 @@ RegGetMultiString(HKEY hKey, const std::wstring& subKey, const std::wstring& val
nullptr, nullptr,
nullptr, nullptr,
&dataSize); &dataSize);
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS) {
{
throw RegistryError{ "Cannot read multi-string from registry", retCode }; throw RegistryError{ "Cannot read multi-string from registry", retCode };
} }
...@@ -488,7 +480,7 @@ RegGetMultiString(HKEY hKey, const std::wstring& subKey, const std::wstring& val ...@@ -488,7 +480,7 @@ RegGetMultiString(HKEY hKey, const std::wstring& subKey, const std::wstring& val
// //
// Read the multi-string from the registry into the vector object // Read the multi-string from the registry into the vector object
// //
retCode = ::RegGetValue( retCode = ::RegGetValueW(
hKey, hKey,
subKey.c_str(), subKey.c_str(),
value.c_str(), value.c_str(),
...@@ -496,8 +488,7 @@ RegGetMultiString(HKEY hKey, const std::wstring& subKey, const std::wstring& val ...@@ -496,8 +488,7 @@ RegGetMultiString(HKEY hKey, const std::wstring& subKey, const std::wstring& val
nullptr, nullptr,
&data[0], &data[0],
&dataSize); &dataSize);
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS) {
{
throw RegistryError{ "Cannot read multi-string from registry", retCode }; throw RegistryError{ "Cannot read multi-string from registry", retCode };
} }
...@@ -506,15 +497,14 @@ RegGetMultiString(HKEY hKey, const std::wstring& subKey, const std::wstring& val ...@@ -506,15 +497,14 @@ RegGetMultiString(HKEY hKey, const std::wstring& subKey, const std::wstring& val
// Note that the vector is a vector of wchar_ts, instead the size returned by GetRegValue() // Note that the vector is a vector of wchar_ts, instead the size returned by GetRegValue()
// is in bytes, so we have to scale from bytes to wchar_t count. // is in bytes, so we have to scale from bytes to wchar_t count.
// //
data.resize( dataSize / sizeof(wchar_t) ); data.resize(dataSize / sizeof(wchar_t));
// //
// Parse the double-NUL-terminated string into a vector<wstring> // Parse the double-NUL-terminated string into a vector<wstring>
// //
std::vector<std::wstring> result; std::vector<std::wstring> result;
const wchar_t* currStringPtr = &data[0]; const wchar_t* currStringPtr = &data[0];
while (*currStringPtr != L'\0') while (*currStringPtr != L'\0') {
{
// Current string is NUL-terminated, so get its length with wcslen() // Current string is NUL-terminated, so get its length with wcslen()
const size_t currStringLength = wcslen(currStringPtr); const size_t currStringLength = wcslen(currStringPtr);
...@@ -535,7 +525,7 @@ inline std::vector<BYTE> RegGetBinary(HKEY hKey, const std::wstring& subKey, con ...@@ -535,7 +525,7 @@ inline std::vector<BYTE> RegGetBinary(HKEY hKey, const std::wstring& subKey, con
// Request the size of the binary data, in bytes // Request the size of the binary data, in bytes
// //
DWORD dataSize{}; DWORD dataSize{};
LONG retCode = ::RegGetValue( LONG retCode = ::RegGetValueW(
hKey, hKey,
subKey.c_str(), subKey.c_str(),
value.c_str(), value.c_str(),
...@@ -543,8 +533,7 @@ inline std::vector<BYTE> RegGetBinary(HKEY hKey, const std::wstring& subKey, con ...@@ -543,8 +533,7 @@ inline std::vector<BYTE> RegGetBinary(HKEY hKey, const std::wstring& subKey, con
nullptr, nullptr,
nullptr, nullptr,
&dataSize); &dataSize);
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS) {
{
throw RegistryError{ "Cannot read binary data from registry", retCode }; throw RegistryError{ "Cannot read binary data from registry", retCode };
} }
...@@ -557,7 +546,7 @@ inline std::vector<BYTE> RegGetBinary(HKEY hKey, const std::wstring& subKey, con ...@@ -557,7 +546,7 @@ inline std::vector<BYTE> RegGetBinary(HKEY hKey, const std::wstring& subKey, con
// //
// Read the binary data from the registry into the vector object // Read the binary data from the registry into the vector object
// //
retCode = ::RegGetValue( retCode = ::RegGetValueW(
hKey, hKey,
subKey.c_str(), subKey.c_str(),
value.c_str(), value.c_str(),
...@@ -565,8 +554,7 @@ inline std::vector<BYTE> RegGetBinary(HKEY hKey, const std::wstring& subKey, con ...@@ -565,8 +554,7 @@ inline std::vector<BYTE> RegGetBinary(HKEY hKey, const std::wstring& subKey, con
nullptr, nullptr,
&data[0], &data[0],
&dataSize); &dataSize);
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS) {
{
throw RegistryError{ "Cannot read binary data from registry", retCode }; throw RegistryError{ "Cannot read binary data from registry", retCode };
} }
...@@ -602,8 +590,7 @@ inline std::vector<std::pair<std::wstring, DWORD>> RegEnumValues(HKEY hKey) ...@@ -602,8 +590,7 @@ inline std::vector<std::pair<std::wstring, DWORD>> RegEnumValues(HKEY hKey)
nullptr, // no security descriptor nullptr, // no security descriptor
nullptr // no last write time nullptr // no last write time
); );
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS) {
{
throw RegistryError{ "RegQueryInfoKey failed while preparing for value enumeration.", retCode }; throw RegistryError{ "RegQueryInfoKey failed while preparing for value enumeration.", retCode };
} }
...@@ -627,14 +614,13 @@ inline std::vector<std::pair<std::wstring, DWORD>> RegEnumValues(HKEY hKey) ...@@ -627,14 +614,13 @@ inline std::vector<std::pair<std::wstring, DWORD>> RegEnumValues(HKEY hKey)
// //
// Enumerate all the values // Enumerate all the values
// //
for (DWORD index = 0; index < valueCount; index++) for (DWORD index = 0; index < valueCount; index++) {
{
// //
// Get the name and the type of the current value // Get the name and the type of the current value
// //
DWORD valueNameLen = maxValueNameLen; DWORD valueNameLen = maxValueNameLen;
DWORD valueType{}; DWORD valueType{};
retCode = ::RegEnumValue( retCode = ::RegEnumValueW(
hKey, hKey,
index, index,
nameBuffer.get(), nameBuffer.get(),
...@@ -644,8 +630,7 @@ inline std::vector<std::pair<std::wstring, DWORD>> RegEnumValues(HKEY hKey) ...@@ -644,8 +630,7 @@ inline std::vector<std::pair<std::wstring, DWORD>> RegEnumValues(HKEY hKey)
nullptr, // no data nullptr, // no data
nullptr // no data size nullptr // no data size
); );
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS) {
{
throw RegistryError{ "Cannot enumerate values: RegEnumValue failed.", retCode }; throw RegistryError{ "Cannot enumerate values: RegEnumValue failed.", retCode };
} }
...@@ -689,8 +674,7 @@ inline std::vector<std::wstring> RegEnumSubKeys(HKEY hKey) ...@@ -689,8 +674,7 @@ inline std::vector<std::wstring> RegEnumSubKeys(HKEY hKey)
nullptr, // no security descriptor nullptr, // no security descriptor
nullptr // no last write time nullptr // no last write time
); );
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS) {
{
throw RegistryError{ "RegQueryInfoKey failed while preparing for subkey enumeration.", retCode }; throw RegistryError{ "RegQueryInfoKey failed while preparing for subkey enumeration.", retCode };
} }
...@@ -714,13 +698,12 @@ inline std::vector<std::wstring> RegEnumSubKeys(HKEY hKey) ...@@ -714,13 +698,12 @@ inline std::vector<std::wstring> RegEnumSubKeys(HKEY hKey)
// //
// Enumerate all the subkeys // Enumerate all the subkeys
// //
for (DWORD index = 0; index < subKeyCount; index++) for (DWORD index = 0; index < subKeyCount; index++) {
{
// //
// Get the name of the current subkey // Get the name of the current subkey
// //
DWORD subKeyNameLen = maxSubKeyNameLen; DWORD subKeyNameLen = maxSubKeyNameLen;
retCode = ::RegEnumKeyEx( retCode = ::RegEnumKeyExW(
hKey, hKey,
index, index,
nameBuffer.get(), nameBuffer.get(),
...@@ -730,8 +713,7 @@ inline std::vector<std::wstring> RegEnumSubKeys(HKEY hKey) ...@@ -730,8 +713,7 @@ inline std::vector<std::wstring> RegEnumSubKeys(HKEY hKey)
nullptr, // no class nullptr, // no class
nullptr // no last write time nullptr // no last write time
); );
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS) {
{
throw RegistryError{ "Cannot enumerate subkeys: RegEnumKeyEx failed.", retCode }; throw RegistryError{ "Cannot enumerate subkeys: RegEnumKeyEx failed.", retCode };
} }
...@@ -751,7 +733,7 @@ inline std::vector<std::wstring> RegEnumSubKeys(HKEY hKey) ...@@ -751,7 +733,7 @@ inline std::vector<std::wstring> RegEnumSubKeys(HKEY hKey)
inline RegKey RegCreateKey( inline RegKey RegCreateKey(
HKEY hKeyParent, HKEY hKeyParent,
const std::wstring& keyName, const std::wstring& keyName,
LPTSTR keyClass, LPWSTR keyClass,
DWORD options, DWORD options,
REGSAM access, REGSAM access,
SECURITY_ATTRIBUTES* securityAttributes, SECURITY_ATTRIBUTES* securityAttributes,
...@@ -762,7 +744,7 @@ inline RegKey RegCreateKey( ...@@ -762,7 +744,7 @@ inline RegKey RegCreateKey(
// Create the key invoking the native RegCreateKeyEx Win32 API // Create the key invoking the native RegCreateKeyEx Win32 API
// //
HKEY hKey = nullptr; HKEY hKey = nullptr;
LONG retCode = ::RegCreateKeyEx( LONG retCode = ::RegCreateKeyExW(
hKeyParent, hKeyParent,
keyName.c_str(), keyName.c_str(),
0, // reserved 0, // reserved
...@@ -773,8 +755,7 @@ inline RegKey RegCreateKey( ...@@ -773,8 +755,7 @@ inline RegKey RegCreateKey(
&hKey, &hKey,
disposition disposition
); );
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS) {
{
throw RegistryError{ "Cannot create registry key: RegCreateKeyEx failed.", retCode }; throw RegistryError{ "Cannot create registry key: RegCreateKeyEx failed.", retCode };
} }
...@@ -795,15 +776,14 @@ inline RegKey RegOpenKey( ...@@ -795,15 +776,14 @@ inline RegKey RegOpenKey(
// Open the key invoking the native RegOpenKeyEx Win32 API // Open the key invoking the native RegOpenKeyEx Win32 API
// //
HKEY hKey = nullptr; HKEY hKey = nullptr;
LONG retCode = ::RegOpenKeyEx( LONG retCode = ::RegOpenKeyExW(
hKeyParent, hKeyParent,
keyName.c_str(), keyName.c_str(),
0, // default options 0, // default options
access, access,
&hKey &hKey
); );
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS) {
{
throw RegistryError{ "Cannot open registry key: RegOpenKeyEx failed.", retCode }; throw RegistryError{ "Cannot open registry key: RegOpenKeyEx failed.", retCode };
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment