How to merge System.Data.SQLite.DLL in EXE without using Assembly.LoadFile()

0

System.Data.SQLite.DLL is a mixed code DLL. It contains C and C#. I know how to add it as Embedded Resource, write it to a temp file and use Assembly.LoadFile() to load it.

My question is there any alternative way to load it without writing it to a temp file? I wish to combine it with EXE into single assembly.

thans for any advice


Responses to Answers:

To: Oleg Ignatov

Hi, I modified it to load like this:

static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    Assembly asm = null;
    AppDomain domain = (AppDomain)sender;
    if (args.Name.Contains("System.Data.SQLite"))
    {
        try
        {
            asm = domain.Load(WindowsFormsApplication24.Properties.Resources.System_Data_SQLite);
        }
        catch (Exception ex)
        {
            Form f = new Form();
            TextBox t = new TextBox(); t.Dock = DockStyle.Fill;
            t.Multiline = true; t.ScrollBars = ScrollBars.Both;
            f.Controls.Add(t);
            t.Text = ex.ToString();
            f.ShowDialog();
        }
    }
    return asm;
}

It generates the exception message of this:

System.IO.FileLoadException: Could not load file or assembly 'System.Data.SQLite, Version=1.0.82.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. Attempt to load an unverifiable executable with fixups (IAT with more than 2 sections or a TLS section.) (Exception from HRESULT: 0x80131019)
File name: 'System.Data.SQLite, Version=1.0.82.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' ---> System.IO.FileLoadException: Attempt to load an unverifiable executable with fixups (IAT with more than 2 sections or a TLS section.) (Exception from HRESULT: 0x80131019)
   at System.Reflection.RuntimeAssembly.nLoadImage(Byte[] rawAssembly, Byte[] rawSymbolStore, Evidence evidence, StackCrawlMark& stackMark, Boolean fIntrospection, SecurityContextSource securityContextSource)
   at System.AppDomain.Load(Byte[] rawAssembly)
   at WindowsFormsApplication24.Program.CurrentDomain_AssemblyResolve(Object sender, ResolveEventArgs args)

This has the same result as I do it previously like this:

byte[] ba = null;
Assembly asm = null;
Assembly curAsm = Assembly.GetExecutingAssembly();
string embeddedResource = "WindowsFormsApplication24.System.Data.SQLite.dll";
using (Stream stm = curAsm.GetManifestResourceStream(embeddedResource))
{
    ba = new byte[(int)stm.Length];
    stm.Read(ba, 0, (int)stm.Length);
    asm = Assembly.Load(ba);
}
return asm;
c#
system.data.sqlite
asked on Stack Overflow Jan 16, 2013 by mjb • edited Jan 16, 2013 by mjb

2 Answers

0

You can handle AssemblyResolve event and use AppDomain.Load method:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    AppDomain domain = (AppDomain)sender;
    if (args.Name.Contains("YourDll"))
    {
        return domain.Load(WindowsFormsApplication1.Properties.Resources.YourDll);
    }
    return null;
}
answered on Stack Overflow Jan 16, 2013 by Oleg Ignatov
0

Did you check on your targeted machine is MS Visual C++ runtime redistributable installed?

if not, you can dynamic link Visual C++ runtime redistributable to your project, as System.Data.SQLite is depend on Visual C++ runtime redistributable.

.Net 2   - MS Visual C++ 2005 redistributable
.Net 3   - MS Visual C++ 2008 redistributable
.Net 4   - MS Visual C++ 2010 redistributable
.Net 4.5 - MS Visual C++ 2012 redistributable
answered on Stack Overflow Dec 8, 2014 by Leng Weh Seng

User contributions licensed under CC BY-SA 3.0