如何在EXE中合并System.Data.SQLite.DLL而不使用Assembly.LoadFile()

本文关键字:Assembly LoadFile DLL SQLite EXE 合并 Data System | 更新日期: 2023-09-27 17:53:24

System.Data.SQLite.DLL是一个混合代码DLL。它包含C和c#。我知道如何将其添加为嵌入式资源,将其写入临时文件并使用Assembly.LoadFile()来加载它。

我的问题是有没有其他方法来加载它而不把它写到一个临时文件?我希望将它与EXE合并成一个程序集。

谢谢你的任何建议


对答案的回应:

To: Oleg Ignatov

嗨,我把它修改成这样加载:

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;
}

生成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)

这与我之前这样做的结果相同:

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;

如何在EXE中合并System.Data.SQLite.DLL而不使用Assembly.LoadFile()

您可以处理AssemblyResolve事件并使用AppDomain。加载方法:

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;
}

您是否检查了您的目标机器上是否安装了MS Visual c++运行时可重新分发版?

如果没有,您可以动态链接Visual c++运行时可重分发到您的项目,因为System.Data.SQLite依赖于Visual c++运行时可重分发。

.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