C# How To Embed And Reference An External Assembly (DLL) - Windows Forms

Поділитися
Вставка
  • Опубліковано 21 гру 2024

КОМЕНТАРІ • 112

  • @IgraphyRupage
    @IgraphyRupage 2 роки тому +1

    Thanks! This video is still relevant in vs2019 in 2022

  • @zenonclips
    @zenonclips 7 років тому

    I have seen many tutorials on this subject. This is the only one that actually worked. Thanks!!!

  • @MrFlashXs
    @MrFlashXs 10 років тому +1

    Thanks man, this was a case where looking at a written tutorial was too much to handle but you broke it down quite well :)

  • @confusedkemono
    @confusedkemono 9 років тому

    oh my god, ive been looking everywhere on how to embed and use DLL assemblies, thank you so much!

  • @jawfox000
    @jawfox000 11 років тому

    you are the best in coding and your videos are really worth to watch !

  • @daniel1antonio2
    @daniel1antonio2 5 років тому +2

    this is exactly what I was looking for, thank bro.

  • @NONAME-dj2gm
    @NONAME-dj2gm 4 роки тому

    helped me a lot, now I can start our hands-on activity.

  • @Alana9374
    @Alana9374 9 років тому

    This is amazing! Yes! I was looking all over for an easy and practical way to do this. I saw a lot of ILMERGE. that's a labyrinth! You are AWESOME.

  • @Cubinator73
    @Cubinator73 11 років тому

    Thank you so much. You don't know how much I was looking for this.

  • @mohamedselim7635
    @mohamedselim7635 3 роки тому +1

    Bro you are a life saver ❤❤❤

  • @etoth19
    @etoth19 4 роки тому

    Very educative. I was looking for how to call a third party's COM dll's functions, which is still not explained well on videos I found so far. I wish to see a sweet and short video like this on how to interface with COM' from .NET

  • @supporttradetech8438
    @supporttradetech8438 11 років тому +6

    Its work for single Dll but what will i do as i have multiple Dll ?
    Thanks

    • @spacesaver4063
      @spacesaver4063 6 років тому

      The last picture of code is incomplete and not working i.e. TerminalServicesManager

  • @BjrnWillyStokkenes
    @BjrnWillyStokkenes 10 років тому +2

    Succinctly. Great. This was just was I was looking for. Very helpful. Thanx.

  • @hudson08buster
    @hudson08buster 7 років тому

    This saved my butt today. Thanks for the tutorial.

  • @huongmai8224
    @huongmai8224 4 роки тому

    does it work for class library? I tried but it didn't work. Not sure if it my mistake

  • @desaird
    @desaird 9 років тому +1

    If more than one DLL file ....?

  • @mujtabaomar3741
    @mujtabaomar3741 4 роки тому

    if i change their places again the first one is working but the second is not working

  • @jacksonn6999
    @jacksonn6999 4 роки тому

    System.NullReferenceException: 'Object reference not set to an instance of an object.'
    stream was null.

  • @ak_8.8.8
    @ak_8.8.8 3 роки тому

    Why does debugger says, "System.NullReferenceException: 'Object reference not set to an instance of an object.
    stream was null."
    Break when this exception type is thrown.
    what's this about?
    Btw it still worked!

  • @Abdullah_khan333
    @Abdullah_khan333 2 роки тому

    how to handle same scenario in case web app, e.g mvc

  • @BetterCoder
    @BetterCoder  11 років тому +1

    Thank you! I appreciate your comment :)

  • @madamsmith9175
    @madamsmith9175 2 роки тому

    How to do you do this for multiple dll

  • @heccafox5178
    @heccafox5178 5 років тому

    Superb mate, got rid of a headache for me!
    Mucho Thanko

  • @sangvu6255
    @sangvu6255 8 років тому

    you can write image from resource to disk?
    I can full example, i try search stackoverflow and very many page, but i don't understand do it, please help me

  • @theroqu
    @theroqu 9 років тому

    Nice, You saved me hours at work today. Thanks a lot :)

  • @actiontower
    @actiontower 10 років тому +7

    thanks man it help me so much But Please Can You tell How To Embed And Reference (2) External Assembly ???
    not just one ..

  • @davidvargas85
    @davidvargas85 10 років тому

    Thanks for this tutorial, it was exactly what I was looking for... :)

  • @nattsurfaren
    @nattsurfaren 4 роки тому

    Doesn't work for me. I get file missing exception before the assemblyResolve

  • @chefbennyj
    @chefbennyj 2 роки тому

    9 yrs later ... What if the embedded resource dll you want to side load has dependancies on other .dll that are also embedded resources.

  • @Siminfrance
    @Siminfrance 5 років тому +1

    Very cool explanation, you should have kept doing videos.

  • @CorruptKrown333
    @CorruptKrown333 7 років тому

    I did every step and even tried Steve Hall's code below. I still get an error 'Could not load file or assembly' :-(

  • @mujtabaomar3741
    @mujtabaomar3741 4 роки тому

    Thank you it was greate but i want to add two dll files but i couldnt do could you help my with my source code how to do that i have to say that the first one is working but the second is not working.

  • @jianjiao542
    @jianjiao542 4 роки тому

    Thank you! Good Example and nice song :)

  • @CodingWithAS
    @CodingWithAS 5 років тому

    Thanks for sharing with us.
    Do you know, How we can load dell from another folder rather then embedded?

  • @Dimon_In_The_Game
    @Dimon_In_The_Game 4 роки тому +1

    Thank you, you helped a lot to solve the problem

  • @mimoa21
    @mimoa21 4 роки тому

    Very nice video tutorial. But how to assemble it, when I have 2 .dll libraries? I get the error that program cannot find the method for example, because it searches it in wrong .dll.

  • @AmmarTheTrainer
    @AmmarTheTrainer 7 років тому

    What is the use of "extern" keyword in C# ?

  • @GregPayne1
    @GregPayne1 9 років тому +1

    Is there a way to do this for a class library? I have written a class library using an external dll but only want to ship one dll.
    Thanks,

    • @szymonliszewski9457
      @szymonliszewski9457 6 років тому

      Hi! Have you found a solution?

    • @amaillo
      @amaillo 5 років тому

      @@szymonliszewski9457 You can use Activator to load a class dynamicaly from a managed dll.

  • @sigmanor
    @sigmanor 11 років тому

    trying to do this for System.Data.SQLite.dll but dont work =/

  • @UntakenNick
    @UntakenNick 11 років тому

    I got it to work with all the dll's I needed except for system.data.sqlite.dll Could it be that it has dots in the name and the resulting name ends up being "assemblyName.System.Data.SQLite.dll"..?

  • @YousefHurf
    @YousefHurf 7 років тому +1

    Please Help Me!!!
    The Code Its Work 100%
    But When i Add 2 Or More .dll
    And Copy The Code And Change The Event Name
    I dont have any error
    But the first Assembly its only work Only!
    Can help my please??

    • @jinhwang7
      @jinhwang7 7 років тому +3

      Hope it's not too late! This seem to help me for multiple DLLs, and I'm hopping by sharing this can also help you!
      String resourceName = new AssemblyName(args.Name).Name + ".dll";
      string resource = Array.Find(Assembly.GetExecutingAssembly().GetManifestResourceNames(), element => element.EndsWith(resourceName));
      using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
      {
      Byte[] assemblyData = new Byte[stream.Length];
      stream.Read(assemblyData, 0, assemblyData.Length);
      return Assembly.Load(assemblyData);
      }
      Good luck!!!

    • @jinhwang7
      @jinhwang7 7 років тому +2

      Got it from here while ago.
      stackoverflow.com/questions/6920920/embedding-dlls-into-exe-in-in-visual-c-sharp-2010

    • @bastarsanchannel8011
      @bastarsanchannel8011 3 роки тому

      @@jinhwang7 you sir, are my savior

  • @Sworn973
    @Sworn973 7 років тому

    I know this video is old, but is it possible to expose the embed DLL that you can refer it from another project? Lets assuming your second project was a class library, and you wanna add only that DLL to your final project and be able to call the "music" dll in you final project.

  • @mizantednioc
    @mizantednioc 5 років тому

    Very helpful. It worked great ! I really appreciate it !

  • @joebrady9829
    @joebrady9829 11 років тому +1

    Great video! I've been reading about this all day with no luck. How would one set this up with multiple .dll files?

  • @Norway174
    @Norway174 9 років тому

    Anyone know how to do this for VB.net? This is the only tutorial I can find on Google and UA-cam which related to the problem I'm having. I've been looking all over, without any luck for VB.net.
    I just need to embed this dll "Newtonsoft.Json.dll" in to my EXE. Any help would be greatly appreciated as I've been stuck with this for days now.

  • @UntakenNick
    @UntakenNick 11 років тому

    I read that it can't be merged into the exe because of something related to it using a C++ library.. anyway, I went from having four dll's to one.

  • @solvedplus858
    @solvedplus858 5 років тому

    very good
    thanks for your valuable tutorial
    i have plugin contains many dll files all in same directory
    i load the first one using reflection but my app can't read the other dll file and throw error
    how can i solve that without embedded it because those dlls are projects in my app?
    many thanks

  • @sigmanor
    @sigmanor 9 років тому +3

    does not work, error:
    HRESULT: 0x80004003 (E_POINTER))

  • @bansal1988
    @bansal1988 8 років тому

    @Betteroder:
    does not work, the code won't even compile after doing all that you did.
    for ref: .dll that I an trying to embed is "Microsoft.Msagl.dll"

  • @rahul9586
    @rahul9586 10 років тому

    awesome!. exactly what i needed. well explained ! cheers.

  • @anandgupta1366
    @anandgupta1366 6 років тому

    sir may i know how to let the dev express dll be in one sub folder and exe out of it and the exe should run properly .
    i mean Is it possible to that dev express dll /assemblies in a separate folder? and exe should run .

  • @pardalrocha
    @pardalrocha 6 років тому

    The link for source code download is broken.

  • @BetterCoder
    @BetterCoder  11 років тому

    No, that shouldn't be a problem, since that is the full path to the file. Could be that it is resolving to the System.Data.SQLite.dll on the computer, and not looking for it in your assembly.

  • @rubencaballerollamas2176
    @rubencaballerollamas2176 7 років тому

    does this also work with a .exe?

  • @manjeetnandal1
    @manjeetnandal1 5 років тому

    Great sharing!
    Many thanks!

  • @jinhwang7
    @jinhwang7 7 років тому

    Thank you very much for sharing this knowledge!!!
    I understand this is an old tutorial in which I find it still very effective for my WinFormApp. However, I like to apply the same solution to my ConsoleApp, but can't seems to work. Can someone please help?
    Thank you very much!

  • @EwertonDutra
    @EwertonDutra Рік тому

    Even in visual studio 2022 there isn't an easier way to do this, isn't there?
    Just maybe mark something and the .exe gets bigger no

  • @rickhamilton663
    @rickhamilton663 6 років тому

    Thanks for this, brilliant.

  • @mujtabaomar3741
    @mujtabaomar3741 4 роки тому

    i need your help

  • @BetterCoder
    @BetterCoder  11 років тому

    Haha you're welcome. That's why I made this video too, I was searching forever, then came up with a solution!

  • @fayax
    @fayax 6 років тому

    Excellent video! btw any chance you can make one of these for embedding EPPLUS or other nuget packages while working in vs 2017 community edition? Just asking :)

  • @stevejessup2937
    @stevejessup2937 3 роки тому

    You did a return inside of a using block so that may prevent the stream.Dispose() method from being executed.

    • @BetterCoder
      @BetterCoder  3 роки тому

      The compiler makes sure that the object is disposed before control leaves the block -- regardless of how it leaves the block.

    • @stevejessup2937
      @stevejessup2937 3 роки тому +1

      @@BetterCoder Created a test project. You are correct.

  • @Joshk326
    @Joshk326 10 років тому

    Will this work with like a ps3lib dll or like a theme dll?

    • @senzaii7455
      @senzaii7455 10 років тому

      if i open a rtm tool and the barney theme song starts playing i know it's your tool xD
      and yes used this method to add PS3.lib and metro framework :p

  • @SomeNussi
    @SomeNussi 9 років тому

    somehow doesn't work fo me. :/

  • @antoinnelamah9789
    @antoinnelamah9789 7 років тому

    cant i just zip thee thing

  • @ArcxangelTroll23
    @ArcxangelTroll23 11 років тому

    well why dont you pass the code here because the letters are very small and i can´t see
    (Sorry for my english i am mexican)

  • @BetterCoder
    @BetterCoder  11 років тому

    Unfortunately, that will also throw an exception if the assembly that is trying to be resolved isn't one of yours, say a System dll. In the morning I'll try to write up something for ya that will work better :)

  • @erikitakara2094
    @erikitakara2094 10 років тому

    outstanding vídeo tutorial
    congratulations!

  • @restuadipradana6803
    @restuadipradana6803 4 роки тому

    thank you, sangat bermanfaat sekali

  • @cwdgamedev822
    @cwdgamedev822 3 роки тому

    Thank you :)

  • @BetterCoder
    @BetterCoder  11 років тому

    Thank you Ahmed!

  • @shemekiabailey2943
    @shemekiabailey2943 9 років тому

    Note that this does not work for Unmanaged dll's (the ones u cant import as reference, non com components)
    Search google on how to do this.

  • @stevehall3619
    @stevehall3619 10 років тому +18

    Good guide but the exe name is hardcoded in this example. This code will work 100%:
    // To embed a dll in a compiled exe:
    // 1 - Change the properties of the dll in References so that Copy Local=false
    // 2 - Add the dll file to the project as an additional file not just a reference
    // 3 - Change the properties of the file so that Build Action=Embedded Resource
    // 4 - Paste this code before Application.Run in the main exe
    AppDomain.CurrentDomain.AssemblyResolve+=(Object sender, ResolveEventArgs args) =>
    {
    String thisExe=System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
    System.Reflection.AssemblyName embeddedAssembly=new System.Reflection.AssemblyName(args.Name);
    String resourceName=thisExe+"."+embeddedAssembly.Name+".dll";
    using (var stream=System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
    {
    Byte[] assemblyData=new Byte[stream.Length];
    stream.Read(assemblyData, 0, assemblyData.Length);
    return System.Reflection.Assembly.Load(assemblyData);
    }
    };

    • @BetterCoder
      @BetterCoder  9 років тому +1

      Steve Hall Definitely! Nice change to my code! I made this video back when I was still learning basics, and now that I have developed my style, I see many stylistic flaws in quite a few of my videos :P I'm hoping to find the time to start making videos again soon!

    • @TehMapleDude
      @TehMapleDude 9 років тому

      +Steve Hall Post has been edited :(

    • @koreycurtis7876
      @koreycurtis7876 9 років тому

      +Magiichan yeah i don't see any thing

    • @TehMapleDude
      @TehMapleDude 9 років тому

      kirito curtis i can see it through google+ surprisingly...

    • @himak21
      @himak21 8 років тому

      Thanx Its do it .)

  • @ArcxangelTroll23
    @ArcxangelTroll23 11 років тому

    Good work i like it the song je

  • @EmreDemircan
    @EmreDemircan 4 роки тому +2

    EmbededAssembly is your project name.....

  • @michalshulman5091
    @michalshulman5091 8 років тому

    Thank You!!!!!!

  • @aesopcarl
    @aesopcarl 8 років тому

    Very good. I had to use: string[] names = Assembly.GetExecutingAssembly().GetManifestResourceNames(); to get the right name because I missed the "namespace" in front of the dll name (ooops). Thanks!

  • @BetterCoder
    @BetterCoder  11 років тому

    Haha thanks! I thought it would be funny ;)

  • @alexroman6246
    @alexroman6246 8 років тому

    +BetterCoder
    it works thanks!
    what about several dlls? ive seen something like this, but its not working. Maybe you know something about:
    code#
    AppDomain.CurrentDomain.AssemblyResolve += (sender, arg) => { if (arg.Name.StartsWith("YOURDLL")) return Assembly.Load(Properties.Resources.YOURDLL); return null; };
    codeends#

  • @Dragon_Screamer
    @Dragon_Screamer 7 років тому

    "The module was expected to contain an assembly manifest"

  • @TheHacka2000
    @TheHacka2000 7 років тому

    Hey, if anyone is having problems with this here is whats working for me:
    foreach (var item in Assembly.GetExecutingAssembly().GetManifestResourceNames())
    if (item.Contains(new AssemblyName(args.Name).Name))
    return Assembly.LoadFile(item);
    Best of luck!!

  • @Coycxxx
    @Coycxxx 11 років тому

    You deserve your username

  • @joebrady9829
    @joebrady9829 11 років тому

    I figured it out.
    string assemblyName = args.Name.Split(',').First();
    using (var stream = Assembly.GetExecutingAssembly(­­).GetManifestResourceStream("­N­amespace." + assemblyName + ".dll"))
    ...

    • @madamsmith9175
      @madamsmith9175 2 роки тому

      Can you share your full code

    • @joebrady9829
      @joebrady9829 2 роки тому

      @@madamsmith9175 I would if I still had it, haven't worked at that job in over four years though

    • @madamsmith9175
      @madamsmith9175 2 роки тому

      @@joebrady9829 OK would be nice to post the hole code

  • @manjeetnandal1
    @manjeetnandal1 5 років тому

    Great sharing!
    Many thanks!