从.Net托管代码加载32或64位DLL

时间:2008-12-18 08:17:40

标签: .net 64-bit fusion

我有一个非托管DLL(Scintilla代码编辑器的scilexer.dll,由CodePlex的Scintilla.Net使用),它是通过Scintilla.Net组件从托管应用程序加载的。 Windows托管应用程序在32位和64位环境下运行都没有问题,但我需要创建使用64或32 scilexer.dll的不同安装。

有没有办法以32位和64位格式分发这两个DLL,以便.Net框架的DLL加载程序根据某些.config选项或某些“路径名称魔法”加载32位或64位格式的非托管DLL “东西?

5 个答案:

答案 0 :(得分:5)

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

    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 + ".");

答案 1 :(得分:4)

不幸的是,我对这个特定的DLL一无所知。但是,当您自己进行P / Invoke,并且可以处理一些重复时,可以为每个平台创建一个代理。

例如,假设您有以下接口,应该由32位或64位DLL实现:

public interface ICodec {
    int Decode(IntPtr input, IntPtr output, long inputLength);
}

您创建代理:

public class CodecX86 : ICodec {
    private const string dllFileName = @"Codec.x86.dll";

    [DllImport(dllFileName)]
    static extern int decode(IntPtr input, IntPtr output, long inputLength);

    public int Decode(IntPtr input, IntPtr output, long inputLength) {
        return decode(input, output, inputLength);
    }
}

public class CodecX64 : ICodec {
    private const string dllFileName = @"Codec.x64.dll";

    [DllImport(dllFileName)]
    static extern int decode(IntPtr input, IntPtr output, long inputLength);

    public int Decode(IntPtr input, IntPtr output, long inputLength) {
        return decode(input, output, inputLength);
    }
}

最后为你选择合适的工厂:

public class CodecFactory {
    ICodec instance = null;

    public ICodec GetCodec() {
        if (instance == null) {
            if (IntPtr.Size == 4) {
                instance = new CodecX86();
            } else if (IntPtr.Size == 8) {
                instance = new CodecX64();
            } else {
                throw new NotSupportedException("Unknown platform");
            }
        }
        return instance;
    }
}

由于DLL在第一次被调用时被懒惰地加载,这实际上是有效的,尽管每个平台只能加载原生的版本。有关更详细的说明,请参阅this article

答案 2 :(得分:2)

我想出的最好成绩如下:

  • 使用两个名为64或32
  • 的DLL分发我的应用程序
  • 在主要启动代码中包含以下内容:
    
    File.Delete(Application.StartupPath + @"\scilexer.dll");
    {
      // Check for 64 bit and copy the proper scilexer dll
        if (IntPtr.Size == 4)
        {
          File.Copy(Application.StartupPath + @"\scilexer32.dll",
            Application.StartupPath + @"\scilexer.dll");
        }
        else
        {
          File.Copy(Application.StartupPath + @"\scilexer64.dll",
            Application.StartupPath + @"\scilexer.dll");
        }
    }

答案 3 :(得分:1)

您可以将dll放在system32中。 syswow64中的32位和实际system32中的64位。对于32位应用程序,当访问system32时,它们将被重定向到Syswow64。

您可以在注册表中创建一个条目。软件密钥有一个名为Wow6432Node的子项,32位应用程序将其视为软件密钥。

以下是powershell installer does

答案 4 :(得分:0)

非托管dll可以与其管理对应方并排安装到GAC中。 This article应该解释它是如何运作的。