{"record":{"id":"ab217629e519e331","repo":"dotnet/wpf","slug":"invalidoperationexception-unsafenativemethodspenimc","errorCode":null,"errorMessage":"InvalidOperationException","messagePattern":"InvalidOperationException","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/Win32/UnsafeNativeMethodsPenimc.cs","lineNumber":181,"sourceCode":"            return ((IPimcManager3)pimcManagerObj);\n        }\n\n        #region COM Locking/Unlocking Functions\n\n        #region General\n\n        /// <summary>\n        /// Calls WISP GIT lock functions on Win8+.\n        /// On Win7 these will always fail since WISP objects are always proxies (WISP is out of proc).\n        /// </summary>\n        /// <param name=\"gitKey\">The GIT key for the object to lock.</param>\n        internal static void CheckedLockWispObjectFromGit(UInt32 gitKey)\n        {\n            if (OSVersionHelper.IsOsWindows8OrGreater)\n            {\n                if (!LockWispObjectFromGit(gitKey))\n                {\n                    throw new InvalidOperationException();\n                }\n            }\n        }\n\n        /// <summary>\n        /// Calls WISP GIT unlock functions on Win8+.\n        /// On Win7 these will always fail since WISP objects are always proxies (WISP is out of proc).\n        /// </summary>\n        /// <param name=\"gitKey\">The GIT key for the object to unlock.</param>\n        internal static void CheckedUnlockWispObjectFromGit(UInt32 gitKey)\n        {\n            if (OSVersionHelper.IsOsWindows8OrGreater)\n            {\n                if (!UnlockWispObjectFromGit(gitKey))\n                {\n                    throw new InvalidOperationException();\n                }\n            }","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/Win32/UnsafeNativeMethodsPenimc.cs#L163-L199","documentation":"WPF's stylus/touch (PenIMC) layer throws this bare InvalidOperationException from CheckedLockWispObjectFromGit when the native Win32 call LockWispObjectFromGit fails on Windows 8 or greater. Locking the WISP (Windows Ink Services Platform) Global Interface Table entry is a prerequisite for using the pen/ink subsystem; failure means the native tablet service did not grant the lock, so WPF aborts rather than continue with inconsistent state.","triggerScenarios":"A WPF app that uses stylus/touch input (Tablet service, InkCanvas, real-time stylus) calls LockWispManager, which invokes CheckedLockWispObjectFromGit; the P/Invoke LockWispObjectFromGit returns false (native failure) on Windows 8+, causing the throw. Happens during TabletService/WispTabletDevice initialization.","commonSituations":"RDP/terminal-server or VM sessions where the tablet service is unavailable or disabled; Windows Tablet Input Service (TabletInputService/TabSvc) stopped or broken; systems where WISP components are corrupted or the pen driver stack fails; headless machines or sessions without a visible desktop that still touch the stylus code path.","solutions":["Ensure the Windows 'Tablet PC Input Service' (TabletInputService / TabSvc) is running on the machine.","Check whether the app is running in an RDP/remote session; WISP real-time stylus is unavailable there - catch the exception or disable stylus-dependent features in remote sessions.","Repair/verify Windows ink components (sfc /scannow, Windows Update, reinstall pen/display drivers).","Wrap WPF window/ink initialization in try-catch and fall back to mouse-only input when WISP locking fails."],"exampleFix":"// before (crash on RDP / no tablet service)\nvar win = new StylusWindow();\n\n// after (degrade gracefully)\ntry\n{\n    var win = new StylusWindow();\n}\ncatch (InvalidOperationException)\n{\n    // WISP GIT lock failed (no tablet service / RDP session)\n    EnableMouseOnlyMode();\n}","handlingStrategy":"try-catch","validationCode":"// before creating WPF windows in constrained environments\nbool stylusLikelyAvailable =\n    Environment.UserInteractive &&\n    !IsRemoteSession() &&\n    ServiceRunning(\"TabletInputService\");","typeGuard":null,"tryCatchPattern":"try\n{\n    InitializeWpfWindow();\n}\ncatch (InvalidOperationException ex) when (IsWispLockFailure(ex))\n{\n    Log.Warn(\"WISP GIT lock failed; falling back to mouse-only input.\");\n    EnableMouseOnlyMode();\n}","preventionTips":["Verify TabletInputService (Tablet PC Input Service) is running before launching stylus-dependent features.","Detect remote-desktop sessions and disable real-time stylus features there.","Keep Windows and pen/digitizer drivers updated to keep WISP healthy.","Add a global handler that degrades to mouse input instead of crashing when WISP initialization fails."],"tags":["wpf","stylus","interop","windows"],"backgroundTag":"internal-invariant-violation","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"}