From d32feb765a319e0ef1f172a9f8c078442d33080c Mon Sep 17 00:00:00 2001 From: Chris Dill Date: Thu, 3 Sep 2026 19:01:21 +0100 Subject: [PATCH] Improve custom dll import error handling * Use NativeLibrary.Load instead of NativeLibrary.TryLoad * Add error details to the DllNotFoundException --- Raylib-cs/interop/Raylib.cs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/Raylib-cs/interop/Raylib.cs b/Raylib-cs/interop/Raylib.cs index 78d492f..4b81f35 100644 --- a/Raylib-cs/interop/Raylib.cs +++ b/Raylib-cs/interop/Raylib.cs @@ -26,21 +26,31 @@ public static unsafe partial class Raylib public static IntPtr ResolveDllImport(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) { - IntPtr handle = IntPtr.Zero; - string libraryPath = GetLibraryPath(libraryName); - - if (NativeLibrary.TryLoad(libraryName, assembly, searchPath, out handle)) + string resolveNameError; + try { + IntPtr handle = NativeLibrary.Load(libraryName, assembly, searchPath); return handle; } - - if (NativeLibrary.TryLoad(libraryPath, out handle)) + catch (Exception ex) { + resolveNameError = ex.Message; + } + + string resolvePathError; + string libraryPath = GetLibraryPath(libraryName); + try + { + IntPtr handle = NativeLibrary.Load(libraryPath); return handle; } + catch (Exception ex) + { + resolvePathError = ex.Message; + } throw new DllNotFoundException( - $"Failed to load {libraryName}." + string.Join("\n", string.Empty, resolveNameError, resolvePathError) ); }