From d0e3528c8e07124964f873a733ab9b37f3ed0a0a Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Wed, 9 Dec 2015 18:27:37 +0100 Subject: [PATCH] Added winpr_strerror function. --- winpr/include/winpr/debug.h | 1 + winpr/libwinpr/utils/debug.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/winpr/include/winpr/debug.h b/winpr/include/winpr/debug.h index 3d2cde6..362bafd 100644 --- a/winpr/include/winpr/debug.h +++ b/winpr/include/winpr/debug.h @@ -32,6 +32,7 @@ WINPR_API void* winpr_backtrace(DWORD size); WINPR_API void winpr_backtrace_free(void* buffer); WINPR_API char** winpr_backtrace_symbols(void* buffer, size_t* used); WINPR_API void winpr_backtrace_symbols_fd(void* buffer, int fd); +WINPR_API char* winpr_strerror(DWORD dw, char* dmsg, size_t size); #ifdef __cplusplus } diff --git a/winpr/libwinpr/utils/debug.c b/winpr/libwinpr/utils/debug.c index d90d1e0..0b3150e 100644 --- a/winpr/libwinpr/utils/debug.c +++ b/winpr/libwinpr/utils/debug.c @@ -471,3 +471,29 @@ void winpr_log_backtrace(const char* tag, DWORD level, DWORD size) winpr_backtrace_free(stack); } +char* winpr_strerror(DWORD dw, char* dmsg, size_t size) { + LPTSTR msg = NULL; + DWORD rc; + +#if defined(_WIN32) + rc = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, dw, 0, (LPTSTR)&msg, 0, NULL); + if (rc) { +#if defined(UNICODE) + WideCharToMultiByte(CP_ACP, 0, msg, rc, dmsg, size - 1, NULL, NULL); +#else + memcpy(dmsg, msg, min(rc, size - 1)); +#endif + dmsg[min(rc, size - 1)] = 0; + LocalFree(msg); + } else { + _snprintf(dmsg, size, "FAILURE: %08X", GetLastError()); + } +#else + _snprintf(dmsg, size, "%s", strerror(dw)); +#endif + + return dmsg; +} -- 2.7.4