1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2020, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
8 * \brief Convert windows error codes to useful C strings.
13 #include "lib/log/win32err.h"
14 #include "lib/malloc/malloc.h"
19 /** Return a newly allocated string describing the windows system error code
20 * <b>err</b>. Note that error codes are different from errno. Error codes
21 * come from GetLastError() when a winapi call fails. errno is set only when
22 * ANSI functions fail. Whee. */
24 format_win32_error(DWORD err
)
30 /* Somebody once decided that this interface was better than strerror(). */
31 n
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
32 FORMAT_MESSAGE_FROM_SYSTEM
|
33 FORMAT_MESSAGE_IGNORE_INSERTS
,
35 MAKELANGID(LANG_ENGLISH
, SUBLANG_DEFAULT
),
43 len
= (128 * 1024) * 2 + 1; /* This shouldn't be possible, but let's
47 result
= tor_malloc(len
);
48 wcstombs(result
,str
,len
);
50 #else /* !defined(UNICODE) */
51 result
= tor_strdup(str
);
52 #endif /* defined(UNICODE) */
54 result
= tor_strdup("<unformattable error>");
57 LocalFree(str
); /* LocalFree != free() */
61 #endif /* defined(_WIN32) */