C# 无法加载 DLL 'SQLite.Interop.dll'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13028069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:24:06  来源:igfitidea点击:

Unable to load DLL 'SQLite.Interop.dll'

c#visual-studio-2010sqlite

提问by xll

Periodically I am getting the following exception:

我定期收到以下异常:

Unable to load DLL 'SQLite.Interop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Unable to load DLL 'SQLite.Interop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

I am using 1.0.82.0. version, installing it with nuget in VS2010, OS Win7 64.

我正在使用 1.0.82.0。版本,在 VS2010,OS Win7 64 中使用 nuget 安装它。

Once exception starts to appear, it appears constantly - in debug and release and running application within or outside VS.

一旦异常开始出现,它就会不断出现 - 在 VS 内部或外部调试和发布和运行应用程序。

The only way to stop it is logoff and logon. The exception is not thrown and dll is loaded. It can work for days, but then it can break again.

停止它的唯一方法是注销和登录。不会抛出异常并加载 dll。它可以工作数天,但随后又会坏掉。

Has anyone seen something like this and is there a solution for it?

有没有人看到过这样的事情并且有解决方案吗?

回答by Clay Compton

Could there be contention for the assembly? Check to see whether there's another application with a file lock on the DLL.

大会会不会有争执?检查是否有另一个应用程序在 DLL 上锁定了文件。

If this is the reason, it should be easy to use a tool like Sysinternal's Process Explorerto discover the offending program.

如果这是原因,那么使用Sysinternal 的 Process Explorer 之类的工具来发现有问题的程序应该很容易。

HTH, Clay

HTH, 粘土

回答by Caleb Kiage

I had this same problem when using SQLite in a WPF project whose platform target was Any CPU. I fixed it by following the following steps:

在平台目标为Any CPU. 我按照以下步骤修复了它:

  1. Open the project designer in Visual Studio. Details on how to do it can be found here.
  2. Click on the Build tab.
  3. Disable the prefer 32-bitoption.
  1. 在 Visual Studio 中打开项目设计器。可以在此处找到有关如何操作的详细信息。
  2. 单击构建选项卡。
  3. 禁用该prefer 32-bit选项。

Alternatively, you could just set the platform target to x86or x64. I think this problem is caused by the System.Data.SQLitelibrary using the platform target to get the location of the 'SQLite.Interop.dll' file.

或者,您可以将平台目标设置为x86x64。我认为这个问题是由System.Data.SQLite库使用平台目标来获取'SQLite.Interop.dll'文件的位置引起的。

UPDATE:

更新:

In case the project designer cannot be reached, just open the project (*.csproj) file from a text editor and add the value <Prefer32Bit>false</Prefer32Bit>into the <PropertyGroup>...</PropertyGroup>tag.

如果无法联系到项目设计者,只需*.csproj从文本编辑器打开项目 ( ) 文件并将值添加<Prefer32Bit>false</Prefer32Bit><PropertyGroup>...</PropertyGroup>标记中。

Example code

示例代码

<PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>[Set by Visual Studio]</ProjectGuid>
    <OutputType>Exe</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>[Set by Visual Studio]</RootNamespace>
    <AssemblyName>[Set by Visual Studio]</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>[Set by Visual Studio]</FileAlignment>
    <!--Add the line below to your project file. Leave everything else untouched-->
    <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>

回答by Jesse McDowell

I don't know if it's a good answer, but I was able to solve this problem by running my application under an AppDomain with an identity of "Local System".

我不知道这是否是一个好的答案,但我能够通过在具有“本地系统”标识的 AppDomain 下运行我的应用程序来解决这个问题。

回答by Kevin Smathers

The default installation of the multi-architecture (x86, x64) version of SQLite from NuGet exhibits the behavior that you described. If you would like to load the correct version for actual architecture that the .NET runtime chose to run your application on your machine, then you can give the DLL loader a hint about where to locate the correct library as follows:

来自 NuGet 的 SQLite 多体系结构(x86、x64)版本的默认安装表现出您描述的行为。如果您想为 .NET 运行时选择在您的机器上运行您的应用程序的实际架构加载正确的版本,那么您可以向 DLL 加载程序提供有关在何处找到正确库的提示,如下所示:

Add a declaration for the kernel32.dll function call to SetDLLDirectory() before your Program.Main():

在你的 Program.Main() 之前添加一个 kernel32.dll 函数调用到 SetDLLDirectory() 的声明:

    [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
    [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
    static extern bool SetDllDirectory(string lpPathName);

Then use your own method for determining the correct subdirectory to find the architecture specific version of 'SQLite.Interop.dll'. I use the following code:

然后使用您自己的方法来确定正确的子目录以查找“SQLite.Interop.dll”的体系结构特定版本。我使用以下代码:

    [STAThread]
    static void Main()
    {
        int wsize = IntPtr.Size;
        string libdir = (wsize == 4)?"x86":"x64";
        string appPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
        SetDllDirectory(System.IO.Path.Combine(appPath, libdir));

回答by Kugel

I know I'm late to the party but I had this issue right after I pulled down latest x86/x64 today (version 1.0.88.0). My local IIS in VS2012 runs 32bit by default and there's no easy way to switch to x64. My production server runs 64bit.

我知道我参加聚会迟到了,但是在我今天拉下最新的 x86/x64(版本 1.0.88.0)之后我就遇到了这个问题。我在 VS2012 中的本地 IIS 默认运行 32 位,并且没有简单的方法可以切换到 x64。我的生产服务器运行 64 位。

Anyway I installed the NuGet package to a DLL project and I got this error. What I had to do to get it working I had to install it to the main siteproject, too. Even if it doesn't touch SQLite classes at all.

无论如何,我将 NuGet 包安装到 DLL 项目中,但出现此错误。我必须做些什么才能让它工作,我也必须将它安装到主站点项目中。即使它根本不涉及 SQLite 类。

My guess is that SQLite uses the entry assembly to detect which version of Interop to load.

我的猜测是 SQLite 使用入口程序集来检测要加载哪个版本的互操作。

回答by Wil

This is how I fixed it in my project.

这就是我在我的项目中修复它的方式。

It was working, and when a colleague submitted his changes, I received the "Unable to load DLL 'SQLite.Interop.dll'" exception.

它正在工作,当一位同事提交他的更改时,我收到了“无法加载 DLL 'SQLite.Interop.dll'”异常。

Diffing the project's .csproj file, this was in the NON-WORKING version:

比较项目的 .csproj 文件,这是在非工作版本中:

<ItemGroup>
     <Content Include="x64\SQLite.Interop.dll" />
     <Content Include="x86\SQLite.Interop.dll" />
</ItemGroup>

And this is what the WORKING version had:

这就是 WORKING 版本所具有的:

<ItemGroup>
     <Content Include="x64\SQLite.Interop.dll">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </Content>
      <Content Include="x86\SQLite.Interop.dll">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </Content>
</ItemGroup>

After reverting back, I didn't receive the exception. The DLL files were dumped in the appropriate Debug\x64 (etc) folders.

恢复后,我没有收到异常。DLL 文件被转储到相应的 Debug\x64 (etc) 文件夹中。

回答by pwrgreg007

I am working on a simple console application to add some test data to an SQLite database and was getting this error. The configuration for the project is "Any CPU". I fixed it by copying the SQLite.Interop.dll to the bin\debug folder. A better way would be to use the method by @Wil, but how do you specify this for "Any CPU" configuration?

我正在开发一个简单的控制台应用程序,将一些测试数据添加到 SQLite 数据库中,但出现此错误。项目的组态为“任何 CPU”。我通过将 SQLite.Interop.dll 复制到 bin\debug 文件夹来修复它。更好的方法是使用@Wil 的方法,但是如何为“任何 CPU”配置指定此方法?

回答by Morten Holmgaard

You could also get this error if you are trying to run a 32 bit dll, in a 64 bit project.

如果您尝试在 64 位项目中运行 32 位 dll,您也可能会收到此错误。

I got this when I have placed the same file(SQLite.Interop.dll in 32 bit version) in both the x86 and x64 folder.

当我在 x86 和 x64 文件夹中放置相同的文件(32 位版本的 SQLite.Interop.dll)时,我得到了这个。

回答by Formentz

even if it is an old post, I'd like to share the solution that I found here: http://system.data.sqlite.org/index.html/info/54e52d4c6f

即使是旧帖子,我也想分享我在这里找到的解决方案:http: //system.data.sqlite.org/index.html/info/54e52d4c6f

If you don't want to read all the issue, the solution is to copy the file "msvcr100.dll" (that can be found in Windows\System32 directory) in the same path as SQLite.Interop.dll.

如果您不想阅读所有问题,解决方案是将文件“msvcr100.dll”(可以在 Windows\System32 目录中找到)复制到与 SQLite.Interop.dll 相同的路径中。

I would advice to read the issue to understand why, and to include the file in your setup but to install it only if the error occurs, I made it an optional component selectable in the setup options.

我建议阅读该问题以了解原因,并将该文件包含在您的设置中,但仅在发生错误时才安装它,我将其设置为可在设置选项中选择的可选组件。

HTH, Formentz

HTH, 福门茨

回答by Elshan

If you download correct binaryfor SQLitethen copy SQLite.Interop.dllinto your Releaseor Debugfolder according to your project build option.

如果您下载正确的二进制文件SQLite然后根据您的项目构建选项复制SQLite.Interop.dll到您的ReleaseDebug文件夹中。