簡體   English   中英

部分客戶端無法加載 SQLite.Interop.dll

[英]Some clients are unable to load SQLite.Interop.dll

有一個應用程序使用 SQLite。 Link to System.Data.SQLite and SQLite.Interop.dll (for x64 and x86 platforms) added statically, version 1.0.112.0. (所有 3 個文件)(SQLite 通過 NuGet 添加到測試項目,並從那里復制到當前項目)。 The System.Data.SQLite and SQLite.Interop.dll files (in the x86 and x64 folders) are present on the client machine. .Net 版本是 4.7.2。

大多數客戶端工作正常,但少數客戶端會拋出異常,並顯示“無法加載 DLL 'SQLite.Interop.dll'”文本。 I build test versions for x86 and x64 on my machine, in each case SQLite.Interop.dll was loaded from the corresponding folder (If SQLite.Interop.dll does not exist on the corresponding path, then there was an exception).

在所有客戶端計算機上如何避免此異常?

有一種預感,客戶端缺少 Microsoft Visual C ++ Redistributable。 但后來我發現安裝了 Redistributable。

下一個假設是系統可以找到SQLite.Interop.dll,但是在加載或初始化過程中發生錯誤,被包裝器(System.Data.SQLite)解釋為DllNotFoundException。

也許指向 dll 的路徑太長了?

當指向 dll 的路徑太長(超過 255 個字符)時,我們遇到了類似的問題。

經過長時間的搜索,找到了DllNotFoundException的原因。 這是因為 SQLite.Interop.dll 依賴於 msvcr120。 (Dependency Walker 被用來解決這個問題)。 但是有個小問題,如果應用是針對x64平台的,那么這個文件預計在C:\Windows\System32文件夾下。 但是如果平台是 x86,那么文件應該在路徑 C:\Windows\SysWOW64。 我看到 msvcr120 在 System32 文件夾中,並確定問題與 msvcr120 無關。

最簡單的解決方案是為 x86 平台構建應用程序,並將 msvcr120 放在 exe 文件旁邊。

我找到了解決問題的更好方法。 msvcr120 的對應版本位於 x86 或 x64 文件夾中的 SQLite.Interop.dll 旁邊。 在使用 sqlite 之前(例如,在 Application 構造函數中或其重載的 OnStartup() 方法中),我們需要將文件夾添加到 PATH 環境變量中。

var pathVar = Environment.GetEnvironmentVariable("PATH");
       
string customDllFolder;
if (Environment.Is64BitProcess)
{
    customDllFolder = Path.Combine(Environment.CurrentDirectory, "x64");
}
else
{
    customDllFolder = Path.Combine(Environment.CurrentDirectory, "x86");
}

pathVar = string.Concat(pathVar, ";", customDllFolder);
Environment.SetEnvironmentVariable("PATH", pathVar, EnvironmentVariableTarget.Process);

這個解決方案更好,因為如果 msvcr120 不在用戶的機器上,我們只會使用它的本地副本。 我們還可以為兩個平台(x86 和 x64)構建應用程序。 這種方法也可以用於其他非托管庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM