From 252af6c3346a2ae0b5fd08251a579bba17dc7314 Mon Sep 17 00:00:00 2001
From: Albert Graef <aggraef@gmail.com>
Date: Sat, 20 Aug 2022 15:59:13 +0200
Subject: [PATCH] Improved handling of dlopen/LoadLibrary errors.

These are now proper errors instead of warnings, and the error message
on Windows is more informative as well, making it easier to detect and
diagnose such errors.

Backport from upstream:

https://github.com/pure-data/pure-data/commit/fee700b3
https://github.com/pure-data/pure-data/commit/af3894da
---
 pd/src/s_loader.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/pd/src/s_loader.c b/pd/src/s_loader.c
index 1dc0646a8..f656f3ddf 100644
--- a/pd/src/s_loader.c
+++ b/pd/src/s_loader.c
@@ -250,7 +250,14 @@ gotone:
         ntdll = LoadLibrary(filename);
         if (!ntdll)
         {
-            verbose(1, "%s: couldn't load", filename);
+            wchar_t wbuf[MAXPDSTRING];
+            char buf[MAXPDSTRING];
+            DWORD count, err = GetLastError();
+            count = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+                0, err, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), wbuf, MAXPDSTRING, NULL);
+            if (!count || !WideCharToMultiByte(CP_UTF8, 0, wbuf, count+1, buf, MAXPDSTRING, 0, 0))
+                *buf = '\0';
+            error("%s: %s (%d)", filename, buf, err);
             class_set_extern_dir(&s_);
             return (0);
         }
@@ -263,7 +270,7 @@ gotone:
     dlobj = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
     if (!dlobj)
     {
-        verbose(1, "%s: %s", filename, dlerror());
+        error("%s: %s", filename, dlerror());
         class_set_extern_dir(&s_);
         return (0);
     }
@@ -277,7 +284,7 @@ gotone:
 
     if (!makeout)
     {
-        verbose(1, "load_object: Symbol \"%s\" not found", symname);
+        error("load_object: Symbol \"%s\" not found", symname);
         class_set_extern_dir(&s_);
         return 0;
     }
-- 
GitLab