From 8b0085d99ceaa11a9e7888712ba3919fadecb5ba Mon Sep 17 00:00:00 2001 From: Max Katz Date: Tue, 27 Jun 2023 19:04:10 -0400 Subject: [PATCH] Test NativeLibraryEx with non net6 --- .../Compatibility/NativeLibrary.cs | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Avalonia.Base/Compatibility/NativeLibrary.cs b/src/Avalonia.Base/Compatibility/NativeLibrary.cs index 8c77ee928a..7627c095bc 100644 --- a/src/Avalonia.Base/Compatibility/NativeLibrary.cs +++ b/src/Avalonia.Base/Compatibility/NativeLibrary.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Reflection; using System.Runtime.InteropServices; using Avalonia.Compatibility; using Avalonia.Platform.Interop; @@ -9,23 +10,25 @@ namespace Avalonia.Compatibility internal class NativeLibraryEx { #if NET6_0_OR_GREATER - public static IntPtr Load(string dll) => System.Runtime.InteropServices.NativeLibrary.Load(dll); - public static bool TryGetExport(IntPtr handle, string name, out IntPtr address) - => System.Runtime.InteropServices.NativeLibrary.TryGetExport(handle, name, out address); + public static IntPtr Load(string dll, Assembly assembly) => NativeLibrary.Load(dll, assembly, null); + public static IntPtr Load(string dll) => NativeLibrary.Load(dll); + public static bool TryGetExport(IntPtr handle, string name, out IntPtr address) => + NativeLibrary.TryGetExport(handle, name, out address); #else + public static IntPtr Load(string dll, Assembly assembly) => Load(dll); public static IntPtr Load(string dll) { - var handle = DlOpen(dll); + var handle = DlOpen!(dll); if (handle != IntPtr.Zero) return handle; - throw new InvalidOperationException("Unable to load " + dll, DlErrorString()); + throw new InvalidOperationException("Unable to load " + dll, DlError!()); } public static bool TryGetExport(IntPtr handle, string name, out IntPtr address) { try { - address = DlSym(handle, name); + address = DlSym!(handle, name); return address != default; } catch (Exception) @@ -56,7 +59,7 @@ namespace Avalonia.Compatibility private static Func? DlOpen; private static Func? DlSym; - private static Func? DlErrorString; + private static Func? DlError; [DllImport("libc")] static extern int uname(IntPtr buf); @@ -73,7 +76,7 @@ namespace Avalonia.Compatibility { DlOpen = LoadLibrary; DlSym = GetProcAddress; - DlErrorString = () => new Win32Exception(Marshal.GetLastWin32Error()); + DlError = () => new Win32Exception(Marshal.GetLastWin32Error()); } } @@ -92,7 +95,7 @@ namespace Avalonia.Compatibility { DlOpen = s => dlopen(s, 1); DlSym = dlsym; - DlErrorString = () => new InvalidOperationException(Marshal.PtrToStringAnsi(dlerror())); + DlError = () => new InvalidOperationException(Marshal.PtrToStringAnsi(dlerror())); } } @@ -111,7 +114,7 @@ namespace Avalonia.Compatibility { DlOpen = s => dlopen(s, 1); DlSym = dlsym; - DlErrorString = () => new InvalidOperationException(Marshal.PtrToStringAnsi(dlerror())); + DlError = () => new InvalidOperationException(Marshal.PtrToStringAnsi(dlerror())); } } #endif