{"record":{"id":"da91178486de14e4","repo":"memstechtips/Winhance","slug":"dism-operation-failed-with-hresult-0x-hr-x8","errorCode":null,"errorMessage":"DISM {operation} failed with HRESULT 0x{hr:X8}","messagePattern":"DISM (.+?) failed with HRESULT 0x(.+?)","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"src/Winhance.Core/Features/Common/Native/DismApi.cs","lineNumber":166,"sourceCode":"\r\n    // --- Helpers ---\r\n\r\n    public static T[] MarshalArray<T>(IntPtr ptr, uint count) where T : struct\r\n    {\r\n        var result = new T[count];\r\n        var structSize = Marshal.SizeOf<T>();\r\n        for (uint i = 0; i < count; i++)\r\n        {\r\n            result[i] = Marshal.PtrToStructure<T>(ptr + (int)(i * structSize));\r\n        }\r\n        return result;\r\n    }\r\n\r\n    public static void ThrowIfFailed(int hr, string operation)\r\n    {\r\n        if (hr < 0)\r\n        {\r\n            throw new Win32Exception(hr, $\"DISM {operation} failed with HRESULT 0x{hr:X8}\");\r\n        }\r\n    }\r\n}\r\n","sourceCodeStart":148,"sourceCodeEnd":170,"githubUrl":"https://github.com/memstechtips/Winhance/blob/f23d554eb2d6400b1827bcc46b91294ffa53fbc9/src/Winhance.Core/Features/Common/Native/DismApi.cs#L148-L170","documentation":"ThrowIfFailed wraps native DISM P/Invoke results: if the returned HRESULT hr is negative (high bit set), it throws System.ComponentModel.Win32Exception with that HRESULT as the native error code. This is the standard pattern for surfacing native DISM failures (Initialize, OpenSession, GetImageInfo, etc.) into managed exceptions.","triggerScenarios":"Any DismApi call that returns an HRESULT passes through ThrowIfFailed: DismInitialize, DismOpenSession, DismGetImageInfo, DismCloseSession, DismShutdown. A negative HRESULT means the DISM servicing engine rejected the call — e.g. 0x80070002 (file not found), 0x80070005 (access denied), 0xC1420115 (image not mounted), 0x800F0906 (source missing).","commonSituations":"The image path passed to DismOpenSession does not exist or is not a valid WIM/VHD. The process is not elevated. The image is already being serviced by another DISM session. The native DISM binaries (dismapi.dll) are missing or wrong version for the OS. A mounted image was unmounted externally.","solutions":["Decode the HRESULT: 0x8007xxxx maps to Win32 GetLastError — look up the low 16 bits. 0x800F0xxx and 0xC1420xxx are CBS/DISM-specific.","Verify the image path exists and is a valid WIM before opening a session (use DismGetImageInfo to probe).","Run elevated — most DISM servicing calls require administrator privileges.","Ensure no other DISM session or dism.exe process is servicing the same image.","On Windows, run `sfc /scannow` and `DISM /Online /Cleanup-Image /RestoreHealth` to repair the DISM/CBS stack if HRESULTs are consistently corrupt-state."],"exampleFix":"// before\npublic static void ThrowIfFailed(int hr, string operation)\n{\n    if (hr < 0)\n        throw new Win32Exception(hr, $\"DISM {operation} failed with HRESULT 0x{hr:X8}\");\n}\n\n// after: translate known DISM HRESULTs into actionable messages\npublic static void ThrowIfFailed(int hr, string operation)\n{\n    if (hr >= 0) return;\n    var msg = hr switch\n    {\n        unchecked((int)0x80070002) => $\"DISM {operation}: file not found (0x{hr:X8}). Check the image path.\",\n        unchecked((int)0x80070005) => $\"DISM {operation}: access denied (0x{hr:X8}). Run as administrator.\",\n        unchecked((int)0xC1420115) => $\"DISM {operation}: image not mounted/available (0x{hr:X8}).\",\n        _ => $\"DISM {operation} failed with HRESULT 0x{hr:X8}\"\n    };\n    throw new Win32Exception(hr, msg);\n}","handlingStrategy":"try-catch","validationCode":"// Validate the image path exists and is a recognizable WIM before opening a DISM session.\nbool IsImageOpenable(string path) => _fileSystemService.FileExists(path);\n// Then call DismApi.DismGetImageInfo to confirm it is a valid WIM before DismOpenSession.","typeGuard":null,"tryCatchPattern":"catch (System.ComponentModel.Win32Exception ex) when (ex.Message.Contains(\"DISM\") && ex.Message.Contains(\"HRESULT\"))\n{\n    // Decode the NativeErrorCode; map 0x80070002 -> file-not-found, 0x80070005 -> needs elevation, etc.\n}","preventionTips":["Probe images with DismGetImageInfo before OpenSession to fail fast with a clear message.","Run elevated for DISM servicing operations.","Repair the servicing stack (sfc + DISM /RestoreHealth) when HRESULTs indicate CBS corruption."],"tags":["dism","native","interop","hresult","windows"],"backgroundTag":null,"analyzedSha":"f23d554eb2d6400b1827bcc46b91294ffa53fbc9","analyzedAt":"2026-08-13T17:55:20.922Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}