C#如何加载嵌入到资源的非托管dll

如何加载非托管Dll

我们总会遇到需要加载非Win32的非托管dll,这里推荐一种方式就是将那些非win32的非托管dll嵌入资源的方式,在入口解压并且加载的方式,我先来看看如何实现吧,首先我们准备好demo,新增控制台项目如下:

代码如下:

        static void Main(string[] args)
        {
            UnzipAndLoad();
        }

        /// <summary>
        /// 解压资源并且加载非托管DLL
        /// </summary>
        static void UnzipAndLoad()
        {
            var folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var dllPath = Path.Combine(folderPath, $"{nameof(Resource.pdfium)}.dll");//解压输出的路径
            if (!File.Exists(dllPath))
                File.WriteAllBytes(dllPath, Resource.pdfium);
            LoadDll(dllPath);//应该每次都加载非托管
        }

        /// <summary>
        /// 加载非托管DLL
        /// </summary>
        /// <param name="dllName"></param>
        public static void LoadDll(string dllName)
        {
            IntPtr h = LoadLibrary(dllName);
            if (h == IntPtr.Zero)
            {
                Exception e = new Win32Exception();
                throw new DllNotFoundException($"Unable to load library: {dllName}", e);
            }

            Console.WriteLine("Load library successful");
        }

        [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
        static extern IntPtr LoadLibrary(string lpFileName);

输出:

Load library successful

其实上述代码还有优化的空间,微软集成了很多win32函数的包,例如我们要导入win32的下常见的kernel32dll和user32dll,我们可以通过nuget安装,我们可以在csproj加入以下代码(或者直接nuget搜索PInvoke.Kernel32):

  <ItemGroup>
        <PackageReference Include="PInvoke.Kernel32" Version="0.7.104" />
  </ItemGroup>

那么之前的代码删除的LoadLibrary方法删除,LoadDll方法则直接改为以下:

    /// <summary>
    /// 加载非托管DLL
    /// </summary>
    /// <param name="dllName"></param>
    public static void LoadDll(string dllName)
    {
        var h =Kernel32.LoadLibrary(dllName);
        if (h.IsInvalid)//是否是无效的
        {
            Exception e = new Win32Exception();
            throw new DllNotFoundException($"Unable to load library: {dllName}", e);
        }
        Console.WriteLine("Load library successful");
    }

参考

https://blog.lindexi.com/post/%E6%8E%A8%E8%8D%90%E5%AE%98%E6%96%B9%E5%BC%80%E6%BA%90-PInvoke-%E5%BA%93-%E5%8C%85%E5%90%AB%E5%A4%A7%E9%87%8F-win32-%E5%B0%81%E8%A3%85.html

posted @ 2021-05-15 12:59  RyzenAdorer  阅读(1639)  评论(0编辑  收藏  举报