{"record":{"id":"30bf6115d83a9976","repo":"dotnet/wpf","slug":"failed-to-initialize-gesturerecognizer","errorCode":null,"errorMessage":"Failed to initialize GestureRecognizer.","messagePattern":"Failed to initialize GestureRecognizer\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/GestureRecognizer/NativeRecognizer.cs","lineNumber":51,"sourceCode":"        /// </summary>\n        static NativeRecognizer()\n        {\n            s_isSupported = LoadRecognizerDll();\n        }\n\n        /// <summary>\n        /// Private constructor\n        /// </summary>\n        private NativeRecognizer()\n        {\n            Debug.Assert(NativeRecognizer.RecognizerHandleSingleton != null);\n\n            int hr = MS.Win32.Recognizer.UnsafeNativeMethods.CreateContext(NativeRecognizer.RecognizerHandleSingleton,\n                                                                        out _hContext);\n            if (HRESULT.Failed(hr))\n            {\n                //don't throw a com exception here, we don't need to pass out any details\n                throw new InvalidOperationException(SR.UnspecifiedGestureConstructionException);\n            }\n\n            // We add a reference of the recognizer to the context handle.\n            // The context will dereference the recognizer reference when it gets disposed.\n            // This trick will prevent the GC from disposing the recognizer before all contexts.\n            _hContext.AddReferenceOnRecognizer(NativeRecognizer.RecognizerHandleSingleton);\n        }\n\n        #endregion Constructors\n\n        //-------------------------------------------------------------------------------\n        //\n        // Internal Methods\n        //\n        //-------------------------------------------------------------------------------\n\n        #region Internal Methods\n","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/GestureRecognizer/NativeRecognizer.cs#L33-L69","documentation":"NativeRecognizer's constructor calls CreateContext to create a ink-recognition context from the system gesture recognizer. If the native call returns a failed HRESULT it throws InvalidOperationException(SR.UnspecifiedGestureConstructionException) — 'Failed to initialize GestureRecognizer.' — deliberately without COM details, since nothing useful can be passed to managed callers.","triggerScenarios":"Constructing a GestureRecognizer (which creates NativeRecognizer) on a machine where the ink recognition native components are missing or failed — e.g. Windows features 'Ink and Handwriting Recognition' not installed, a corrupted Tablet PC/ink stack, or CreateContext returning an error HRESULT.","commonSituations":"Server SKUs or stripped-down Windows installations without the handwriting/ink recognizer features; running in environments (containers, services, virtualized sessions) lacking the Tablet PC runtime; WPF apps using gesture recognition on systems where the recognizer DLLs fail to load.","solutions":["Install the Windows 'Ink and Handwriting Recognition' / Tablet PC optional features on the target machine.","Wrap GestureRecognizer construction in try/catch for InvalidOperationException and degrade gracefully (disable gesture recognition) when unavailable.","Detect recognizer availability before use and hide/disable ink-gesture UI accordingly.","Repair/verify the Windows ink components (sfc /scannow, reinstall the ink feature) if they are corrupted."],"exampleFix":"// before\nvar recognizer = new GestureRecognizer(); // InvalidOperationException on systems without ink recognizers\n// after\nGestureRecognizer recognizer = null;\ntry\n{\n    recognizer = new GestureRecognizer();\n}\ncatch (InvalidOperationException)\n{\n    // ink recognition unavailable: fall back to no gesture support\n}","handlingStrategy":"try-catch","validationCode":"static bool InkRecognizersAvailable()\n{\n    try { using var g = new GestureRecognizer(); return true; }\n    catch (InvalidOperationException) { return false; }\n}","typeGuard":"static GestureRecognizer TryCreateRecognizer() =>\n    OperatingSystem.IsWindows() ? ProbeRecognizer() : null;","tryCatchPattern":"try { recognizer = new GestureRecognizer(); }\ncatch (InvalidOperationException)\n{\n    recognizer = null;\n    DisableGestureFeatures(); // degrade gracefully\n}","preventionTips":["Install the Windows Ink/Handwriting Recognition optional feature on target machines","Feature-detect recognizer availability at startup","Design UI to work without gesture recognition on server/SKU-limited systems"],"tags":["wpf","ink","gesturerecognizer","native-interop","initialization-failure","windows"],"backgroundTag":"module-init-failed","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"}