|
| 1 | +#include "pch.h" |
| 2 | + |
| 3 | +#include "InteropHResult.h" |
| 4 | + |
| 5 | +#include <windows.h> |
| 6 | + |
| 7 | +using namespace System::ComponentModel; |
| 8 | +using namespace System::Runtime::InteropServices; |
| 9 | +using namespace System::Threading; |
| 10 | +using namespace System; |
| 11 | +using namespace WebRtcNet::Logging; |
| 12 | + |
| 13 | +namespace WebRtcInterop |
| 14 | +{ |
| 15 | + namespace |
| 16 | + { |
| 17 | + String^ TrimSystemMessage(String^ message) |
| 18 | + { |
| 19 | + if (String::IsNullOrWhiteSpace(message)) |
| 20 | + return "Unknown system error"; |
| 21 | + |
| 22 | + return message->Trim(); |
| 23 | + } |
| 24 | + |
| 25 | + String^ FormatSystemMessage(const HRESULT hr) |
| 26 | + { |
| 27 | + LPWSTR rawMessage = nullptr; |
| 28 | + const auto flags = |
| 29 | + FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 30 | + FORMAT_MESSAGE_FROM_SYSTEM | |
| 31 | + FORMAT_MESSAGE_IGNORE_INSERTS; |
| 32 | + auto messageId = static_cast<DWORD>(hr); |
| 33 | + auto length = FormatMessageW( |
| 34 | + flags, |
| 35 | + nullptr, |
| 36 | + messageId, |
| 37 | + 0, |
| 38 | + reinterpret_cast<LPWSTR>(&rawMessage), |
| 39 | + 0, |
| 40 | + nullptr); |
| 41 | + |
| 42 | + if (length == 0 && HRESULT_FACILITY(hr) == FACILITY_WIN32) |
| 43 | + { |
| 44 | + messageId = HRESULT_CODE(hr); |
| 45 | + length = FormatMessageW( |
| 46 | + flags, |
| 47 | + nullptr, |
| 48 | + messageId, |
| 49 | + 0, |
| 50 | + reinterpret_cast<LPWSTR>(&rawMessage), |
| 51 | + 0, |
| 52 | + nullptr); |
| 53 | + } |
| 54 | + |
| 55 | + if (length == 0 || rawMessage == nullptr) |
| 56 | + return "Unknown system error"; |
| 57 | + |
| 58 | + try |
| 59 | + { |
| 60 | + return TrimSystemMessage(gcnew String(rawMessage)); |
| 61 | + } |
| 62 | + finally |
| 63 | + { |
| 64 | + LocalFree(rawMessage); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + void InteropHResult::ThrowIfFailed(HRESULT hr, String^ message) |
| 70 | + { |
| 71 | + if (SUCCEEDED(hr)) |
| 72 | + return; |
| 73 | + |
| 74 | + if (HRESULT_FACILITY(hr) == FACILITY_WIN32) |
| 75 | + throw gcnew Win32Exception(HRESULT_CODE(hr), message); |
| 76 | + |
| 77 | + throw gcnew COMException(message, hr); |
| 78 | + } |
| 79 | + |
| 80 | + bool InteropHResult::LogIfFailed(HRESULT hr, String^ operation, String^ category) |
| 81 | + { |
| 82 | + if (SUCCEEDED(hr)) |
| 83 | + return false; |
| 84 | + |
| 85 | + if (String::IsNullOrWhiteSpace(operation)) |
| 86 | + operation = "Interop operation"; |
| 87 | + if (String::IsNullOrWhiteSpace(category)) |
| 88 | + category = "Interop.HResult"; |
| 89 | + |
| 90 | + try |
| 91 | + { |
| 92 | + const auto formatted = String::Format( |
| 93 | + "{0} failed. HRESULT=0x{1:X8} ({2}), Facility={3}, Code={4}, SystemMessage=\"{5}\"", |
| 94 | + operation, |
| 95 | + static_cast<UInt32>(hr), |
| 96 | + hr, |
| 97 | + HRESULT_FACILITY(hr), |
| 98 | + HRESULT_CODE(hr), |
| 99 | + FormatSystemMessage(hr)); |
| 100 | + |
| 101 | + WebRtcLogWriterBridge::WriteInteropLog( |
| 102 | + 4, |
| 103 | + static_cast<int>(WebRtcLogEventId::InteropHResultFailure), |
| 104 | + category, |
| 105 | + Thread::CurrentThread->ManagedThreadId, |
| 106 | + formatted); |
| 107 | + } |
| 108 | + catch (...) |
| 109 | + { |
| 110 | + } |
| 111 | + |
| 112 | + return true; |
| 113 | + } |
| 114 | +} |
0 commit comments