#include "error.h"
#include "utf-conv.h"
#ifdef GIT_WINHTTP
# include <winhttp.h>
#endif
char *git_win32_get_error_message(DWORD error_code)
{
LPWSTR lpMsgBuf = NULL;
HMODULE hModule = NULL;
char *utf8_msg = NULL;
DWORD dwFlags =
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS;
if (!error_code)
return NULL;
#ifdef GIT_WINHTTP
if (error_code >= WINHTTP_ERROR_BASE &&
error_code <= WINHTTP_ERROR_LAST)
hModule = GetModuleHandleW(L"winhttp");
#endif
GIT_UNUSED(hModule);
if (hModule)
dwFlags |= FORMAT_MESSAGE_FROM_HMODULE;
else
dwFlags |= FORMAT_MESSAGE_FROM_SYSTEM;
if (FormatMessageW(dwFlags, hModule, error_code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&lpMsgBuf, 0, NULL)) {
if (git__utf16_to_8_alloc(&utf8_msg, lpMsgBuf) < 0)
utf8_msg = NULL;
LocalFree(lpMsgBuf);
}
return utf8_msg;
}