{"record":{"id":"e84894f5169b5842","repo":"dotnet/wpf","slug":"win32exception-safenativemethodsclr","errorCode":null,"errorMessage":"Win32Exception","messagePattern":"Win32Exception","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/SafeNativeMethodsCLR.cs","lineNumber":36,"sourceCode":"        {\n            return SafeNativeMethodsPrivate.GetKeyboardLayout(dwLayout);\n        }\n\n        public static IntPtr ActivateKeyboardLayout(HandleRef hkl, int uFlags)\n        {\n            return SafeNativeMethodsPrivate.ActivateKeyboardLayout(hkl, uFlags);\n        }\n\n#if BASE_NATIVEMETHODS\n        public static int GetKeyboardLayoutList(int size, [Out, MarshalAs(UnmanagedType.LPArray)] IntPtr[] hkls)\n        {\n            int result = NativeMethodsSetLastError.GetKeyboardLayoutList(size, hkls);\n            if (result == 0)\n            {\n                int win32Err = Marshal.GetLastWin32Error();\n                if (win32Err != 0)\n                {\n                    throw new Win32Exception(win32Err);\n                }\n            }\n\n            return result;\n        }\n#endif\n\n\n        internal static void GetMonitorInfo(HandleRef hmonitor, [In, Out] NativeMethods.MONITORINFOEX info)\n        {\n            if (!SafeNativeMethodsPrivate.IntGetMonitorInfo(hmonitor, info))\n            {\n                throw new Win32Exception();\n            }\n        }\n\n\n        public static IntPtr MonitorFromPoint(NativeMethods.POINT pt, int flags)","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/SafeNativeMethodsCLR.cs#L18-L54","documentation":"SafeNativeMethodsCLR.GetKeyboardLayoutList wraps the Win32 GetKeyboardLayoutList API and throws a Win32Exception wrapping Marshal.GetLastWin32Error when the API returns 0 (failure) with a non-zero error code — the list of installed keyboard layouts could not be retrieved.","triggerScenarios":"Calling GetKeyboardLayoutList when the underlying Win32 call fails: invalid size/buffer handling, or an OS-level failure such as insufficient resources or a corrupted keyboard-layout registry state.","commonSituations":"Input-language enumeration at startup on machines with damaged HKL registry entries (HKLM\\...\\Keyboard Layout\\Preload); running in unusual session or service contexts (session 0) with no input desktop.","solutions":["Read the Win32 error code on the exception and address the underlying OS issue (e.g. ERROR_INSUFFICIENT_BUFFER means size handling is broken)","Repair/inspect the keyboard layout Preload registry keys if layouts are corrupted","Avoid calling from non-interactive sessions (services) where keyboard layout APIs may fail","Retry once after failure — transient resource failures can clear"],"exampleFix":"// before\nint count = SafeNativeMethodsCLR.GetKeyboardLayoutList(0, null); // may throw\n// after\nint count;\ntry { count = SafeNativeMethodsCLR.GetKeyboardLayoutList(0, null); }\ncatch (Win32Exception ex) { log(ex.NativeErrorCode); count = 1; /* fallback to default layout */ }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { layouts = SafeNativeMethodsCLR.GetKeyboardLayoutList(size, hkls); } catch (Win32Exception ex) { log(ex.NativeErrorCode); layouts = fallbackToDefaultLayout(); }","preventionTips":["Don't call keyboard-layout APIs from non-interactive sessions/services","Keep machine keyboard-layout registry (Preload keys) healthy; suspect corruption on recurring failures","Provide a default-layout fallback so input enumeration never blocks startup"],"tags":["wpf","win32","keyboard","interop"],"backgroundTag":"api-error-response","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}