Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. std::string get_last_error_std_string(const DWORD error_code)
  2. {
  3. const DWORD fmt_message_flags =
  4. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
  5.  
  6. const DWORD lng_id = MAKELANGID(LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA);
  7.  
  8. std::string result;
  9.  
  10. if (error_code)
  11. {
  12. LPVOID msg_buf = nullptr;
  13.  
  14. const DWORD buf_len = FormatMessage
  15. (
  16. fmt_message_flags,
  17. nullptr,
  18. error_code,
  19. lng_id,
  20. reinterpret_cast<LPWSTR>(&msg_buf),
  21. 0,
  22. nullptr
  23. );
  24.  
  25. if (buf_len)
  26. {
  27. const int required_memory = WideCharToMultiByte
  28. (
  29. CP_UTF8,
  30. 0,
  31. static_cast<LPCWCH>(msg_buf),
  32. -1,
  33. nullptr,
  34. 0,
  35. nullptr,
  36. nullptr
  37. );
  38.  
  39. result.resize(required_memory);
  40.  
  41. (void)WideCharToMultiByte
  42. (
  43. CP_UTF8,
  44. 0,
  45. static_cast<LPCWCH>(msg_buf),
  46. static_cast<int>(buf_len),
  47. &result[0],
  48. required_memory,
  49. nullptr,
  50. nullptr
  51. );
  52.  
  53. LocalFree(msg_buf);
  54. }
  55. }
  56.  
  57. return result;
  58. }
  59.  
  60. throw std::runtime_error(u8"{0}{1:#x} Ошибка: "{2}"{5}Файл: {3}{5}Строка: {4}"_format
  61. (
  62.     u8"Не удалось зарегистрировать класс окна т.к. функция `RegisterClassExW` вернула ошибку ",
  63.     error_code,
  64.     get_last_error_std_string(error_code),
  65.     __FILE__,
  66.     __LINE__ - 2,
  67.     "n"
  68. ));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement