From 0c0f6031e2c312cb906b454b7803e929103e2b5b Mon Sep 17 00:00:00 2001 From: Colin Edwards Date: Wed, 17 Aug 2016 13:09:36 -0500 Subject: [PATCH] libobs/util: Use FormatMessage on error when LoadLibrary fails Outputting a human-readable error message on library load failure makes it a little bit easier for plugin developers to determine why a plugin library may have failed to load (such as missing dependency), rather than having to look up the error code each time. Closes jp9000/obs-studio#596 --- libobs/util/platform-windows.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/libobs/util/platform-windows.c b/libobs/util/platform-windows.c index 2e33cfaf1..b4201b903 100644 --- a/libobs/util/platform-windows.c +++ b/libobs/util/platform-windows.c @@ -84,9 +84,24 @@ void *os_dlopen(const char *path) if (wpath_slash) SetDllDirectoryW(NULL); - if (!h_library) - blog(LOG_INFO, "LoadLibrary failed for '%s', error: %ld", - path, GetLastError()); + if (!h_library) { + DWORD error = GetLastError(); + char *message = NULL; + + FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS | + FORMAT_MESSAGE_ALLOCATE_BUFFER, + NULL, error, + MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), + (LPSTR)&message, 0, NULL); + + blog(LOG_INFO, "LoadLibrary failed for '%s': %s (%lu)", + path, message, error); + + if (message) + LocalFree(message); + } + return h_library; }