mirror of
https://github.com/mpv-player/mpv
synced 2025-01-03 13:32:16 +00:00
windows_utils: try and use FormatMessage for errors.
This is useful in particular for GetLastError, unfortunately, it's stil pretty dumb with regards to WASAPI or D3D specific errors, so keep the hresult_to_string switch.
This commit is contained in:
parent
f371367aac
commit
50d9a2609a
@ -124,9 +124,29 @@ static char *hresult_to_str(const HRESULT hr)
|
|||||||
#undef E
|
#undef E
|
||||||
}
|
}
|
||||||
|
|
||||||
char *mp_HRESULT_to_str_buf(char *buf, size_t buf_size, HRESULT hr)
|
static char *fmtmsg_buf(char *buf, size_t buf_size, DWORD errorID)
|
||||||
{
|
{
|
||||||
snprintf(buf, buf_size, "%s (0x%"PRIx32")",
|
DWORD n = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
|
||||||
hresult_to_str(hr), (uint32_t) hr);
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
|
NULL, errorID, 0, buf, buf_size, NULL);
|
||||||
|
if (!n && GetLastError() == ERROR_MORE_DATA) {
|
||||||
|
snprintf(buf, buf_size,
|
||||||
|
"<Insufficient buffer size (%zd) for error message>",
|
||||||
|
buf_size);
|
||||||
|
} else {
|
||||||
|
if (n > 0 && buf[n-1] == '\n')
|
||||||
|
buf[n-1] = '\0';
|
||||||
|
if (n > 1 && buf[n-2] == '\r')
|
||||||
|
buf[n-2] = '\0';
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
#define fmtmsg(hr) fmtmsg_buf((char[243]){0}, 243, (hr))
|
||||||
|
|
||||||
|
char *mp_HRESULT_to_str_buf(char *buf, size_t buf_size, HRESULT hr)
|
||||||
|
{
|
||||||
|
char* msg = fmtmsg(hr);
|
||||||
|
msg = msg[0] ? msg : hresult_to_str(hr);
|
||||||
|
snprintf(buf, buf_size, "%s (0x%"PRIx32")", msg, (uint32_t)hr);
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
char *mp_GUID_to_str_buf(char *buf, size_t buf_size, const GUID *guid);
|
char *mp_GUID_to_str_buf(char *buf, size_t buf_size, const GUID *guid);
|
||||||
#define mp_GUID_to_str(guid) mp_GUID_to_str_buf((char[40]){0}, 40, (guid))
|
#define mp_GUID_to_str(guid) mp_GUID_to_str_buf((char[40]){0}, 40, (guid))
|
||||||
char *mp_HRESULT_to_str_buf(char *buf, size_t buf_size, HRESULT hr);
|
char *mp_HRESULT_to_str_buf(char *buf, size_t buf_size, HRESULT hr);
|
||||||
#define mp_HRESULT_to_str(hr) mp_HRESULT_to_str_buf((char[60]){0}, 60, (hr))
|
#define mp_HRESULT_to_str(hr) mp_HRESULT_to_str_buf((char[256]){0}, 256, (hr))
|
||||||
#define mp_LastError_to_str() mp_HRESULT_to_str(HRESULT_FROM_WIN32(GetLastError()))
|
#define mp_LastError_to_str() mp_HRESULT_to_str(HRESULT_FROM_WIN32(GetLastError()))
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user