CsWinRT icon indicating copy to clipboard operation
CsWinRT copied to clipboard

Bug: COMException often has empty error message

Open whiskhub opened this issue 11 months ago • 0 comments

Description

COMExceptions thrown by WinRT code often times only have an empty error message. This greatly impacts the developer experience, especially for beginners. To understand the error, the HResult must be researched manually, which can be confusing if you don't know what it means.

The easiest to reproduce with is when setting some WinUI component value on the wrong thread (RPC_E_WRONG_THREAD -2147417842)

Image

I assume it is a CsWinRT bug, because the error message (The application called an interface that was marshalled for a different thread.) is correctly shown in C++ (also in the IRestrictedErrorInfo) (C++WinRT): Image

Steps To Reproduce

  1. Have a WinUI 3 C# app
  2. Access a UI component from another thread, e.g.:
await Task.Run(() =>
{
    try
    {
        myButton.Content = "WRONG THREAD";
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }
});
  1. Check the exception message.

Expected Behavior

The COMException message should contain a message.

It should be the same message as in the C++WinRT exception.

If there is no specific exception message set, it would be nice if CsWinRT could at also generate a message based on the HResult code as a fallback.

Version Info

CsWinRT 2.2.0 Windows App SDK 1.6 or 1.7 .NET 8/9

Additional Context

No response

whiskhub avatar Feb 07 '25 17:02 whiskhub