从.NET 32位或64位的DLL加载管理code [英] 32 or 64 bit DLL loading from .Net managed code

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

问题描述

我有一个非托管的DLL(的Scin​​tilla的code主编的scilexer.dll,从 $ C使用Scintilla.Net $ CPLEX )是从托管应用程序加载线槽Scintilla.Net组件。窗户托管应用程序运行没有在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 Framework的DLL加载器加载取决于一些的.config选项或一些路径名神奇的非托管的DLL在32位或64位格式东东?

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 /调用使用LoadLibrary来加载DLL文件,如果有已经装载了一个给定的名称的库,调用LoadLibrary将返回。所以,如果你可以给该DLL的两个版本相同的名字,但把它们放在不同的目录中,你可以做这样的事情,从scilexer.dll函数的第一次调用之前只有一次,而无需复制你的外部声明:

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加载管理code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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