- const WCHAR szKeyboardLayoutPath[] = L"\\Keyboard Layout\\Preload";
- const WCHAR szDefaultUserPath[] = L"\\REGISTRY\\USER\\.DEFAULT";
-
- HANDLE KeyHandle;
- LCID LayoutLocaleId = 0;
- NTSTATUS Status;
- OBJECT_ATTRIBUTES KeyAttributes;
- PKBL pKbl;
- UNICODE_STRING CurrentUserPath;
- UNICODE_STRING FullKeyboardLayoutPath;
- UNICODE_STRING LayoutValueName;
- UNICODE_STRING LayoutLocaleIdString;
- WCHAR wszBuffer[MAX_PATH];
-
- // Get the path to HKEY_CURRENT_USER
- Status = RtlFormatCurrentUserKeyPath(&CurrentUserPath);
-
- if( NT_SUCCESS(Status) )
- {
- FullKeyboardLayoutPath.Buffer = wszBuffer;
- FullKeyboardLayoutPath.MaximumLength = sizeof(wszBuffer);
-
- // FIXME: Is this 100% correct?
- // We're called very early, so HKEY_CURRENT_USER might not be available yet. Check this first.
- InitializeObjectAttributes(&KeyAttributes, &CurrentUserPath, OBJ_CASE_INSENSITIVE, NULL, NULL);
- Status = ZwOpenKey(&KeyHandle, KEY_READ, &KeyAttributes);
-
- if(Status == STATUS_OBJECT_NAME_NOT_FOUND)
- {
- // It is not available, so read it from HKEY_USERS\.DEFAULT
- FullKeyboardLayoutPath.Length = sizeof(szDefaultUserPath) - sizeof(UNICODE_NULL);
- RtlCopyMemory(wszBuffer, szDefaultUserPath, sizeof(szDefaultUserPath));
- }
- else
- {
- // The path is available
- ZwClose(KeyHandle);
- RtlCopyUnicodeString(&FullKeyboardLayoutPath, &CurrentUserPath);
- }
-
- // Free CurrentUserPath - we dont need it anymore
- RtlFreeUnicodeString(&CurrentUserPath);
-
- Status = RtlAppendUnicodeToString(&FullKeyboardLayoutPath, szKeyboardLayoutPath);
-
- if( NT_SUCCESS(Status) )
- {
- // Return the first keyboard layout listed there
- RtlInitUnicodeString(&LayoutValueName, L"1");
-
- Status = ReadRegistryValue(&FullKeyboardLayoutPath, &LayoutValueName, &LayoutLocaleIdString);
-
- if( NT_SUCCESS(Status) )
- {
- RtlUnicodeStringToInteger(&LayoutLocaleIdString, 16, &LayoutLocaleId);
- ExFreePoolWithTag(LayoutLocaleIdString.Buffer, TAG_STRING);
- }
- else
- ERR("ReadRegistryValue failed! (%08lx).\n", Status);
- }
- else
- ERR("RtlAppendUnicodeToString failed! (%08lx)\n", Status);
- }
- else
- ERR("RtlFormatCurrentUserKeyPath failed! (%08lx)\n", Status);
-
- if(!LayoutLocaleId)
- {
- ERR("Assuming default locale for the keyboard layout (0x409 - US)\n");
- LayoutLocaleId = 0x409;
- }
-
- pKbl = KBLList;
- do
- {
- if(pKbl->klid == LayoutLocaleId)
- {
- return pKbl;
- }
-
- pKbl = (PKBL) pKbl->List.Flink;
- } while(pKbl != KBLList);
-
- TRACE("Loading new default keyboard layout.\n");
- pKbl = UserLoadDllAndCreateKbl(LayoutLocaleId);
-
- if(!pKbl)
- {
- TRACE("Failed to load %x!!! Returning any availableKL.\n", LayoutLocaleId);
- return KBLList;
- }
-
- InsertTailList(&KBLList->List, &pKbl->List);
- return pKbl;
+ const WCHAR szKeyboardLayoutPath[] = L"\\Keyboard Layout\\Preload";
+ const WCHAR szDefaultUserPath[] = L"\\REGISTRY\\USER\\.DEFAULT";
+
+ HANDLE KeyHandle;
+ LCID LayoutLocaleId = 0;
+ NTSTATUS Status;
+ OBJECT_ATTRIBUTES KeyAttributes;
+ PKBL pKbl;
+ UNICODE_STRING CurrentUserPath;
+ UNICODE_STRING FullKeyboardLayoutPath;
+ UNICODE_STRING LayoutValueName;
+ UNICODE_STRING LayoutLocaleIdString;
+ WCHAR wszBuffer[MAX_PATH];
+
+ // Get the path to HKEY_CURRENT_USER
+ Status = RtlFormatCurrentUserKeyPath(&CurrentUserPath);
+
+ if (NT_SUCCESS(Status))
+ {
+ FullKeyboardLayoutPath.Buffer = wszBuffer;
+ FullKeyboardLayoutPath.MaximumLength = sizeof(wszBuffer);
+
+ // FIXME: Is this 100% correct?
+ // We're called very early, so HKEY_CURRENT_USER might not be available yet. Check this first.
+ InitializeObjectAttributes(&KeyAttributes, &CurrentUserPath, OBJ_CASE_INSENSITIVE, NULL, NULL);
+ Status = ZwOpenKey(&KeyHandle, KEY_READ, &KeyAttributes);
+
+ if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
+ {
+ // It is not available, so read it from HKEY_USERS\.DEFAULT
+ FullKeyboardLayoutPath.Length = sizeof(szDefaultUserPath) - sizeof(UNICODE_NULL);
+ RtlCopyMemory(wszBuffer, szDefaultUserPath, sizeof(szDefaultUserPath));
+ }
+ else
+ {
+ // The path is available
+ ZwClose(KeyHandle);
+ RtlCopyUnicodeString(&FullKeyboardLayoutPath, &CurrentUserPath);
+ }
+
+ // Free CurrentUserPath - we dont need it anymore
+ RtlFreeUnicodeString(&CurrentUserPath);
+
+ Status = RtlAppendUnicodeToString(&FullKeyboardLayoutPath, szKeyboardLayoutPath);
+
+ if (NT_SUCCESS(Status))
+ {
+ // Return the first keyboard layout listed there
+ RtlInitUnicodeString(&LayoutValueName, L"1");
+
+ Status = ReadRegistryValue(&FullKeyboardLayoutPath, &LayoutValueName, &LayoutLocaleIdString);
+
+ if (NT_SUCCESS(Status))
+ {
+ RtlUnicodeStringToInteger(&LayoutLocaleIdString, 16, &LayoutLocaleId);
+ ExFreePoolWithTag(LayoutLocaleIdString.Buffer, TAG_STRING);
+ }
+ else
+ ERR("ReadRegistryValue failed! (%08lx).\n", Status);
+ }
+ else
+ ERR("RtlAppendUnicodeToString failed! (%08lx)\n", Status);
+ }
+ else
+ ERR("RtlFormatCurrentUserKeyPath failed! (%08lx)\n", Status);
+
+ if (!LayoutLocaleId)
+ {
+ ERR("Assuming default locale for the keyboard layout (0x409 - US)\n");
+ LayoutLocaleId = 0x409;
+ }
+
+ pKbl = KBLList;
+ do
+ {
+ if (pKbl->klid == LayoutLocaleId)
+ {
+ return pKbl;
+ }
+
+ pKbl = (PKBL) pKbl->List.Flink;
+ } while (pKbl != KBLList);
+
+ TRACE("Loading new default keyboard layout.\n");
+ pKbl = UserLoadDllAndCreateKbl(LayoutLocaleId);
+
+ if (!pKbl)
+ {
+ TRACE("Failed to load %x!!! Returning any availableKL.\n", LayoutLocaleId);
+ return KBLList;
+ }
+
+ InsertTailList(&KBLList->List, &pKbl->List);
+ return pKbl;