summaryrefslogtreecommitdiff
path: root/src/util/error.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/error.c')
-rw-r--r--src/util/error.c29
1 files changed, 21 insertions, 8 deletions
diff --git a/src/util/error.c b/src/util/error.c
index 46fd8e6..7f2e847 100644
--- a/src/util/error.c
+++ b/src/util/error.c
@@ -1,6 +1,9 @@
#include "error.h"
static __thread char errorbuf[NNWT_ERROR_STR_MAXLEN];
+#ifdef NAUNET_ON_WINDOWS
+static __thread wchar_t errorbufw[NNWT_ERROR_STR_MAXLEN];
+#endif
char *nn_strerror(s32 errnum)
{
@@ -19,21 +22,31 @@ char *nn_strerror(s32 errnum)
#ifdef NAUNET_ON_WINDOWS
#include "../winwrap.h"
-// @TODO: GetLastError() should be used in common places like file_stdio.
+#include <wchar.h>
// https://stackoverflow.com/a/46104456
char *nn_win32_error_message(s32 err)
{
- /*
- DWORD ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+ DWORD ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- errorbuf,
+ 0,
+ errorbufw,
NNWT_ERROR_STR_MAXLEN,
NULL);
- return !ret ? NULL : errorbuf;
- */
- return NULL;
+ if (!ret) {
+ return NULL;
+ }
+ errorbufw[ret] = L'\0';
+ mbstate_t mbstate = { 0 };
+ wchar_t *wc = errorbufw;
+ size_t length = wcsrtombs(NULL, (const wchar_t **)&wc, 0, &mbstate);
+ if (length == (size_t)-1) {
+ return NULL;
+ }
+ length = wcsrtombs(errorbuf, (const wchar_t **)&wc, length, &mbstate);
+ al_assert(length < NNWT_ERROR_STR_MAXLEN);
+ errorbuf[length] = L'\0';
+ return errorbuf;
}
#endif