我有一个非托管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?
发布于 2009-03-17 02:31:56
P/Invoke使用LoadLibrary加载DLL,如果已经有一个使用给定名称加载的库,LoadLibrary将返回它。因此,如果您可以为两个版本的DLL提供相同的名称,但将它们放在不同的目录中,则可以在第一次从scilexer.dll调用函数之前执行一次这样的操作,而无需复制外部声明:
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 + ".");发布于 2009-01-31 17:18:01
不幸的是,我对这个特定的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。
发布于 2008-12-18 08:25:26
您可以将动态链接库放在system32中。syswow64中的32位和实际system32中的64位。对于32位应用程序,当他们访问system32时,它们被重定向到Syswow64。
您可以在注册表中创建条目。软件密钥具有名为Wow6432Node的子密钥,32位应用程序将其视为软件密钥。
以下是powershell installer does的内容。
https://stackoverflow.com/questions/377181
复制相似问题