/ tools / MonitorReportTool / ErrorMessage.h
ErrorMessage.h
 1  #pragma once
 2  
 3  #include <optional>
 4  #include <string>
 5  #include <system_error>
 6  #include <Windows.h>
 7  
 8  inline std::optional<std::wstring> get_last_error_message(const DWORD dw)
 9  {
10      std::optional<std::wstring> message;
11      try
12      {
13          const auto msg = std::system_category().message(dw);
14          message.emplace(begin(msg), end(msg));
15      }
16      catch (...)
17      {
18      }
19      return message;
20  }
21  
22  inline std::wstring get_last_error_or_default(const DWORD dw)
23  {
24      auto message = get_last_error_message(dw);
25      return message.has_value() ? message.value() : L"";
26  }