Skip to content

Commit f46e97f

Browse files
committed
Common (Windows): fixes querying HKEY_LOCAL_MACHINE on Windows 8.1
... and adds debug log
1 parent 7821552 commit f46e97f

1 file changed

Lines changed: 51 additions & 15 deletions

File tree

src/common/windows/registry.c

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "registry.h"
22
#include "unicode.h"
33
#include "common/mallocHelper.h"
4+
#include "common/debug.h"
45
#include "common/windows/nt.h"
56

67
#include <stdalign.h>
@@ -29,42 +30,62 @@ HANDLE ffRegGetRootKeyHandle(HKEY hKey)
2930
assert(hKey);
3031
assert((uintptr_t) hKey >= (uintptr_t) HKEY_CLASSES_ROOT && (uintptr_t) hKey <= (uintptr_t) HKEY_CURRENT_USER_LOCAL_SETTINGS);
3132

32-
if (hRootKeys[(uintptr_t) hKey - (uintptr_t) HKEY_CLASSES_ROOT])
33-
return hRootKeys[(uintptr_t) hKey - (uintptr_t) HKEY_CLASSES_ROOT];
33+
FF_DEBUG("Getting root key handle for HKEY %08llx", (uint64_t)(uintptr_t) hKey);
34+
35+
HANDLE result = hRootKeys[(uintptr_t) hKey - (uintptr_t) HKEY_CLASSES_ROOT];
36+
if (result)
37+
{
38+
FF_DEBUG("Found cached root key handle for %s -> %p", hKey2Str(result), result);
39+
return result;
40+
}
3441

35-
HANDLE result = NULL;
3642
switch ((uintptr_t) hKey)
3743
{
3844
case (uintptr_t) HKEY_CURRENT_USER: {
3945
UNICODE_STRING path = {};
40-
if (!NT_SUCCESS(RtlFormatCurrentUserKeyPath(&path))) return NULL;
41-
if (!NT_SUCCESS(NtOpenKey(&result, KEY_READ, &(OBJECT_ATTRIBUTES) {
46+
NTSTATUS status = RtlFormatCurrentUserKeyPath(&path);
47+
if (!NT_SUCCESS(status))
48+
{
49+
FF_DEBUG("RtlFormatCurrentUserKeyPath() failed: %s", ffDebugNtStatus(status));
50+
return NULL;
51+
}
52+
status = NtOpenKey(&result, KEY_READ, &(OBJECT_ATTRIBUTES) {
4253
.Length = sizeof(OBJECT_ATTRIBUTES),
4354
.RootDirectory = NULL,
4455
.ObjectName = &path,
45-
})))
56+
});
57+
if (!NT_SUCCESS(status))
4658
{
59+
FF_DEBUG("NtOpenKey(%ls) failed: %s (0x%08lx)", path.Buffer, ffDebugNtStatus(status), status);
4760
RtlFreeUnicodeString(&path);
4861
return NULL;
4962
}
5063
RtlFreeUnicodeString(&path);
5164
break;
5265
}
5366
case (uintptr_t) HKEY_LOCAL_MACHINE: {
54-
if (!NT_SUCCESS(NtOpenKey(&result, KEY_READ, &(OBJECT_ATTRIBUTES) {
67+
UNICODE_STRING path = RTL_CONSTANT_STRING(L"\\Registry\\Machine");
68+
NTSTATUS status = NtOpenKey(&result, KEY_READ, &(OBJECT_ATTRIBUTES) {
5569
.Length = sizeof(OBJECT_ATTRIBUTES),
5670
.RootDirectory = NULL,
57-
.ObjectName = &(UNICODE_STRING)RTL_CONSTANT_STRING(L"\\Registry\\Machine"),
58-
})))
71+
.ObjectName = &path,
72+
.Attributes = OBJ_CASE_INSENSITIVE,
73+
});
74+
if (!NT_SUCCESS(status))
75+
{
76+
FF_DEBUG("NtOpenKey(%ls) failed: %s (0x%08lx)", path.Buffer, ffDebugNtStatus(status), status);
5977
return NULL;
78+
}
6079
break;
6180
}
6281
default:
6382
// Unsupported
83+
FF_DEBUG("Unsupported root key: %p", hKey);
6484
assert(false);
6585
return NULL;
6686
}
6787
hRootKeys[(uintptr_t) hKey - (uintptr_t) HKEY_CLASSES_ROOT] = result;
88+
FF_DEBUG("Opened root key %s -> %p", hKey2Str(result), result);
6889
return result;
6990
}
7091

@@ -74,6 +95,8 @@ bool ffRegOpenSubkeyForRead(HANDLE hKey, const wchar_t* subKeyW, HANDLE* result,
7495
assert(subKeyW);
7596
assert(result);
7697

98+
FF_DEBUG("Opening subkey %s\\%ls for read", hKey2Str(hKey), subKeyW);
99+
77100
USHORT subKeyLen = (USHORT) (wcslen(subKeyW) * sizeof(wchar_t));
78101
if (!NT_SUCCESS(NtOpenKey(result, KEY_READ, &(OBJECT_ATTRIBUTES) {
79102
.Length = sizeof(OBJECT_ATTRIBUTES),
@@ -85,13 +108,15 @@ bool ffRegOpenSubkeyForRead(HANDLE hKey, const wchar_t* subKeyW, HANDLE* result,
85108
},
86109
})))
87110
{
111+
FF_DEBUG("NtOpenKey(%s\\<subkey>) failed", hKey2Str(hKey));
88112
if (error)
89113
{
90114
FF_STRBUF_AUTO_DESTROY subKeyA = ffStrbufCreateWS(subKeyW);
91115
ffStrbufAppendF(error, "NtOpenKey(%s\\%s) failed", hKey2Str(hKey), subKeyA.chars);
92116
}
93117
return false;
94118
}
119+
FF_DEBUG("Opened subkey under %s -> %p", hKey2Str(hKey), *result);
95120
return true;
96121
}
97122

@@ -230,6 +255,8 @@ static bool processRegValue(const FFRegValueArg* arg, const ULONG regType, const
230255
return true;
231256

232257
type_mismatch:
258+
FF_DEBUG("ffRegReadValues(%ls) type mismatch: regType=%u, argType=%u, dataLen=%u",
259+
arg->name ?: L"(default)", (unsigned) regType, (unsigned) arg->type, (unsigned) regDataLen);
233260
if (error)
234261
{
235262
FF_STRBUF_AUTO_DESTROY nameA = arg->name ? ffStrbufCreateWS(arg->name) : ffStrbufCreateStatic("(default)");
@@ -257,10 +284,11 @@ bool ffRegReadValue(HANDLE hKey, const FFRegValueArg* arg, FFstrbuf* error)
257284

258285
if (bufSize == 0)
259286
{
287+
FF_DEBUG("NtQueryValueKey(%p, %ls) failed (bufSize=0)", hKey, arg->name ?: L"(default)");
260288
if (error)
261289
{
262290
FF_STRBUF_AUTO_DESTROY valueNameA = arg->name ? ffStrbufCreateWS(arg->name) : ffStrbufCreateStatic("(default)");
263-
ffStrbufAppendF(error, "NtQueryValueKey(%s, %s) failed", hKey2Str(hKey), valueNameA.chars);
291+
ffStrbufAppendF(error, "NtQueryValueKey(%p, %s) failed", hKey, valueNameA.chars);
264292
}
265293
return false;
266294
}
@@ -270,15 +298,17 @@ bool ffRegReadValue(HANDLE hKey, const FFRegValueArg* arg, FFstrbuf* error)
270298

271299
if (!NT_SUCCESS(NtQueryValueKey(hKey, valueNameU, KeyValuePartialInformation, buffer, bufSize, &bufSize)))
272300
{
301+
FF_DEBUG("NtQueryValueKey(%p, %ls, buffer=%u) failed", hKey, arg->name ?: L"(default)", (unsigned) bufSize);
273302
if (error)
274303
{
275304
FF_STRBUF_AUTO_DESTROY valueNameA = arg->name ? ffStrbufCreateWS(arg->name) : ffStrbufCreateStatic("(default)");
276-
ffStrbufAppendF(error, "NtQueryValueKey(%s, %s, buffer) failed", hKey2Str(hKey), valueNameA.chars);
305+
ffStrbufAppendF(error, "NtQueryValueKey(%p, %s, buffer) failed", hKey, valueNameA.chars);
277306
}
278307
return false;
279308
}
280309

281310
process_value:
311+
FF_DEBUG("Read value from %p (%ls), type=%u, len=%u", hKey, arg->name ?: L"(default)", (unsigned) buffer->Type, (unsigned) buffer->DataLength);
282312
return processRegValue(arg, buffer->Type, buffer->Data, buffer->DataLength, error);
283313
}
284314

@@ -296,6 +326,7 @@ bool ffRegReadValues(HANDLE hKey, uint32_t argc, const FFRegValueArg argv[], FFs
296326
{
297327
if (__builtin_expect(!argv[i].value, false))
298328
{
329+
FF_DEBUG("ffRegReadValues(argv[%u].value) is NULL", (unsigned) i);
299330
if (error) ffStrbufAppendF(error, "ffRegReadValues(argv[%u].pVar) is NULL", (unsigned) i);
300331
return false;
301332
}
@@ -327,13 +358,15 @@ bool ffRegReadValues(HANDLE hKey, uint32_t argc, const FFRegValueArg argv[], FFs
327358
// Buffer too small: docs guarantee requiredSize is returned when provided.
328359
if (requiredSize > bufferSize)
329360
{
361+
FF_DEBUG("NtQueryMultipleValueKey(%p) resize buffer: %u -> %u", hKey, (unsigned) bufferSize, (unsigned) requiredSize);
330362
bufferSize = requiredSize;
331363
continue;
332364
}
333365

366+
FF_DEBUG("NtQueryMultipleValueKey(%p, argc=%u) failed, status=0x%08X", hKey, (unsigned) argc, (unsigned) status);
334367
if (error)
335-
ffStrbufAppendF(error, "NtQueryMultipleValueKey(%s, argc=%u) failed, status=0x%08X",
336-
hKey2Str(hKey), (unsigned) argc, (unsigned) status);
368+
ffStrbufAppendF(error, "NtQueryMultipleValueKey(%p, argc=%u) failed, status=0x%08X",
369+
hKey, (unsigned) argc, (unsigned) status);
337370
return false;
338371
}
339372

@@ -345,6 +378,7 @@ bool ffRegReadValues(HANDLE hKey, uint32_t argc, const FFRegValueArg argv[], FFs
345378
const FFRegValueArg* arg = &argv[i];
346379
const KEY_VALUE_ENTRY* entry = &entries[i];
347380

381+
FF_DEBUG("Read value[%u] from %p: type=%u, len=%u", (unsigned) i, hKey, (unsigned) entry->Type, (unsigned) entry->DataLength);
348382
if (!processRegValue(arg, entry->Type, buffer + entry->DataOffset, entry->DataLength, error))
349383
return false;
350384
}
@@ -363,8 +397,9 @@ bool ffRegGetSubKey(HANDLE hKey, uint32_t index, FFstrbuf* result, FFstrbuf* err
363397

364398
if (!NT_SUCCESS(NtEnumerateKey(hKey, index, KeyBasicInformation, keyInfo, bufSize, &bufSize)))
365399
{
400+
FF_DEBUG("NtEnumerateKey(hKey=%p, index=%u) failed", hKey, (unsigned) index);
366401
if (error)
367-
ffStrbufAppendF(error, "NtEnumerateKey(hKey, %u, keyInfo) failed", (unsigned) index);
402+
ffStrbufAppendF(error, "NtEnumerateKey(hKey=%p, %u, keyInfo) failed", hKey, (unsigned) index);
368403
return false;
369404
}
370405

@@ -383,8 +418,9 @@ bool ffRegGetNSubKeys(HANDLE hKey, uint32_t* result, FFstrbuf* error)
383418

384419
if (!NT_SUCCESS(NtQueryKey(hKey, KeyFullInformation, keyInfo, bufSize, &bufSize)))
385420
{
421+
FF_DEBUG("NtQueryKey(hKey=%p, KeyFullInformation) failed", hKey);
386422
if (error)
387-
ffStrbufAppendS(error, "NtQueryKey(hKey, KeyFullInformation, keyInfo) failed");
423+
ffStrbufAppendF(error, "NtQueryKey(hKey=%p, KeyFullInformation, keyInfo) failed", hKey);
388424
return false;
389425
}
390426

0 commit comments

Comments
 (0)