{"record":{"id":"5505fc32c4f59f3e","repo":"HandyOrg/HandyControl","slug":"statusexception-status-interopmethods","errorCode":null,"errorMessage":"StatusException(status)","messagePattern":"StatusException\\(status\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"src/Shared/HandyControl_Shared/Tools/Interop/InteropMethods.cs","lineNumber":590,"sourceCode":"        private readonly struct StartupOutput\n        {\n            private readonly IntPtr hook;\n\n            private readonly IntPtr unhook;\n        }\n\n        [ResourceExposure(ResourceScope.None)]\n        [ResourceConsumption(ResourceScope.AppDomain, ResourceScope.AppDomain)]\n        [SuppressMessage(\"Microsoft.Performance\", \"CA1804:RemoveUnusedLocals\")]\n        private static void Initialize()\n        {\n            var input = StartupInput.GetDefault();\n\n            var status = GdiplusStartup(out InitToken, ref input, out _);\n\n            if (status != Ok)\n            {\n                throw StatusException(status);\n            }\n\n            var currentDomain = AppDomain.CurrentDomain;\n            currentDomain.ProcessExit += OnProcessExit;\n\n            if (!currentDomain.IsDefaultAppDomain())\n            {\n                currentDomain.DomainUnload += OnProcessExit;\n            }\n        }\n\n        [PrePrepareMethod]\n        [ResourceExposure(ResourceScope.AppDomain)]\n        [ResourceConsumption(ResourceScope.AppDomain)]\n        private static void OnProcessExit(object sender, EventArgs e) => Shutdown();\n\n        [SuppressMessage(\"Microsoft.Reliability\", \"CA2001:AvoidCallingProblematicMethods\")]\n        [ResourceExposure(ResourceScope.AppDomain)]","sourceCodeStart":572,"sourceCodeEnd":608,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/HandyControl_Shared/Tools/Interop/InteropMethods.cs#L572-L608","documentation":"During lazy GDI+ startup in InteropMethods, GdiplusStartup returned a non-Ok status and StatusException is thrown. This means the GDI+ library could not be initialized for the process — no GDI+ drawing/codec APIs will work afterward.","triggerScenarios":"First GDI+ usage in the app domain triggers EnsureGdiplusInitialized; GdiplusStartup fails with statuses like NotImplemented (rare platforms) or OutOfMemory, or startup is attempted in an unsupported app domain hosting scenario.","commonSituations":"Unusual hosting environments (unit-test app domains, plugins, server environments where GDI+ is restricted), corrupted GDI+ install, or platform (Wine/trimmed Windows) lacking GDI+ support.","solutions":["Check the exact status code in the exception (NotInitialized/OutOfMemory/etc.) to pinpoint the failure.","Verify gdiplus.dll is present and healthy on the machine (sfc /scannow).","Avoid calling GDI+ APIs in non-default app domains or before the runtime is fully loaded; force initialization early in Main.","If running in a restricted environment, switch to a non-GDI+ imaging library (e.g. SkiaSharp, WIC).","Ensure StartupInput/InitToken handling matches the GDI+ version in use."],"exampleFix":"// before\nvar status = GdiplusStartup(out InitToken, ref input, out _);\nif (status != Ok)\n{\n    throw StatusException(status);\n}\n// after\nvar status = GdiplusStartup(out InitToken, ref input, out _);\nif (status != Ok)\n{\n    throw StatusException(status, $\"GDI+ startup failed (status={status}); ensure gdiplus.dll is available and the app domain supports GDI+\");\n}","handlingStrategy":"try-catch","validationCode":"if (!InteropMethods.Gdip.ApplicationStarted)\n{\n    try { InteropMethods.Gdip.EnsureGdiplusInitialized(); }\n    catch (StatusException ex) { logger.Fatal($\"GDI+ init failed: {ex.Status}\"); throw; }\n}","typeGuard":"bool CanUseGdiplus() => Environment.OSVersion.Platform == PlatformID.Win32NT && InteropMethods.Gdip.ApplicationStarted;","tryCatchPattern":"try\n{\n    InitializeGdiplusDependentFeature();\n}\ncatch (StatusException ex)\n{\n    logger.Fatal($\"GDI+ startup failed (status {ex.Status}); disabling imaging features.\");\n    featureEnabled = false;\n}","preventionTips":["Force GDI+ initialization early in Main so failures surface at startup, not mid-feature.","Avoid GDI+ usage in unit-test app domains or restricted hosts.","Have a non-GDI+ imaging fallback (WIC/SkiaSharp) for broken environments.","Health-check gdiplus.dll availability when deploying to new machines."],"tags":["gdiplus","initialization","native-interop"],"backgroundTag":"module-init-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"}