Skip to content

Commit

Permalink
cpu_stats: fmt::win_error_to_string
Browse files Browse the repository at this point in the history
  • Loading branch information
Megamouse committed Mar 16, 2022
1 parent 7a1a454 commit e58906c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 16 deletions.
6 changes: 3 additions & 3 deletions Utilities/StrFmt.cpp
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions Utilities/StrFmt.h
Expand Up @@ -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
}

Expand Down
12 changes: 1 addition & 11 deletions rpcs3/util/cpu_stats.cpp
Expand Up @@ -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

Expand Down

0 comments on commit e58906c

Please sign in to comment.