using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Web; namespace Paho.MqttDotnet { /// /// 表示动态库加载器 /// static class LibraryLoader { /// /// 加载dll /// /// /// [DllImport("kernel32")] private static extern IntPtr LoadLibraryA([MarshalAs(UnmanagedType.LPStr)] string fileName); /// /// 获取dll的完整路径 /// /// dll名称 /// private static string GetDllFullPath(string dllName) { var dllFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory , Environment.Is64BitProcess ? @"x64\" : @"x86\", dllName); if (HttpContext.Current != null) { dllFile = Path.Combine("~\\bin", dllFile); dllFile = HttpContext.Current.Server.MapPath(dllFile); } return dllFile; } /// /// 加载dll /// /// dll名 /// /// public static IntPtr[] Load(params string[] dllName) { return dllName.Select(dll => { var dllPath = GetDllFullPath(dll); if (File.Exists(dllPath) == false) { throw new FileNotFoundException(dllPath); } return LoadLibraryA(dllPath); }).ToArray(); } } }