I'm trying to dynamically load the appropriate x86/x64 version of the SQLite3.DLL at runtime for use with the Devart.SQLite.DLL. I don't have control over installing the appropriate version of the DLL to the application root beforehand, so I must somehow try and get the correct version from either a /x86 or /x64 subdirectory from the application root.
Any ideas on how to accomplish this? Admittedly, I'm completely lost here. My code thus far is:
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
EDIT As mentioned in the comments, I failed to specify what the actual error I was receiving when trying to load the sqlite3.dll. As it turns out, I was missing the following in my 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>
Once I added this to the App.Config, my previous code sample worked as expected. Thank everyone for their help.