Added winpr_strerror function.

This commit is contained in:
Armin Novak 2015-12-09 18:27:37 +01:00
parent a04f1f8fc5
commit d0e3528c8e
2 changed files with 27 additions and 0 deletions

View File

@ -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
}

View File

@ -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;
}