Handle exceptions in GetMonitorColorProfileFromWindow#4
Handle exceptions in GetMonitorColorProfileFromWindow#4Bananz0 wants to merge 1 commit intoQL-Win:masterfrom
Conversation
…uring desktop composition issues
There was a problem hiding this comment.
Pull request overview
This PR improves resilience of DisplayDeviceHelper.GetMonitorColorProfileFromWindow by handling exceptions that can occur during display reconfiguration scenarios (e.g., eGPU disconnect/reconnect), preventing crashes and returning null instead.
Changes:
- Wrap
GetMonitorColorProfileFromWindowlogic in atry/catch. - Add a specific
COMExceptionfilter for0x80263001(desktop composition disabled) with a targeted log message. - Add a general exception handler that logs and returns
null.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| catch (COMException ex) when (ex.HResult == unchecked((int)0x80263001)) | ||
| { | ||
| // Desktop composition is disabled (e.g., during eGPU reconnection) | ||
| ProcessHelper.WriteLog("Failed to get color profile: Desktop composition is disabled. This is expected during display reconfiguration."); | ||
| return null; |
There was a problem hiding this comment.
The HRESULT check uses a magic number and ex.HResult. For readability and to make the intent explicit (DWM_E_COMPOSITIONDISABLED = 0x80263001), consider introducing a named constant (or enum) and using COMException.ErrorCode in the filter instead of HResult.
| return GetMonitorColorProfile(hMonitor); | ||
| try | ||
| { | ||
| var hMonitor = MonitorFromWindow(new WindowInteropHelper(window).EnsureHandle(), MonitorDefaults.TONEAREST); |
There was a problem hiding this comment.
Replace this call with a call to managed code if possible.
I have a laptop with an eGPU and have this issue when the egpu is unplugged and replugged in.