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
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!
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
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.
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.
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"..?
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??
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!!!
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.
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.
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
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 .
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.
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!
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 :)
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 :)
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); } };
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!
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 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#
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!!
Thanks! This video is still relevant in vs2019 in 2022
I have seen many tutorials on this subject. This is the only one that actually worked. Thanks!!!
Thanks man, this was a case where looking at a written tutorial was too much to handle but you broke it down quite well :)
oh my god, ive been looking everywhere on how to embed and use DLL assemblies, thank you so much!
you are the best in coding and your videos are really worth to watch !
this is exactly what I was looking for, thank bro.
helped me a lot, now I can start our hands-on activity.
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.
Thank you so much. You don't know how much I was looking for this.
Bro you are a life saver ❤❤❤
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
Its work for single Dll but what will i do as i have multiple Dll ?
Thanks
The last picture of code is incomplete and not working i.e. TerminalServicesManager
Succinctly. Great. This was just was I was looking for. Very helpful. Thanx.
This saved my butt today. Thanks for the tutorial.
does it work for class library? I tried but it didn't work. Not sure if it my mistake
If more than one DLL file ....?
if i change their places again the first one is working but the second is not working
System.NullReferenceException: 'Object reference not set to an instance of an object.'
stream was null.
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!
how to handle same scenario in case web app, e.g mvc
Thank you! I appreciate your comment :)
How to do you do this for multiple dll
Superb mate, got rid of a headache for me!
Mucho Thanko
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
Nice, You saved me hours at work today. Thanks a lot :)
thanks man it help me so much But Please Can You tell How To Embed And Reference (2) External Assembly ???
not just one ..
Thanks for this tutorial, it was exactly what I was looking for... :)
Doesn't work for me. I get file missing exception before the assemblyResolve
9 yrs later ... What if the embedded resource dll you want to side load has dependancies on other .dll that are also embedded resources.
🤯
Very cool explanation, you should have kept doing videos.
I did every step and even tried Steve Hall's code below. I still get an error 'Could not load file or assembly' :-(
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.
Thank you! Good Example and nice song :)
Thanks for sharing with us.
Do you know, How we can load dell from another folder rather then embedded?
Thank you, you helped a lot to solve the problem
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.
What is the use of "extern" keyword in C# ?
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,
Hi! Have you found a solution?
@@szymonliszewski9457 You can use Activator to load a class dynamicaly from a managed dll.
trying to do this for System.Data.SQLite.dll but dont work =/
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"..?
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??
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!!!
Got it from here while ago.
stackoverflow.com/questions/6920920/embedding-dlls-into-exe-in-in-visual-c-sharp-2010
@@jinhwang7 you sir, are my savior
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.
Very helpful. It worked great ! I really appreciate it !
Great video! I've been reading about this all day with no luck. How would one set this up with multiple .dll files?
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.
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.
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
does not work, error:
HRESULT: 0x80004003 (E_POINTER))
Same problem :-(
@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"
awesome!. exactly what i needed. well explained ! cheers.
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 .
The link for source code download is broken.
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.
does this also work with a .exe?
Great sharing!
Many thanks!
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!
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
Thanks for this, brilliant.
i need your help
Haha you're welcome. That's why I made this video too, I was searching forever, then came up with a solution!
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 :)
You did a return inside of a using block so that may prevent the stream.Dispose() method from being executed.
The compiler makes sure that the object is disposed before control leaves the block -- regardless of how it leaves the block.
@@BetterCoder Created a test project. You are correct.
Will this work with like a ps3lib dll or like a theme dll?
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
somehow doesn't work fo me. :/
cant i just zip thee thing
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)
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 :)
outstanding vídeo tutorial
congratulations!
thank you, sangat bermanfaat sekali
Thank you :)
Thank you Ahmed!
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.
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);
}
};
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!
+Steve Hall Post has been edited :(
+Magiichan yeah i don't see any thing
kirito curtis i can see it through google+ surprisingly...
Thanx Its do it .)
Good work i like it the song je
EmbededAssembly is your project name.....
Thank You!!!!!!
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!
Haha thanks! I thought it would be funny ;)
+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#
"The module was expected to contain an assembly manifest"
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!!
You deserve your username
I figured it out.
string assemblyName = args.Name.Split(',').First();
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Namespace." + assemblyName + ".dll"))
...
Can you share your full code
@@madamsmith9175 I would if I still had it, haven't worked at that job in over four years though
@@joebrady9829 OK would be nice to post the hole code
Great sharing!
Many thanks!