Skip to content

cppwinrt.h: Improve conversion of error information #495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
arthuraraujo-msft opened this issue Jan 7, 2025 · 1 comment
Open

cppwinrt.h: Improve conversion of error information #495

arthuraraujo-msft opened this issue Jan 7, 2025 · 1 comment

Comments

@arthuraraujo-msft
Copy link

arthuraraujo-msft commented Jan 7, 2025

When using WIL with C++/WinRT with the cppwinrt.h header, the winrt_to_hresult_handler global handler is hooked from WIL directly into the C++/WinRT codebase.

The handler allows WIL exceptions to be understood by C++/WinRT and their HRESULTs extracted, but messages are lost in the way when going over the API boundaries because only the HRESULT is converted in winrt::to_hresult().

C++/WinRT makes use of Get and SetErrorInfo() to recover the messages when re-creating the exceptions on the caller side. The set is done inside winrt::hresult_error::to_abi() in winrt::to_hresult() on the server side, but only when winrt_to_hresult_handler is not hooked. The retrieval happens when re-creating the winrt::hresult_error on the caller.

If WIL were to call the to_abi() method in the WIL hook for winrt_to_hresult_handler, it would properly trigger the storage of the message and later on it could be retrieved when C++/WinRT creates a new winrt::hresult_error on the caller side.

@arthuraraujo-msft
Copy link
Author

          Correct me if I'm wrong, but you seem to effectively be using C++/WinRT as a way to call `RoOriginateError`? There's already a header for that: [result_originate.h](https://github.com/microsoft/wil/blob/master/include/wil/result_originate.h).

Originally posted by @dunhor in #498 (comment)

Duncan's suggestion resolves this issue. I hadn't seen this before anywhere. Perhaps this could be added to the docs? Especially the C++/WinRT integration docs on the WIL wiki.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant