效果如下:

$ llbtest "E:\Developer\emgucv-windesktop 3.3.0.2824\libs\x64"
LoadLibraryExW PATH: E:\Developer\emgucv-windesktop 3.3.0.2824\libs\x64
E:\Developer\emgucv-windesktop 3.3.0.2824\libs\x64\concrt140.dll......[OK] ......Free: [OK]
E:\Developer\emgucv-windesktop 3.3.0.2824\libs\x64\cvextern.dll......[OK] ......Free: [OK]
E:\Developer\emgucv-windesktop 3.3.0.2824\libs\x64\msvcp140.dll......[OK] ......Free: [OK]
E:\Developer\emgucv-windesktop 3.3.0.2824\libs\x64\opencv_ffmpeg330_64.dll......[OK] ......Free: [OK]
E:\Developer\emgucv-windesktop 3.3.0.2824\libs\x64\vcruntime140.dll......[OK] ......Free: [OK]
-- Completed --

llbtest.cs 源代码:

用csc llbtest.cs编译即可

 using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices; class Program
{
static void Main(string[] args)
{
if (args.Length == || args.Length > )
{
ShowHelp();
return;
}
var path = args[];
if (!Directory.Exists(path))
{
ShowHelp();
return;
} var files = Directory.EnumerateFiles(path, "*.dll");
Console.WriteLine($"LoadLibraryExW PATH: {path}");
foreach (var file in files)
{
Console.Write(file);
const int loadLibrarySearchDllLoadDir = 0x00000100;
const int loadLibrarySearchDefaultDirs = 0x00001000;
//const int loadLibrarySearchUserDirs = 0x00000400;
IntPtr handler = NativeMethods.LoadLibraryExW(file, IntPtr.Zero, loadLibrarySearchDllLoadDir | loadLibrarySearchDefaultDirs);
//IntPtr handler = LoadLibraryEx(dllname, IntPtr.Zero, loadLibrarySearchUserDirs);
if (handler == IntPtr.Zero)
{
var error = Marshal.GetLastWin32Error();
var ex = new Win32Exception(error);
Console.WriteLine($"......[ERROR] {ex.NativeErrorCode}: {ex.Message}");
}
else
{
Console.Write($"......[OK] {handler}");
var freeLibrary = NativeMethods.FreeLibrary(handler);
Console.WriteLine($"......Free: {(freeLibrary ? "[OK]" : "[ERROR]")}");
}
}
Console.WriteLine("-- Completed --");
if(args.Length == && args[] == "...")
{
Console.Write("Press any key to exit...");
Console.ReadKey(true);
Console.WriteLine();
}
} private static void ShowHelp()
{
var x = AppDomain.CurrentDomain.FriendlyName;
Console.WriteLine($"Usage:\n\t{x} <path> [...]\n\tpath\tThe directory path that includes *.dll files\n\t...\tPause me when completed.");
}
} [StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct HINSTANCE__
{ /// int
public int unused;
} public partial class NativeMethods
{ /// Return Type: HMODULE->HINSTANCE->HINSTANCE__*
///lpLibFileName: LPCWSTR->WCHAR*
///hFile: HANDLE->void*
///dwFlags: DWORD->unsigned int
[DllImport("kernel32.dll", EntryPoint = "LoadLibraryExW", SetLastError = true)]
public static extern System.IntPtr LoadLibraryExW([In()] [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpLibFileName, System.IntPtr hFile, uint dwFlags); /// Return Type: BOOL->int
///hLibModule: HMODULE->HINSTANCE->HINSTANCE__*
[DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
[return: MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool FreeLibrary([In()] System.IntPtr hLibModule);
}
05-22 11:16