Skip to content
Snippets Groups Projects
Commit 30198dbc authored by Ben Boeckel's avatar Ben Boeckel
Browse files

DynamicLoader: fix error reporting on Windows

Windows was reporting errors in `wchar_t` which was not reinterpreted as
UTF-8 which is expected from kwsys errors. This basically made error
messages appear as truncated since the UCS-2 byte array had embedded
byte NUL characters.
parent fd41ac36
No related branches found
No related tags found
No related merge requests found
......@@ -247,24 +247,38 @@ DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
# endif
}
# define DYNLOAD_ERROR_BUFFER_SIZE 1024
const char* DynamicLoader::LastError()
{
LPVOID lpMsgBuf = NULL;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR)&lpMsgBuf, 0, NULL);
wchar_t lpMsgBuf[DYNLOAD_ERROR_BUFFER_SIZE + 1];
DWORD error = GetLastError();
DWORD length = FormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
lpMsgBuf, DYNLOAD_ERROR_BUFFER_SIZE, NULL);
static char str[DYNLOAD_ERROR_BUFFER_SIZE + 1];
if (length < 1) {
/* FormatMessage failed. Use a default message. */
_snprintf(str, DYNLOAD_ERROR_BUFFER_SIZE,
"DynamicLoader encountered error 0x%X. "
"FormatMessage failed with error 0x%X",
error, GetLastError());
return str;
}
if (!lpMsgBuf) {
return NULL;
if (!WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, str,
DYNLOAD_ERROR_BUFFER_SIZE, NULL, NULL)) {
/* WideCharToMultiByte failed. Use a default message. */
_snprintf(str, DYNLOAD_ERROR_BUFFER_SIZE,
"DynamicLoader encountered error 0x%X. "
"WideCharToMultiByte failed with error 0x%X",
error, GetLastError());
}
static char* str = 0;
delete[] str;
str = strcpy(new char[strlen((char*)lpMsgBuf) + 1], (char*)lpMsgBuf);
// Free the buffer.
LocalFree(lpMsgBuf);
return str;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment