win32_exception.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. #include "win32_exception.hpp"
  2. #include "../resource_wrapper.hpp"
  3. #include "../resource_traits/win32/local_alloc.hpp"
  4. #include "../cp_converter.hpp"
  5. namespace nkg::exceptions {
  6. win32_exception::win32_exception(std::string_view file, int line, error_code_t win32_error_code, std::string_view message) noexcept :
  7. ::nkg::exception(file, line, message)
  8. {
  9. m_error_code = win32_error_code;
  10. ::nkg::resource_wrapper error_string{ ::nkg::resource_traits::win32::local_alloc{} };
  11. FormatMessageW(
  12. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
  13. NULL,
  14. win32_error_code,
  15. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  16. error_string.template unsafe_addressof<wchar_t>(),
  17. 0,
  18. NULL
  19. );
  20. if (error_string.is_valid()) {
  21. m_error_string = ::nkg::cp_converter<-1, CP_UTF8>::convert(error_string.template as<wchar_t*>());
  22. } else {
  23. std::terminate();
  24. }
  25. }
  26. }