Skip to content

Commit e439dcc

Browse files
authored
Add a hook to throw_hresult for integration with WIL (#843)
1 parent 9ed46e2 commit e439dcc

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

strings/base_error.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,11 @@ WINRT_EXPORT namespace winrt
426426

427427
[[noreturn]] inline __declspec(noinline) void throw_hresult(hresult const result)
428428
{
429+
if (winrt_throw_hresult_handler)
430+
{
431+
winrt_throw_hresult_handler(0, nullptr, nullptr, _ReturnAddress(), result);
432+
}
433+
429434
if (result == impl::error_bad_alloc)
430435
{
431436
throw std::bad_alloc();

strings/base_extern.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
__declspec(selectany) int32_t(__stdcall* winrt_to_hresult_handler)(void* address) noexcept {};
3+
__declspec(selectany) void(__stdcall* winrt_throw_hresult_handler)(uint32_t lineNumber, char const* fileName, char const* functionName, void* returnAddress, winrt::hresult const result) noexcept {};
34
__declspec(selectany) void(__stdcall* winrt_suspend_handler)(void const* token) noexcept {};
45
__declspec(selectany) void(__stdcall* winrt_resume_handler)(void const* token) noexcept {};
56
__declspec(selectany) int32_t(__stdcall* winrt_activation_handler)(void* classId, winrt::guid const& iid, void** factory) noexcept {};

test/test/custom_error.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ namespace
3636
REQUIRE(false);
3737
return 0;
3838
}
39+
40+
static bool s_loggerCalled = false;
41+
42+
void __stdcall logger(uint32_t lineNumber, char const* fileName, char const* functionName, void* returnAddress, winrt::hresult const result) noexcept
43+
{
44+
lineNumber; fileName; functionName;
45+
REQUIRE(returnAddress);
46+
REQUIRE(result == 0x80000018); // E_ILLEGAL_DELEGATE_ASSIGNMENT)
47+
s_loggerCalled = true;
48+
}
49+
3950
}
4051

4152
TEST_CASE("custom_error")
@@ -50,3 +61,19 @@ TEST_CASE("custom_error")
5061
// Remove global handler
5162
winrt_to_hresult_handler = nullptr;
5263
}
64+
65+
TEST_CASE("custom_error_logger")
66+
{
67+
// Set up global handler
68+
REQUIRE(!s_loggerCalled);
69+
REQUIRE(!winrt_throw_hresult_handler);
70+
winrt_throw_hresult_handler = logger;
71+
72+
// Validate that handler translated exception
73+
REQUIRE_THROWS_AS(check_hresult(0x80000018), hresult_illegal_delegate_assignment);
74+
REQUIRE(s_loggerCalled);
75+
76+
// Remove global handler
77+
winrt_throw_hresult_handler = nullptr;
78+
s_loggerCalled = false;
79+
}

0 commit comments

Comments
 (0)