{"record":{"id":"23990b5b5a43012e","repo":"dotnet/wpf","slug":"win32error-system-componentmodel-win32exception","errorCode":null,"errorMessage":"win32Error (System.ComponentModel.Win32Exception)","messagePattern":"win32Error \\(System\\.ComponentModel\\.Win32Exception\\)","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextFindEngine.cs","lineNumber":582,"sourceCode":"                    }\n                }\n            }\n\n            return matchIndex;\n        }\n\n        //  Fixing method signature to meet TAS security requirements.\n        private static int FindNLSString(int locale, uint flags, string sourceString, string findString, out int found)\n        {\n            int matchIndex = UnsafeNativeMethods.FindNLSString(locale, flags,\n                                sourceString, sourceString.Length, findString, findString.Length, out found);\n\n            if (matchIndex == -1)\n            {\n                int win32Error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();\n                if (win32Error != 0)\n                {\n                    throw new System.ComponentModel.Win32Exception(win32Error);\n                }\n            }\n\n            return matchIndex;\n        }\n\n        // Returns true iff two matching strings continue to match when kashida are considered.\n        private static bool IsKashidaMatch(string text, string pattern, CompareInfo compareInfo)\n        {\n            const CompareOptions options = CompareOptions.IgnoreSymbols | CompareOptions.StringSort | CompareOptions.IgnoreNonSpace;\n\n            // Relace the Kashida char with a non-symbolic, non-diacritic constant value.\n            // Kashida is ignored with the CompareOptions we use during the search.\n            // When called, it's ok if the constant value matches other chars in the find pattern.\n            // We've already got a match ignoring kashida.\n\n            text = text.Replace(UnicodeArabicKashida, '0');\n            pattern = pattern.Replace(UnicodeArabicKashida, '0');","sourceCodeStart":564,"sourceCodeEnd":600,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextFindEngine.cs#L564-L600","documentation":"TextFindEngine.FindNLSString wraps the native FindNLSStringEx Win32 API. When the native call fails (matchIndex == -1) and Marshal.GetLastWin32Error() is non-zero, the error code is surfaced as a System.ComponentModel.Win32Exception. This means the OS-level culture-aware string search itself failed.","triggerScenarios":"FindNLSString invoked with flags/inputs the native API rejects (e.g., invalid find flag combinations such as Bidi/diacritics handling on unsupported data), causing the P/Invoke to return -1 with a Win32 error set.","commonSituations":"Searching text in RichTextBox/TextRange with unusual culture settings; exotic locale data on stripped-down Windows installs; calling find helpers with flags unsupported by the running OS version.","solutions":["Catch Win32Exception and fall back to a simple String.IndexOf comparison","Verify the OS supports the FindNLSStringEx flags being requested (Windows Vista+)","Check the exception's NativeErrorCode against Win32 error docs to identify the exact failure","Normalize culture/locale arguments passed into the find call"],"exampleFix":"// before\nint idx = TextFindEngine.FindNLSString(source, flags, value);\n\n// after\nint idx;\ntry { idx = TextFindEngine.FindNLSString(source, flags, value); }\ncatch (Win32Exception ex)\n{\n    Trace.TraceWarning(\"NLS find failed: {0}\", ex.NativeErrorCode);\n    idx = source.IndexOf(value, StringComparison.CurrentCulture);\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { idx = TextFindEngine.FindNLSString(source, flags, value); } catch (System.ComponentModel.Win32Exception ex) { log(ex.NativeErrorCode); idx = source.IndexOf(value, StringComparison.CurrentCulture); }","preventionTips":["Fall back to managed string comparison when NLS search fails","Only use find flags supported by the target OS version","Log the NativeErrorCode to diagnose locale/OS-specific failures"],"tags":["win32","native-interop","text-search"],"backgroundTag":"win32-error","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}