{"record":{"id":"ff2773d23f2f4f40","repo":"HandyOrg/HandyControl","slug":"unable-to-create-a-device-context-from-the-specified-device","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":"error","filePath":"src/Shared/Microsoft.Windows.Shell/Standard/SafeDC.cs","lineNumber":52,"sourceCode":"    [SuppressMessage(\"Microsoft.Usage\", \"CA2201:DoNotRaiseReservedExceptionTypes\")]\n    public static SafeDC CreateDC(string deviceName)\n    {\n        SafeDC safeDC = null;\n        try\n        {\n            safeDC = SafeDC.NativeMethods.CreateDC(deviceName, null, IntPtr.Zero, IntPtr.Zero);\n        }\n        finally\n        {\n            if (safeDC != null)\n            {\n                safeDC._created = true;\n            }\n        }\n        if (safeDC.IsInvalid)\n        {\n            safeDC.Dispose();\n            throw new SystemException(\"Unable to create a device context from the specified device information.\");\n        }\n        return safeDC;\n    }\n\n    [SuppressMessage(\"Microsoft.Usage\", \"CA2201:DoNotRaiseReservedExceptionTypes\")]\n    [SuppressMessage(\"Microsoft.Performance\", \"CA1811:AvoidUncalledPrivateCode\")]\n    public static SafeDC CreateCompatibleDC(SafeDC hdc)\n    {\n        SafeDC safeDC = null;\n        try\n        {\n            IntPtr hdc2 = IntPtr.Zero;\n            if (hdc != null)\n            {\n                hdc2 = hdc.handle;\n            }\n            safeDC = SafeDC.NativeMethods.CreateCompatibleDC(hdc2);\n            if (safeDC == null)","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/Microsoft.Windows.Shell/Standard/SafeDC.cs#L34-L70","documentation":"Guard in SafeDC.CreateDC: the Win32 CreateDC call returned a null/invalid device context handle for the given deviceName. Although thrown as SystemException, it is a sentinel wrapper meaning the GDI call itself failed — the device name is misspelled/null, the device/display is not available, or GDI resources are exhausted. The failing input is the deviceName string passed through to NativeMethods.CreateDC.","triggerScenarios":"Calling SafeDC.CreateDC with a driver/device name, or device-information string the GDI subsystem cannot resolve — e.g. an invalid printer name or unsupported device string.","commonSituations":"Querying printer/screen DCs where the printer driver is missing or the device string is stale (e.g. after a printer was removed); passing lpszDevice values copied from outdated configuration.","solutions":["Verify the device/driver name string is valid and the device still exists (e.g. via EnumPrinters)","Pass null device name to get the screen DC if that is what is intended","Reinstall/update the printer or display driver","Catch SystemException and fall back to a default DC"],"exampleFix":"// before\nvar dc = SafeDC.CreateDC(null, stalePrinterName, null, null); // SystemException\n// after\nstring device = PrinterExists(stalePrinterName) ? stalePrinterName : null;\nvar dc = SafeDC.CreateDC(null, device, null, null);","handlingStrategy":"fallback","validationCode":"static bool DeviceExists(string deviceName) =>\n    string.IsNullOrEmpty(deviceName) || EnumPrinters().Any(p => p.Name == deviceName);","typeGuard":"static bool IsValidDc(SafeDC dc) => dc != null && !dc.IsInvalid;","tryCatchPattern":"try { dc = SafeDC.CreateDC(null, deviceName, null, null); }\ncatch (SystemException ex) { Log.Warn(\"CreateDC failed for \" + deviceName, ex); dc = SafeDC.GetDC(IntPtr.Zero); }","preventionTips":["Validate printer/device names against EnumPrinters before CreateDC","Use null device name when you only need the screen DC","Keep printer configuration synchronized with installed devices"],"tags":["gdi","device-context","win32","unmanaged"],"backgroundTag":"file-open-failed","analyzedSha":"2c0875ebd67326e0c67282967e3e809c69282fee","analyzedAt":"2026-09-14T14:45:29.754Z","contentChangedAt":"2026-09-14T14:45:29.754Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}