diff --git a/Utilities/StrFmt.cpp b/Utilities/StrFmt.cpp index f0ee12cf48b6..06c7ca9e3557 100644 --- a/Utilities/StrFmt.cpp +++ b/Utilities/StrFmt.cpp @@ -34,12 +34,12 @@ std::wstring utf8_to_wchar(std::string_view src) return wchar_string; } -std::string fmt::win_error_to_string(unsigned long error) +std::string fmt::win_error_to_string(unsigned long error, void* module_handle) { std::string message; LPWSTR message_buffer = nullptr; - if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, - nullptr, error, 0, (LPWSTR)&message_buffer, 0, nullptr)) + if (FormatMessageW((module_handle ? FORMAT_MESSAGE_FROM_HMODULE : FORMAT_MESSAGE_FROM_SYSTEM) | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, + module_handle, error, 0, (LPWSTR)&message_buffer, 0, nullptr)) { message = fmt::format("%s (0x%x)", fmt::trim(wchar_to_utf8(message_buffer), " \t\n\r\f\v"), error); } diff --git a/Utilities/StrFmt.h b/Utilities/StrFmt.h index 64da174e799e..3f4f55cd88f4 100644 --- a/Utilities/StrFmt.h +++ b/Utilities/StrFmt.h @@ -10,8 +10,8 @@ namespace fmt static std::string format(const CharT(&)[N], const Args&...); #ifdef _WIN32 - // Get a string for a windows error (DWORD) - std::string win_error_to_string(unsigned long error); + // Get a string for a windows error (DWORD). Optionally a module HANDLE can be passed. + std::string win_error_to_string(unsigned long error, void* module_handle = nullptr); #endif } diff --git a/rpcs3/util/cpu_stats.cpp b/rpcs3/util/cpu_stats.cpp index d592695cc08e..3b34f72dc989 100644 --- a/rpcs3/util/cpu_stats.cpp +++ b/rpcs3/util/cpu_stats.cpp @@ -56,17 +56,7 @@ namespace utils #ifdef _WIN32 std::string pdh_error(PDH_STATUS status) { - HANDLE hPdhLibrary = LoadLibrary(L"pdh.dll"); - LPWSTR pMessage = NULL; - - if (hPdhLibrary && - FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, - hPdhLibrary, status, 0, (LPWSTR)&pMessage, 0, NULL)) - { - return fmt::format("%s (0x%x)", fmt::trim(wchar_to_utf8(pMessage), " \t\n\r\f\v"), status); - } - - return fmt::format("0x%x", status); + return fmt::win_error_to_string(status, LoadLibrary(L"pdh.dll")); } #endif