我试图在运行时动态加载 SQLite3.DLL 的适当 x86/x64 版本,以便与 Devart.SQLite.DLL 一起使用。我无法事先将适当版本的 DLL 安装到应用程序根目录,因此我必须以某种方式尝试从应用程序根目录的 /x86 或 /x64 子目录中获取正确的版本。
关于如何做到这一点的任何想法?诚然,我在这里完全迷失了。到目前为止,我的代码是:
Public Sub New()
LoadAssembly("sqlite3.dll", True)
End Sub
Private Function GetAssemblyPath(ByVal assembly As String, ByVal version As String) As String
Return Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location) & "\" & version & "\" & assembly
End Function ' GetAssemblyName
<Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint:="LoadLibraryW")> _
Public Shared Function LoadLibraryW(<Runtime.InteropServices.InAttribute()> <Runtime.InteropServices.MarshalAsAttribute(Runtime.InteropServices.UnmanagedType.LPWStr)> lpLibFileName As String) As IntPtr
End Function
Private Sub LoadAssembly(ByVal myAssembly As String, Optional ByVal doLoadLibrary As Boolean = False)
Dim an As AssemblyName
Dim filename As String
Dim version As String
If UIntPtr.Size = 8 Then version = "x64" Else version = "x86"
filename = GetAssemblyPath(myAssembly, version)
Try
If doLoadLibrary Then
HostLog.WriteEntry(filename, EventLogEntryType.Information)
Dim ptr As IntPtr = LoadLibraryW(filename)
HostLog.WriteEntry(ptr.ToString(), EventLogEntryType.Information)
Else
an = AssemblyName.GetAssemblyName(filename)
AppDomain.CurrentDomain.Load(an)
End If
Catch ex As Exception
HostLog.WriteEntry(ex.Message, EventLogEntryType.Error)
End Try
End Sub ' LoadAssembly
编辑 正如评论中提到的,我没有指定我在尝试加载 sqlite3.dll 时收到的实际错误。事实证明,我的 App.Config 中缺少以下内容:
<system.data>
<DbProviderFactories>
<remove invariant="Devart.Data.SQLite" />
<add name="dotConnect for SQLite"
invariant="Devart.Data.SQLite"
description="Devart dotConnect for SQLite"
type="Devart.Data.SQLite.SQLiteProviderFactory, Devart.Data.SQLite, Version=4.2.122.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
</DbProviderFactories>
</system.data>
将其添加到 App.Config 后,我之前的代码示例按预期工作。感谢大家的帮助。