#ifdef _WIN32
#include "orconfig.h"
#include "lib/log/win32err.h"
#include "lib/malloc/malloc.h"
#include <tchar.h>
#include <windows.h>
char *
format_win32_error(DWORD err)
{
TCHAR *str = NULL;
char *result;
DWORD n;
n = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err,
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
(LPVOID)&str,
0, NULL);
if (str && n) {
#ifdef UNICODE
size_t len;
if (n > 128*1024)
len = (128 * 1024) * 2 + 1;
else
len = n * 2 + 1;
result = tor_malloc(len);
wcstombs(result,str,len);
result[len-1] = '\0';
#else
result = tor_strdup(str);
#endif
} else {
result = tor_strdup("<unformattable error>");
}
if (str) {
LocalFree(str);
}
return result;
}
#endif