HandyOrg/HandyControl · error · SystemException

Unable to create a device context from the specified device…

Error message

Unable to create a device context from the specified device information.

What it means

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.

Solutions

  1. Verify the device/driver name string is valid and the device still exists (e.g. via EnumPrinters)
  2. Pass null device name to get the screen DC if that is what is intended
  3. Reinstall/update the printer or display driver
  4. Catch SystemException and fall back to a default DC

Example fix

// before
var dc = SafeDC.CreateDC(null, stalePrinterName, null, null); // SystemException
// after
string device = PrinterExists(stalePrinterName) ? stalePrinterName : null;
var dc = SafeDC.CreateDC(null, device, null, null);
Defensive patterns

Strategy: fallback

Validate before calling

static bool DeviceExists(string deviceName) =>
    string.IsNullOrEmpty(deviceName) || EnumPrinters().Any(p => p.Name == deviceName);

Type guard

static bool IsValidDc(SafeDC dc) => dc != null && !dc.IsInvalid;

Try / catch

try { dc = SafeDC.CreateDC(null, deviceName, null, null); }
catch (SystemException ex) { Log.Warn("CreateDC failed for " + deviceName, ex); dc = SafeDC.GetDC(IntPtr.Zero); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.

Related errors


AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14). Data as JSON: /api/errors/ff2773d23f2f4f40. Report an issue: GitHub.

Appendix: source

Thrown at src/Shared/Microsoft.Windows.Shell/Standard/SafeDC.cs:52

    [SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")]
    public static SafeDC CreateDC(string deviceName)
    {
        SafeDC safeDC = null;
        try
        {
            safeDC = SafeDC.NativeMethods.CreateDC(deviceName, null, IntPtr.Zero, IntPtr.Zero);
        }
        finally
        {
            if (safeDC != null)
            {
                safeDC._created = true;
            }
        }
        if (safeDC.IsInvalid)
        {
            safeDC.Dispose();
            throw new SystemException("Unable to create a device context from the specified device information.");
        }
        return safeDC;
    }

    [SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")]
    [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
    public static SafeDC CreateCompatibleDC(SafeDC hdc)
    {
        SafeDC safeDC = null;
        try
        {
            IntPtr hdc2 = IntPtr.Zero;
            if (hdc != null)
            {
                hdc2 = hdc.handle;
            }
            safeDC = SafeDC.NativeMethods.CreateCompatibleDC(hdc2);
            if (safeDC == null)

View on GitHub (pinned to 2c0875ebd6)