зеркало из https://github.com/microsoft/cppwinrt.git
Add a hook to throw_hresult for integration with WIL (#843)
This commit is contained in:
Родитель
9ed46e2fe8
Коммит
e439dcc8cf
|
@ -426,6 +426,11 @@ WINRT_EXPORT namespace winrt
|
|||
|
||||
[[noreturn]] inline __declspec(noinline) void throw_hresult(hresult const result)
|
||||
{
|
||||
if (winrt_throw_hresult_handler)
|
||||
{
|
||||
winrt_throw_hresult_handler(0, nullptr, nullptr, _ReturnAddress(), result);
|
||||
}
|
||||
|
||||
if (result == impl::error_bad_alloc)
|
||||
{
|
||||
throw std::bad_alloc();
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
__declspec(selectany) int32_t(__stdcall* winrt_to_hresult_handler)(void* address) noexcept {};
|
||||
__declspec(selectany) void(__stdcall* winrt_throw_hresult_handler)(uint32_t lineNumber, char const* fileName, char const* functionName, void* returnAddress, winrt::hresult const result) noexcept {};
|
||||
__declspec(selectany) void(__stdcall* winrt_suspend_handler)(void const* token) noexcept {};
|
||||
__declspec(selectany) void(__stdcall* winrt_resume_handler)(void const* token) noexcept {};
|
||||
__declspec(selectany) int32_t(__stdcall* winrt_activation_handler)(void* classId, winrt::guid const& iid, void** factory) noexcept {};
|
||||
|
|
|
@ -36,6 +36,17 @@ namespace
|
|||
REQUIRE(false);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool s_loggerCalled = false;
|
||||
|
||||
void __stdcall logger(uint32_t lineNumber, char const* fileName, char const* functionName, void* returnAddress, winrt::hresult const result) noexcept
|
||||
{
|
||||
lineNumber; fileName; functionName;
|
||||
REQUIRE(returnAddress);
|
||||
REQUIRE(result == 0x80000018); // E_ILLEGAL_DELEGATE_ASSIGNMENT)
|
||||
s_loggerCalled = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("custom_error")
|
||||
|
@ -50,3 +61,19 @@ TEST_CASE("custom_error")
|
|||
// Remove global handler
|
||||
winrt_to_hresult_handler = nullptr;
|
||||
}
|
||||
|
||||
TEST_CASE("custom_error_logger")
|
||||
{
|
||||
// Set up global handler
|
||||
REQUIRE(!s_loggerCalled);
|
||||
REQUIRE(!winrt_throw_hresult_handler);
|
||||
winrt_throw_hresult_handler = logger;
|
||||
|
||||
// Validate that handler translated exception
|
||||
REQUIRE_THROWS_AS(check_hresult(0x80000018), hresult_illegal_delegate_assignment);
|
||||
REQUIRE(s_loggerCalled);
|
||||
|
||||
// Remove global handler
|
||||
winrt_throw_hresult_handler = nullptr;
|
||||
s_loggerCalled = false;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче