While working on a side project (Serial Key Maker) I explored the ability to embed a DLL within my executable. I didn’t want to deploy multiple DLL’s – I was trying to keep the number of dependencies down, but still centralize the common routines in my logging object. I found how to embed the logging DLL within my application’s components, and how to call its methods from the application. I wrote about the process on my application’s blog.
I figured out that there may be a case for doing the same with an executable. I have not had a reason to do it at Passport, but I am writing about it anyway…and looking for an opportunity to use it
To recap, the point of this exercise is to embed an executable within our application, and then make a call to it while our application is running. To set this up, we need to pull the EXE into our application and then mark it to be placed in the resource list. Once there we can reference it using reflection, and then use it. In the case of the DLL, we created an instance of it, and then called its methods. For an EXE it is a little simpler. We just get the EXE from the application’s resource store, stream it to a file, and the start it using “Process.Start”.
This article uses the following demo application as reference for explaining how to do this: [Download the DEMO here]
To add the EXE to the resource store of your application, do the following:
Right-click on your application in the solution explorer, and select “add existing”.
Note: Do NOT use the Add reference option.
Navigate to, and select, the EXE you want to embed.
Now, Select the EXE, and open the properties window. The first property is “Build Action”. Change the option to be “Embed Resource”.

When you compile the project, the exe will be drawn in to your executable and will be available as a resource.
Now, to use the embedded EXE, you need to:
- pull the referenced EXE into a stream from the resource list

- read the stream into a buffer

- write it to a file

- start it as you would for any executable.
You can even pass in command line parameters just as you would for any other EXE

To make this easier, I created a helper class (EmbeddedAssemblyHelper.vb) to take care of reading the resources and working with them. There is a shared function “LoadExecutable” which reads the EXE from the resource stream, saves it to a unique file and then returns the file name. The calling application can then start the executable by calling Process.Start.
For simplicity, I have the method accepting the EXE’s name. It might be desirable to have the “LoadExecutable” method discover the EXE’s name.
The downside to this is that is it much slower than simply executing the EXE. So, there needs to be a very good reason to embed your EXE within another (besides the coolness factor, that is
) I can see a use for it in simplifying distribution. It would also make dealing with obfuscation much easier.
I love to hear of any real world uses for this technique. Email me at grant.porteous@passporthealth.com
Thanks for reading,
Grant