win32: Add private HR_CHECK() macro

This works like GDK_VK_CHECK() in that it adds a check for a HRESULT and
if the result is not okay, it prints a g_warning() about the statement
that was evaluated and the error message, ideally with its string
version.

This is somewhat brittle because it's unclear if HRESULTs are allowed to
be treated like errors from GetLastError() but that's what everyone else
seems to do, too.
This commit is contained in:
Benjamin Otte
2023-04-29 06:37:59 +02:00
committed by Chun-wei Fan
parent d6a4c65bfa
commit 41f5678c2a

View File

@@ -251,12 +251,39 @@ void _gdk_win32_print_event (GdkEvent *event);
#endif
char *_gdk_win32_last_error_string (void);
void _gdk_win32_api_failed (const char *where,
const char *api);
void _gdk_other_api_failed (const char *where,
const char *api);
static inline HRESULT
hr_check (HRESULT hr,
const char *domain,
const char *file,
const char *line,
const char *func,
const char *expr)
{
char *error_string;
if (G_LIKELY (SUCCEEDED (hr)))
return hr;
error_string = g_win32_error_message (hr);
g_log_structured_standard (domain,
G_LOG_LEVEL_WARNING,
file,
line,
func,
"%s = 0x%lx: %s", expr, hr, error_string);
g_free (error_string);
return hr;
}
#define HR_CHECK(expr) hr_check (expr, G_LOG_DOMAIN, __FILE__, G_STRINGIFY (__LINE__), G_STRFUNC, #expr);
#define WIN32_API_FAILED(api) _gdk_win32_api_failed (G_STRLOC , api)
#define WIN32_GDI_FAILED(api) WIN32_API_FAILED (api)
#define OTHER_API_FAILED(api) _gdk_other_api_failed (G_STRLOC, api)