system_error.cpp 886 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "system_error.h"
  2. #include <Windows.h>
  3. namespace am {
  4. const std::string& system_error::error2str(unsigned long error)
  5. {
  6. DWORD system_locale = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
  7. HLOCAL local_buf = nullptr;
  8. BOOL ret = FormatMessage(
  9. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  10. NULL, error, system_locale,(PSTR) &local_buf, 0, NULL);
  11. if (!ret) {
  12. HMODULE hnetmsg = LoadLibraryEx("netmsg.dll", NULL, DONT_RESOLVE_DLL_REFERENCES);
  13. if (hnetmsg != nullptr) {
  14. ret = FormatMessage(
  15. FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  16. hnetmsg, error, system_locale, (PSTR)&local_buf, 0, NULL);
  17. FreeLibrary(hnetmsg);
  18. }
  19. }
  20. std::string error_str;
  21. if (ret) {
  22. error_str = (LPCTSTR)LocalLock(local_buf);
  23. LocalFree(local_buf);
  24. }
  25. return error_str;
  26. }
  27. }