Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8936245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:10:34+00:00 2026-06-15T10:10:34+00:00

I’m trying to dynamically load the appropriate x86/x64 version of the SQLite3.DLL at runtime

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:10:35+00:00Added an answer on June 15, 2026 at 10:10 am

    As it turns out, the original code sample I had did work, but I failed to properly register the DBProviderFactory for SQLite in my program’s App.Config. I now can bundle the x86 and x64 sqlite3.dlls in their respective /x86 and /x64 directory from the application root at have the proper version loaded at runtime. For those looking for a full solution, see below (thanks everyone for your code improvements; they have been incorporated):

    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>
    

    AssemblyLoader.vb

    Imports System.IO
    Imports System.Reflection
    Imports System.Security
    
    ''' <summary>
    ''' Handles dynamically loading managed and unmanaged assemblies.
    ''' </summary>
    Public Class AssemblyLoader
    
      ''' <summary>
      ''' Loads the appropriate x86/x64 version of an assembly based on its filename.
      ''' </summary>
      ''' <param name="myAssembly">The filename of the assembly.</param>
      ''' <param name="isManaged">True if the assembly is managed, otherwise False.</param>
      ''' <exception cref="ArgumentException">If myAssembly is invalid, such as an assembly with an invalid culture.</exception>
      ''' <exception cref="SecurityException">The caller does not have path discovery permission.</exception>
      ''' <exception cref="BadImageFormatException">Thrown if myAssembly is not a valid assembly. -or-Version 2.0 or later of the common language runtime is currently loaded and assemblyRef was compiled with a later version.</exception>
      ''' <exception cref="FileLoadException">An assembly or module was loaded twice with two different evidences.</exception>
      ''' <exception cref="AppDomainUnloadedException">The operation is attempted on an unloaded application domain.</exception>
      Public Shared Sub LoadByVersion(ByVal myAssembly As String, Optional ByVal isManaged As Boolean = True)
        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 Not File.Exists(filename) Then Exit Sub
            If isManaged Then
                an = AssemblyName.GetAssemblyName(filename)
                If Not IsNothing(an) Then AppDomain.CurrentDomain.Load(an)
            Else
                LoadLibraryW(filename)
            End If
        Catch ex As ArgumentException
            Throw
        Catch ex As SecurityException
            Throw
        Catch ex As BadImageFormatException
            Throw
        Catch ex As FileLoadException
            Throw
        Catch ex As AppDomainUnloadedException
            Throw
        End Try
      End Sub ' LoadAssembly
    
      ''' <summary>
      ''' Gets the absolute path of the dependant assembly.
      ''' </summary>
      ''' <param name="assembly">The filename (without path) of the dependent assembly.</param>
      ''' <param name="version">The subfolder containing the version of the assembly needed (e.g. "x86", "x64").</param>
      ''' <returns>The absolute path of the specific version of the assembly.</returns>
      Private Shared Function GetAssemblyPath(ByVal assembly As String, ByVal version As String) As String
        Return Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), version, assembly)
      End Function ' GetAssemblyName
    
      ''' Return Type: HMODULE->HINSTANCE->HINSTANCE__*
      '''lpLibFileName: LPCWSTR->WCHAR*
      <Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint:="LoadLibraryW")> _
      Private Shared Function LoadLibraryW(<Runtime.InteropServices.InAttribute()> <Runtime.InteropServices.MarshalAsAttribute(Runtime.InteropServices.UnmanagedType.LPWStr)> lpLibFileName As String) As IntPtr
      End Function ' LoadLibraryW
    
    End Class ' AssemblyLoader
    

    ** MyApp **

    <snip>
    Public Sub New()
        Try
            AssemblyLoader.LoadByVersion("sqlite3.dll", False)
        Catch ex As Exception
            ' Error logging done here
        End Try
    End Sub
    </snip>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.