Skip to content

Commit

Permalink
Revert to using ::SHQueryValueEx instead of ::RegQueryValueEx
Browse files Browse the repository at this point in the history
https://www.geoffchappell.com/studies/windows/shell/shlwapi/api/reg/queryvalueex.htm
```
Although this function and the standard API function RegQueryValueEx have exactly the same prototype, the two do not behave identically. A call to the SHLWAPI function is essentially a call to the standard function but with post-processing, presumably with the idea of improving or even correcting the standard function. There are two general aims:

to fix REG_SZ or REG_EXPAND_SZ data that is not properly null-terminated;
to expand REG_EXPAND_SZ data and return it as REG_SZ data.
```
  • Loading branch information
sorinj committed Dec 13, 2023
1 parent 09093d5 commit b7df00a
Showing 1 changed file with 36 additions and 38 deletions.
74 changes: 36 additions & 38 deletions omaha/base/reg_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -486,21 +486,20 @@ HRESULT RegKey::GetValueHelper(const TCHAR * value_name,
*value = NULL;

DWORD num_bytes = 0;
LONG res =
::RegQueryValueEx(h_key_, value_name, NULL, type, NULL, &num_bytes);
LONG res = ::SHQueryValueEx(h_key_, value_name, NULL, type, NULL, &num_bytes);
HRESULT hr = HRESULT_FROM_WIN32(res);

if (hr == S_OK) {
if (num_bytes != 0) {
*value = new byte[num_bytes];
ASSERT1(*value);

res = ::RegQueryValueEx(h_key_,
value_name,
NULL,
type,
*value,
&num_bytes);
res = ::SHQueryValueEx(h_key_,
value_name,
NULL,
type,
*value,
&num_bytes);
hr = HRESULT_FROM_WIN32(res);
ASSERT1(S_OK == hr);
}
Expand All @@ -518,8 +517,7 @@ HRESULT RegKey::GetValueType(const TCHAR* value_name,

*value_type = REG_NONE;

LONG res =
::RegQueryValueEx(h_key_, value_name, NULL, value_type, NULL, NULL);
LONG res = ::SHQueryValueEx(h_key_, value_name, NULL, value_type, NULL, NULL);
if (res != ERROR_SUCCESS) {
return HRESULT_FROM_WIN32(res);
}
Expand All @@ -535,12 +533,12 @@ HRESULT RegKey::GetValue(const TCHAR * value_name, DWORD * value) const {

DWORD type = 0;
DWORD byte_count = sizeof(DWORD);
LONG res = ::RegQueryValueEx(h_key_,
value_name,
NULL,
&type,
reinterpret_cast<byte*>(value),
&byte_count);
LONG res = ::SHQueryValueEx(h_key_,
value_name,
NULL,
&type,
reinterpret_cast<byte*>(value),
&byte_count);
HRESULT hr = HRESULT_FROM_WIN32(res);
ASSERT1((hr != S_OK) || (type == REG_DWORD));
ASSERT1((hr != S_OK) || (byte_count == sizeof(DWORD)));
Expand All @@ -555,12 +553,12 @@ HRESULT RegKey::GetValue(const TCHAR * value_name, DWORD64 * value) const {

DWORD type = 0;
DWORD byte_count = sizeof(DWORD64);
LONG res = ::RegQueryValueEx(h_key_,
value_name,
NULL,
&type,
reinterpret_cast<byte *>(value),
&byte_count);
LONG res = ::SHQueryValueEx(h_key_,
value_name,
NULL,
&type,
reinterpret_cast<byte *>(value),
&byte_count);
HRESULT hr = HRESULT_FROM_WIN32(res);
ASSERT1((hr != S_OK) || (type == REG_QWORD));
ASSERT1((hr != S_OK) || (byte_count == sizeof(DWORD64)));
Expand All @@ -587,12 +585,12 @@ HRESULT RegKey::GetValue(const TCHAR * value_name, TCHAR * * value) const {
DWORD type = 0;

// first get the size of the string buffer
LONG res = ::RegQueryValueEx(h_key_,
value_name,
NULL,
&type,
NULL,
&byte_count);
LONG res = ::SHQueryValueEx(h_key_,
value_name,
NULL,
&type,
NULL,
&byte_count);
HRESULT hr = HRESULT_FROM_WIN32(res);

if (hr == S_OK) {
Expand All @@ -602,8 +600,8 @@ HRESULT RegKey::GetValue(const TCHAR * value_name, TCHAR * * value) const {
if ((*value) != NULL) {
if (byte_count != 0) {
// make the call again
res = ::RegQueryValueEx(h_key_, value_name, NULL, &type,
reinterpret_cast<byte*>(*value), &byte_count);
res = ::SHQueryValueEx(h_key_, value_name, NULL, &type,
reinterpret_cast<byte*>(*value), &byte_count);
hr = HRESULT_FROM_WIN32(res);
} else {
(*value)[0] = _T('\0');
Expand All @@ -629,12 +627,12 @@ HRESULT RegKey::GetValue(const TCHAR* value_name, OUT CString* value) const {
DWORD type = 0;

// first get the size of the string buffer
LONG res = ::RegQueryValueEx(h_key_,
value_name,
NULL,
&type,
NULL,
&byte_count);
LONG res = ::SHQueryValueEx(h_key_,
value_name,
NULL,
&type,
NULL,
&byte_count);
HRESULT hr = HRESULT_FROM_WIN32(res);

if (hr == S_OK) {
Expand All @@ -644,8 +642,8 @@ HRESULT RegKey::GetValue(const TCHAR* value_name, OUT CString* value) const {
if (buffer == NULL) {
hr = E_OUTOFMEMORY;
} else {
res = ::RegQueryValueEx(h_key_, value_name, NULL, &type,
reinterpret_cast<byte*>(buffer), &byte_count);
res = ::SHQueryValueEx(h_key_, value_name, NULL, &type,
reinterpret_cast<byte*>(buffer), &byte_count);
hr = HRESULT_FROM_WIN32(res);
}
value->ReleaseBuffer();
Expand Down

0 comments on commit b7df00a

Please sign in to comment.