从 .Net 托管代码加载 32 位或 64 位 DLL [英] 32 or 64 bit DLL loading from .Net managed code

查看:42
本文介绍了从 .Net 托管代码加载 32 位或 64 位 DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非托管 DLL(Scintilla 代码编辑器的 scilexer.dll,由来自 CodePlex 的 Scintilla.Net 使用)a>) 通过 Scintilla.Net 组件从托管应用程序加载.Windows 管理的应用程序在 32 位和 64 位环境下都可以正常运行,但我需要创建使用 64 位或 32 位 scilexer.dll 的不同安装.

I have a unmanaged DLL (the scilexer.dll of Scintilla code editor, used by Scintilla.Net from CodePlex) that is loaded from a managed application trough the Scintilla.Net component. The windows managed application runs without problem on both 32 and 64 bit environments, but I need to create different installations that uses the 64 or the 32 scilexer.dll.

有没有办法同时分发 32 位和 64 位格式的 DLL,以便 .Net 框架的 DLL 加载器根据某些 .config 选项或某些路径名魔术"加载 32 位或 64 位格式的非托管 DLL东西?

Is there a way to distribute both DLLs in 32 and 64 bit format so that the DLL loader of the .Net framework loads the unmanaged DLL in the 32 or 64 bit format depending on some .config option or some "path name magic" stuff?

推荐答案

P/Invoke 使用 LoadLibrary 加载 DLL,如果已经加载了一个给定名称的库,LoadLibrary 将返回它.因此,如果您可以为 DLL 的两个版本指定相同的名称,但将它们放在不同的目录中,那么您可以在第一次从 scilexer.dll 调用函数之前执行类似的操作,而无需重复您的 extern 声明:

P/Invoke uses LoadLibrary to load DLLs, and if there is already a library loaded with a given name, LoadLibrary will return it. So if you can give both versions of the DLL the same name, but put them in different directories, you can do something like this just once before your first call to a function from scilexer.dll, without needing to duplicate your extern declarations:

    string platform = IntPtr.Size == 4 ? "x86" : "x64";
    string dll = installDir + @"lib-" + platform + @"scilexer.dll";
    if (LoadLibrary(dll) == IntPtr.Zero)
        throw new IOException("Unable to load " + dll + ".");

这篇关于从 .Net 托管代码加载 32 位或 64 位 DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆