{"record":{"id":"4696c3a8439c8c96","repo":"dotnet/wpf","slug":"win32exception-fontcacheutil","errorCode":null,"errorMessage":"Win32Exception","messagePattern":"Win32Exception","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs","lineNumber":357,"sourceCode":"        internal static int Dpi\n        {\n            get\n            {\n                if (!_dpiInitialized)\n                {\n                    lock (_dpiLock)\n                    {\n                        if (!_dpiInitialized)\n                        {\n                            HandleRef desktopWnd = new HandleRef(null, IntPtr.Zero);\n\n                            // Win32Exception will get the Win32 error code so we don't have to\n                            IntPtr dc = MS.Win32.UnsafeNativeMethods.GetDC(desktopWnd);\n\n                            // Detecting error case from unmanaged call, required by PREsharp to throw a Win32Exception\n                            if (dc == IntPtr.Zero)\n                            {\n                                throw new Win32Exception();\n                            }\n\n                            try\n                            {\n                                _dpi = MS.Win32.UnsafeNativeMethods.GetDeviceCaps(new HandleRef(null, dc), NativeMethods.LOGPIXELSY);\n                                _dpiInitialized = true;\n                            }\n                            finally\n                            {\n                                MS.Win32.UnsafeNativeMethods.ReleaseDC(desktopWnd, new HandleRef(null, dc));\n                            }\n                        }\n                    }\n                }\n                return _dpi;\n            }\n        }\n","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs#L339-L375","documentation":"During font-cache initialization, Util obtains a device context for the desktop window via GetDC to read the system DPI (LOGPIXELSY). If GetDC returns IntPtr.Zero it throws Win32Exception, signaling the Win32 display/GDI subsystem failed to provide a DC.","triggerScenarios":"Util.GetSystemDPI (first DPI access) calling GetDC(desktopWnd) when no desktop window handle is available or GDI resources are exhausted, yielding a null DC.","commonSituations":"Running WPF in a session without an interactive desktop (e.g. a Windows service or session-0 context); running under a headless CI agent; GDI handle exhaustion; calling font APIs before the window station/desktop is attached.","solutions":["Run the application in an interactive user session with an available desktop (not session 0).","If running as a service/CI, ensure the process runs under a logged-in user account with a desktop, or use a headless-safe rendering path.","Check GDI handle usage; fix handle leaks so GetDC can allocate.","Delay/avoid font-cache-touching APIs until the app has a valid HWND/desktop association."],"exampleFix":"// before (service context)\n// FontCache utilization happens on startup -> Win32Exception\n// after: run rendering work only after the service has an interactive session,\n// or guard:\nif (Environment.UserInteractive)\n    StartWpfFontDependentWork();\nelse\n    Log(\"WPF font APIs require an interactive desktop session\");","handlingStrategy":"try-catch","validationCode":"if (!Environment.UserInteractive)\n    throw new InvalidOperationException(\"WPF font APIs require an interactive desktop session.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    StartFontDependentWork();\n}\ncatch (Win32Exception ex)\n{\n    Log($\"GDI/desktop unavailable for font cache init: {ex.Message}\");\n    // run in interactive session or defer work\n}","preventionTips":["Do not run WPF rendering/font code in session 0 or headless services.","Monitor and avoid GDI handle leaks.","Initialize font-dependent work only after the process has a window/desktop association."],"tags":["win32","gdi","font-cache","headless","wpf"],"backgroundTag":"unsupported-platform","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}