11

I have a DLL >> System.Data.SQLite.dll

To use it in a normal way > just add it as reference and

using System.Data.SQLite;

then, I can use all the functions inside this DLL.

But, I want to merge my app.exe and this DLL into one single file. I have tried using ILmerge, but fail. As I know, ILmerge cannot merge unmanage DLL.

So, I tried another method > make the DLL as embbed resource. I am able to load it as an assembly with the below code:

Stream stm = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.System.Data.SQLite.dll");
byte[] ba = null;
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
    int read;
    while ((read = stm.Read(buffer, 0, buffer.Length)) > 0)  
    {
        ms.Write(buffer, 0, read);
    }
    ba = ms.ToArray();
}
Assembly sSQLiteDLL = Assembly.Load(ba);

but, how am I going to use the functions in SQLiteDLL?

I also tried add the DLL as resource in properties and load it like this:

public Form1()
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    InitializeComponent();
}

System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    AppDomain domain = (AppDomain)sender;
    if (args.Name.Contains("System_Data_SQLite"))
    {
        return domain.Load(MyApp.Properties.Resources.System_Data_SQLite);
    }
    return null;
}

The above explained what I've got so far and I don't know what to do next to use the DLL? I still can't use the functions inside the DLL.

For example, when I type this:

SQLiteCommand cmd = new SQLiteCommand();

The Visual Studio says:

Error 21 The type or namespace name 'SQLiteCommand' could not be found (are you missing a using directive or an assembly reference?)

Can you share your insight? Thanks.

alont
  • 263
  • 2
  • 3
  • 12
  • 2
    You should be able to combine the methods you have above to achieve this. It does require that the SQLite assembly is added as a reference though, otherwise VS can't know what to do with it. I'm not sure how this solves your native DLL problem though. You'll still need it as a physical file at runtime. – M.Babcock Feb 10 '12 at 13:35
  • You asked a very similar question and got lots of answers including some links etc. - did you checkout these answers/links ? – Yahia Feb 13 '12 at 12:14
  • @Yahia : studying... will take some time. I have tried SmartAssembly and yes it success. Fail to use **ILmerge**. Maybe missout some parameters. For **ILmerge**, the parameters I used is: 'ilmerge /out:MyApp2.exe MyApp.exe System.Data.SQLite.dll' An error occur, its says that The assembly **System.Data.SQLite.dll** is not marked as containing only managed code. Now I will try to manually merge the DLL within the code. I will let you know if success. and hey, thanks for your help. Truely appreciated. – alont Feb 14 '12 at 02:01

2 Answers2

7

You can embed an assembly AND reference it (in VS) at the same time... for the way you want to use it you need to reference it! Any reason you don't reference the Assembly ?

Using a Type from an embedded Assembly (managed) without referencing it is a bit harder but possible using Reflection etc. - see these links (they include reference material AND some sample code etc.):

On embedding managed DLLs you have several options:

OR

  • use some tool like SmartAssembly (commercial)
    it can embed and merge among other things (no need to change your source code)

OR

  • code that yourself in less than 10 lines (free but minimal source code change)
    mark all needed dependencies as "embedded resource" - this way they are included in the EXE file... you need to setup an AssemblyResolve handler which at runtime reads from Resources and returns the needed DLLs to the .NET runtime...
Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • 5
    Hi, Yahia, after some success in some projects. I have decided to write a tutorial for this. Read more: [link](http://www.codeproject.com/Articles/528178/Load-DLL-From-Embedded-Resource) – alont Jan 16 '13 at 15:26
  • The "code that yourself" link is broken for me (404) – Thomas Weller Sep 01 '20 at 11:55
-1

This is not a method to use assemblies loaded in AppDomain. Please read this article: How to: Load Assemblies into an Application Domain

in short you should call GetMethod() with method name (for example SqlCommand) and then call it via .Invoke() method.

Alex F
  • 3,180
  • 2
  • 28
  • 40