Skip to content

Commit

Permalink
Use winhttp handle for its errors (fixes #164)
Browse files Browse the repository at this point in the history
  • Loading branch information
namazso committed Jul 15, 2023
1 parent d402df3 commit 998f813
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions OpenHashTab/utl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,19 +363,28 @@ std::wstring utl::ErrorToString(DWORD error) {
// FormatMessageW: "This buffer cannot be larger than 64K bytes."
wstr.resize(64 * 1024 / sizeof(wchar_t));

HMODULE source = nullptr;
DWORD flags = FORMAT_MESSAGE_IGNORE_INSERTS;
if (error >= WINHTTP_ERROR_BASE && error < WINHTTP_ERROR_BASE + 1000) {
source = GetModuleHandleW(L"winhttp");
flags |= FORMAT_MESSAGE_FROM_HMODULE;
} else {
flags |= FORMAT_MESSAGE_FROM_SYSTEM;
}

const auto size = FormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
flags,
source,
error,
MAKELANGID(LANG_USER_DEFAULT, SUBLANG_DEFAULT),
0,
wstr.data(),
static_cast<DWORD>(wstr.size()),
nullptr
);
wstr.resize(size);
const auto pos = wstr.find_last_not_of(L"\r\n");
if (pos != std::wstring::npos)
wstr.resize(pos);
wstr.resize(pos + 1);
return wstr;
}

Expand Down

0 comments on commit 998f813

Please sign in to comment.