{"record":{"id":"f37e0ced0da72462","repo":"dotnet/maui","slug":"unable-to-create-a-device-context-from-the-specifi","errorCode":null,"errorMessage":"Unable to create a device context from the specified device information.","messagePattern":"Unable to create a device context from the specified device information\\.","errorType":"exception","errorClass":"SystemException","httpStatus":null,"severity":"critical","filePath":"src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/Standard/NativeMethods.cs","lineNumber":1488,"sourceCode":"\t\t{\n\t\t\tSafeDC dc = null;\n\t\t\ttry\n\t\t\t{\n\t\t\t\t// Should this really be on the driver parameter?\n\t\t\t\tdc = NativeMethods.CreateDC(deviceName, null, IntPtr.Zero, IntPtr.Zero);\n\t\t\t}\n\t\t\tfinally\n\t\t\t{\n\t\t\t\tif (dc != null)\n\t\t\t\t{\n\t\t\t\t\tdc._created = true;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (dc.IsInvalid)\n\t\t\t{\n\t\t\t\tdc.Dispose();\n\t\t\t\tthrow new SystemException(\"Unable to create a device context from the specified device information.\");\n\t\t\t}\n\n\t\t\treturn dc;\n\t\t}\n\n\t\t[SuppressMessage(\"Microsoft.Usage\", \"CA2201:DoNotRaiseReservedExceptionTypes\"), SuppressMessage(\"Microsoft.Performance\", \"CA1811:AvoidUncalledPrivateCode\")]\n\t\tpublic static SafeDC CreateCompatibleDC(SafeDC hdc)\n\t\t{\n\t\t\tSafeDC dc = null;\n\t\t\ttry\n\t\t\t{\n\t\t\t\tIntPtr hPtr = IntPtr.Zero;\n\t\t\t\tif (hdc != null)\n\t\t\t\t{\n\t\t\t\t\thPtr = hdc.handle;\n\t\t\t\t}\n\t\t\t\tdc = NativeMethods.CreateCompatibleDC(hPtr);\n\t\t\t\tif (dc == null)","sourceCodeStart":1470,"sourceCodeEnd":1506,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/Standard/NativeMethods.cs#L1470-L1506","documentation":"NativeMethods.CreateDeviceContext (CreateIC path) throws SystemException(\"Unable to create a device context from the specified device information.\") when the resulting SafeDC handle is invalid after CreateDC/CreateIC. The shell's standard interop wrapper treats an invalid GDI device-context handle as fatal because subsequent rendering/metrics calls would all fail.","triggerScenarios":"Calling CreateDC/CreateIC with a device name or DEVMODE that the system rejects; running under a session with no display (services, RDP disconnect, headless CI); GDI handle exhaustion; corrupted display driver state.","commonSituations":"Headless CI/service context where no interactive desktop exists; invalid printer/display device name; GDI resource leak exhausting the session handle pool; remote session teardown mid-call.","solutions":["Run the code on an interactive desktop session (or mock the device-context dependency in CI).","Validate the device name (lpszDevice) before calling CreateIC; fall back to DISPLAY or null.","Monitor GDI handle count and release SafeDC handles promptly to avoid exhaustion.","For services, run the rendering in a session-0 interactive process or use a virtual display."],"exampleFix":"// before\nusing (var dc = NativeMethods.CreateIC(\"BadDevice\", null, null, null)) { ... }\n\n// after\nvar device = IsValidDeviceName(\"BadDevice\") ? \"BadDevice\" : null;\nusing (var dc = NativeMethods.CreateIC(device, null, null, null)) { ... }","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(device) || !IsKnownDevice(device)) device = null;\nusing var dc = NativeMethods.CreateIC(device, null, null, null);","typeGuard":"static bool HasInteractiveDesktop =>\n    System.Windows.Application.Current != null &&\n    !System.Windows.Application.Current.Dispatcher.HasShutdownStarted;","tryCatchPattern":"try { using var dc = NativeMethods.CreateIC(device, null, null, null); }\ncatch (SystemException ex) when (ex.Message.Contains(\"device context\"))\n{ /* fall back to GetDC(IntPtr.Zero) or report headless env */ }","preventionTips":["Run on an interactive desktop session; mock GDI in CI.","Validate device names before CreateIC; fall back to DISPLAY.","Dispose SafeDC promptly to avoid handle exhaustion."],"tags":["wpf","gdi","native-interop","device-context","systemexception"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}