ExceptionWin32.hpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #pragma once
  2. #include "Exception.hpp"
  3. namespace nkg {
  4. class Win32Error final : public Exception {
  5. private:
  6. DWORD _ErrorCode;
  7. std::xstring _ErrorString;
  8. public:
  9. Win32Error(PCTSTR SourceFile, SIZE_T SourceLine, DWORD Win32ErrorCode, PCTSTR CustomMessage) noexcept :
  10. Exception(SourceFile, SourceLine, CustomMessage),
  11. _ErrorCode(Win32ErrorCode)
  12. {
  13. PTSTR Text = NULL;
  14. FormatMessage(
  15. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
  16. NULL,
  17. Win32ErrorCode,
  18. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  19. reinterpret_cast<PTSTR>(&Text),
  20. 0,
  21. NULL
  22. );
  23. if (Text) {
  24. _ErrorString = Text;
  25. LocalFree(Text);
  26. }
  27. }
  28. [[nodiscard]]
  29. virtual bool HasErrorCode() const noexcept override {
  30. return true;
  31. }
  32. [[nodiscard]]
  33. virtual ULONG_PTR ErrorCode() const noexcept override {
  34. return _ErrorCode;
  35. }
  36. [[nodiscard]]
  37. virtual PCTSTR ErrorString() const noexcept override {
  38. return _ErrorString.c_str();
  39. }
  40. };
  41. }