From c0345c798a88a0e1fe45c433de98548c58950879 Mon Sep 17 00:00:00 2001 From: Canming Huang Date: Tue, 14 Jul 2020 13:16:42 -0400 Subject: [PATCH] Make Toolbox.LoadLibrary .netstandard compilation compatible with UWP target. --- Emgu.Util/Toolbox.cs | 57 ++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/Emgu.Util/Toolbox.cs b/Emgu.Util/Toolbox.cs index ebd32c434..148d54a5b 100644 --- a/Emgu.Util/Toolbox.cs +++ b/Emgu.Util/Toolbox.cs @@ -153,13 +153,6 @@ namespace Emgu.Util } - [DllImport("Kernel32.dll", SetLastError = true)] - private static extern IntPtr LoadPackagedLibrary( - [MarshalAs(UnmanagedType.LPStr)] - String fileName, - int dwFlags); - - /// /// Call a command from command line /// @@ -530,22 +523,10 @@ namespace Emgu.Util const int loadLibrarySearchDllLoadDir = 0x00000100; const int loadLibrarySearchDefaultDirs = 0x00001000; return LoadLibraryEx(dllname, IntPtr.Zero, loadLibrarySearchDllLoadDir | loadLibrarySearchDefaultDirs); -#elif NETFX_CORE - IntPtr handler = LoadPackagedLibrary(dllname, 0); - - if (handler == IntPtr.Zero) - { - int error = Marshal.GetLastWin32Error(); - - System.Diagnostics.Debug.WriteLine(String.Format("Error loading {0}: error code {1}", dllname, (uint)error)); - } - - return handler; #else if (Platform.OperationSystem == Emgu.Util.Platform.OS.Windows) { - //if (Platform.ClrType == TypeEnum.ClrType.NetFxCore) - { + const int loadLibrarySearchDllLoadDir = 0x00000100; const int loadLibrarySearchDefaultDirs = 0x00001000; const int loadLibrarySearchApplicationDir = 0x00000200; @@ -562,10 +543,22 @@ namespace Emgu.Util { System.Diagnostics.Trace.WriteLine(String.Format("Please check if the current user has execute permission for file: {0} ", dllname)); } - } + + //Also try loadPackagedLibrary + IntPtr packagedLibraryHandler = LoadPackagedLibrary(dllname, 0); + if (packagedLibraryHandler == IntPtr.Zero) + { + error = Marshal.GetLastWin32Error(); + ex = new System.ComponentModel.Win32Exception(error); + System.Diagnostics.Debug.WriteLine(String.Format("LoadPackagedLibrary {0} failed with error code {1}: {2}", dllname, (uint)error, ex.Message)); + } + else + { + System.Diagnostics.Trace.WriteLine(String.Format("LoadPackagedLibrary loaded: {0}", dllname)); + return packagedLibraryHandler; + } + } return handler; - } //else - //return WinAPILoadLibrary(dllname); } else { @@ -574,6 +567,24 @@ namespace Emgu.Util #endif } + /* + [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] + static extern UIntPtr GetProcAddress(IntPtr hModule, string procName); + + private static bool? _kernel32HasLoadPackagedLibrary = null; + private static IntPtr LoadPackagedLibrarySafe(String fileName, int dwFlags) + { + if (_kernel32HasLoadPackagedLibrary == null) + { + UIntPtr = GetProcAddress("") + } + }*/ + + [DllImport("Kernel32.dll", SetLastError = true)] + private static extern IntPtr LoadPackagedLibrary( + [MarshalAs(UnmanagedType.LPStr)] + String fileName, + int dwFlags); [DllImport("Kernel32.dll", SetLastError = true)] private static extern IntPtr LoadLibraryEx(