{"record":{"id":"0dd7b66e0d6ccef0","repo":"unoplatform/uno","slug":"wglcreatecontext-failed","errorCode":null,"errorMessage":"wglCreateContext failed","messagePattern":"wglCreateContext failed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/AddIns/Uno.WinUI.Graphics3DGL/Windows/WinUINativeOpenGLWrapper.cs","lineNumber":75,"sourceCode":"\t\t// To inspect the chosen pixel format:\n\t\t// WindowsRenderingNativeMethods.PIXELFORMATDESCRIPTOR temp_pfd = default;\n\t\t// WindowsRenderingNativeMethods.DescribePixelFormat(_hdc, _pixelFormat, (uint)Marshal.SizeOf<WindowsRenderingNativeMethods.PIXELFORMATDESCRIPTOR>(), ref temp_pfd);\n\n\t\tif (pixelFormat == 0)\n\t\t{\n\t\t\tthrow new InvalidOperationException(\"ChoosePixelFormat failed\");\n\t\t}\n\n\t\tif (WindowsRenderingNativeMethods.SetPixelFormat(_hdc, pixelFormat, ref pfd) == 0)\n\t\t{\n\t\t\tthrow new InvalidOperationException(\"SetPixelFormat failed\");\n\t\t}\n\n\t\t_glContext = WindowsRenderingNativeMethods.wglCreateContext(_hdc);\n\n\t\tif (_glContext == IntPtr.Zero)\n\t\t{\n\t\t\tthrow new InvalidOperationException(\"wglCreateContext failed\");\n\t\t}\n\t}\n\n\t// https://sharovarskyi.com/blog/posts/csharp-win32-opengl-silknet/\n\tpublic bool TryGetProcAddress(string proc, out nint addr)\n\t{\n\t\tif (_opengl32.Value != IntPtr.Zero && NativeLibrary.TryGetExport(_opengl32.Value, proc, out addr))\n\t\t{\n\t\t\treturn true;\n\t\t}\n\n\t\taddr = WindowsRenderingNativeMethods.wglGetProcAddress(proc);\n\t\treturn addr != IntPtr.Zero;\n\t}\n\n\tpublic nint GetProcAddress(string proc)\n\t{\n\t\tif (TryGetProcAddress(proc, out var address))","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/AddIns/Uno.WinUI.Graphics3DGL/Windows/WinUINativeOpenGLWrapper.cs#L57-L93","documentation":"After the pixel format is set, wglCreateContext(_hdc) creates the GL rendering context; it returns NULL on failure and the wrapper throws InvalidOperationException. wglCreateContext fails when the HDC has no valid pixel format set, the driver cannot allocate a context (out of GPU memory), the GDI/DLL opengl32 is unavailable, or the application is in a session type (e.g. certain service/RDP contexts) that disallows GL context creation.","triggerScenarios":"HDC's pixel format not actually set (so [53] should have thrown earlier but didn't, e.g. custom code path); GPU memory exhausted; opengl32.dll missing/unloaded; session has no interactive desktop.","commonSituations":"Too many live GL contexts exhausting GPU handles; corrupt/missing GPU driver; running the GL host in a non-interactive session; running on a server with no GPU and a software GL stack that rejects context creation.","solutions":["Guarantee ChoosePixelFormat + SetPixelFormat succeeded on the same HDC before wglCreateContext (chain the checks).","Reduce the number of simultaneously live GL contexts to free GPU handles.","Install/repair GPU drivers and opengl32.dll; verify the session is an interactive desktop.","Call Marshal.GetLastWin32Error / GetLastError for the specific failure reason and surface it."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (_glContext == IntPtr.Zero)\n{\n\tint err = System.Runtime.InteropServices.Marshal.GetLastWin32Error();\n\tthrow new InvalidOperationException($\"wglCreateContext failed (Win32 {err})\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Chain the checks: pixel format first, then wglCreateContext on the same DC.","Limit the number of simultaneously live GL contexts.","Run GL hosts on an interactive desktop with working GPU drivers."],"tags":["opengl","windows","wgl","native-interop","context"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}