1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include "stdafx.h"
#include "SystemError.h"
#include "Convert.h"
#ifdef WINDOWS
#undef null
#include <comdef.h>
#define null NULL
#endif
namespace storm {
#if defined(WINDOWS)
Str *systemErrorMessage(Engine &e, DWORD lastError) {
wchar *buffer = null;
size_t size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
lastError,
// MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(wchar *)&buffer,
0,
NULL);
try {
Str *msg = new (e) Str(buffer);
LocalFree(buffer);
return msg;
} catch (...) {
LocalFree(buffer);
throw;
}
}
Str *hresultErrorMessage(Engine &e, HRESULT result) {
return new (e) Str(_com_error(result).ErrorMessage());
}
#endif
#if defined(POSIX)
// There are two variants of strerror_r with different return values. Detect which one is used
// and extract the error as appropriate.
static const char *extract(char *buffer, int result) {
if (result != 0)
strcat(buffer, "unknown error");
return buffer;
}
static const char *extract(char *buffer, const char *result) {
(void)buffer;
return result;
}
Str *systemErrorMessage(Engine &e, int error) {
const size_t bufsize = 512;
char buffer[bufsize] = { 0 };
const char *msg = extract(buffer, strerror_r(error, buffer, bufsize));
return new (e) Str(toWChar(e, msg));
}
#endif
}
|