Make system error messeges in Windows 10 use UTF-8

The previous error messages did not support Unicode characters. This
commit will use UTF-8 encoding to be able to display error messages in
every language.
pull/8/head
Alex Tanskanen 5 years ago committed by Lauri Kasanen
parent 06f3413446
commit 57427d5d33

@ -54,33 +54,16 @@ SystemException::SystemException(const char* s, int err_)
{ {
strncat(str_, ": ", len-1-strlen(str_)); strncat(str_, ": ", len-1-strlen(str_));
#ifdef _WIN32 #ifdef _WIN32
// Windows error messages are crap, so use unix ones for common errors. wchar_t *currStr = new wchar_t[len-strlen(str_)];
const char* msg = 0; FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
switch (err) { 0, err, 0, currStr, len-1-strlen(str_), 0);
case WSAECONNREFUSED: msg = "Connection refused"; break; WideCharToMultiByte(CP_UTF8, 0, currStr, -1, str_+strlen(str_),
case WSAETIMEDOUT: msg = "Connection timed out"; break; len-1-strlen(str_), 0, 0);
case WSAECONNRESET: msg = "Connection reset by peer"; break; delete [] currStr;
case WSAECONNABORTED: msg = "Connection aborted"; break;
} int l = strlen(str_);
if (msg) { if ((l >= 2) && (str_[l-2] == '\r') && (str_[l-1] == '\n'))
strncat(str_, msg, len-1-strlen(str_));
} else {
#ifdef UNICODE
WCHAR* tmsg = new WCHAR[len-strlen(str_)];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
0, err, 0, tmsg, len-1-strlen(str_), 0);
WideCharToMultiByte(CP_ACP, 0, tmsg, wcslen(tmsg)+1,
str_+strlen(str_), len-strlen(str_), 0, 0);
delete [] tmsg;
#else
char* currStr = str_+strlen(str_);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
0, err, 0, currStr, len-1-strlen(str_), 0);
#endif
int l = strlen(str_);
if ((l >= 2) && (str_[l-2] == '\r') && (str_[l-1] == '\n'))
str_[l-2] = 0; str_[l-2] = 0;
}
#else #else
strncat(str_, strerror(err), len-1-strlen(str_)); strncat(str_, strerror(err), len-1-strlen(str_));

Loading…
Cancel
Save