misc.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstddef>
  5. #ifdef _WIN32
  6. #include <windows.h>
  7. #else
  8. #include <cerrno>
  9. #include <cstring>
  10. #endif
  11. #include "common/common_funcs.h"
  12. // Generic function to get last error message.
  13. // Call directly after the command or use the error num.
  14. // This function might change the error code.
  15. std::string GetLastErrorMsg() {
  16. static constexpr std::size_t buff_size = 255;
  17. char err_str[buff_size];
  18. #ifdef _WIN32
  19. FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(),
  20. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), err_str, buff_size, nullptr);
  21. return std::string(err_str, buff_size);
  22. #elif defined(__GLIBC__) && (_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600))
  23. // Thread safe (GNU-specific)
  24. const char* str = strerror_r(errno, err_str, buff_size);
  25. return std::string(str);
  26. #else
  27. // Thread safe (XSI-compliant)
  28. const int success = strerror_r(errno, err_str, buff_size);
  29. if (success != 0) {
  30. return {};
  31. }
  32. return std::string(err_str);
  33. #endif
  34. }