加载 x86 或 x64 程序集 [英] Loading x86 or x64 assembly

查看:34
本文介绍了加载 x86 或 x64 程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个版本的 System.Data.SQLite.DLL - 适用于 x86 和 x64 平台.x86 版本保存在 application 文件夹中,x64 版本保存在 appFolderx64 文件夹中.应用程序编译为 AnyCPU.如何根据 windows 平台加载所需版本的 SQLite?

I have two versions of System.Data.SQLite.DLL - for x86 and x64 platform. The x86 version keeps in application folder and x64 version keeps in appFolderx64 folder. The application compiled as AnyCPU. How can i load needed version of SQLite according to windows platform?

推荐答案

如果您使用的是来自 http://system.data.sqlite 的 SQLite.org,System.Data.SQLite.DLL 是完全托管的.有一个底层本机 DLL SQLite.Interop.DLL,它需要根据进程(32 位或 64 位)进行更改.

If you are using SQLite from http://system.data.sqlite.org, the System.Data.SQLite.DLL is completely managed. There is an underlying native DLL, SQLite.Interop.DLL, that needs to change depending on the process (32- or 64-bit).

对于 64 位,我在.NativeX64"和 32 位的.NativeX86"中部署本机库.在运行时 P/Invoke SetDllDirectory 设置指向进程正确路径的 DLL 加载目录.http://msdn.microsoft.com/en-us/library/ms686203(v=vs.85).aspx

I deploy the native libraries in ".NativeX64" for 64-bit and ".NativeX86" for 32-bit. At runtime P/Invoke SetDllDirectory to set the DLL load directory pointing at the correct path for the process. http://msdn.microsoft.com/en-us/library/ms686203(v=vs.85).aspx

(请注意,我不熟悉 http://sqlite.phxsoftware 中遗留的 System.Data.SQLite.DLL 版本的架构.com)

(Note that I'm not familiar with the architecture of the legacy System.Data.SQLite.DLL version from http://sqlite.phxsoftware.com)

private static class NativeMethods
{
    [DllImport("kernel32.dll", CallingConvention = CallingConvention.Cdecl)]
    internal static extern bool SetDllDirectory(string pathName);
}

... 

    // Underlying SQLite libraries are native. 
    // Manually set the DLL load path depending on the process.
    var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Native");
    if(IntPtr.Size == 8) // or: if(Environment.Is64BitProcess) // .NET 4.0
    {
        path = Path.Combine(path, "X64");
    }
    else
    {
        // X32
        path = Path.Combine(path, "X86");
    }
    NativeMethods.SetDllDirectory(path);

这篇关于加载 x86 或 x64 程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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