Embed one executable in another

Embed one executable in another

[Originally posted on July 18th 2010 on my previous site]

For a while now I’ve been using a C++ command line utility to help me measure user logon times on terminal servers. The utility spits out information command line, reporting user logon times and system information – really useful application.

The problem is that the information it outputs is not neatly formatted, and is different on each OS its executed on :-/

I decided it was about time I modified this program to output its information more elegantly, better suiting my needs. So I started looking round for its source code, hoping I could just make a few modifications and recompile the application. I couldn’t find the source or the person who wrote the application anywhere!

So I was faced with the following choices:

  1. Re-write the application from scratch.
  2. Carry on as before just putting up with the shortcomings.

Then it occurred to me – why not embed the C++ utility in bespoke application that could parse its output…

After some googling I found an article ( I can’t remember where) that explained a way to embed an executable into another using visual studio, re-building the embedded executable at run time. I know this sounds like an easy way to get up to mischief, hiding dodgy executable’s- but for me this solution was perfect.

Steps to embed the executable and unpack it:

1. Copy the executable you want to embed into the root folder of your visual studio solution.

Step One.

2. Select the executable you want to embed (above this would be notepad.exe) to view its properties. Change the “Build Action” property so that it says “Embedded Resource”

Step Two.

3. Use the following code to unpack the executable.

private static void RecreateAllExecutableResources()
{
 // Get Current Assembly reference
 Assembly currentAssembly = 
 Assembly.GetExecutingAssembly();
 
 // Get all imbedded resources
 string[]arrResources = 
 currentAssembly.GetManifestResourceNames();
 
 foreach (string resourceName in arrResources)
 {
   if (resourceName.EndsWith(".exe"))
   {
     //Name of the file saved on disk
     string saveAsName = resourceName;
 
     //here we ensure that when the
     //executable is written to disk
     //it does not contain the name
     //of this solution as a prefix.
     saveAsName = saveAsName.Replace("ExectubaleAsResource_Sample.", "");
 
     FileInfo fileInfoOutputFile = new FileInfo(saveAsName);
 
     //If the destination executable doesn't already exist
     if (!fileInfoOutputFile.Exists)
     {
       //OPEN NEWLY CREATING
       //FILE FOR WRITTING
       FileStream streamToOutputFile =
       fileInfoOutputFile.OpenWrite();
 
       //GET THE STREAM TO THE RESOURCES
       Stream streamToResourceFile =
       currentAssembly.GetManifestResourceStream(resourceName);
 
       //SAVE TO DISK
       const int size = 4096;
       byte[] bytes = new byte[4096];
       int numBytes;
       while ((numBytes = streamToResourceFile.Read(bytes, 0, size)) > 0)
       {
         streamToOutputFile.Write(bytes, 0, numBytes);
       }
 
       streamToOutputFile.Close();
       streamToResourceFile.Close();
      }
     }
 
   }
}

This code will output the embedded executable along side the executable of the solution at run time. So to be clear it will appear in the “bin\Debug” directory when run in visual studio.

Step Three.

 

That’s it. Fairly easy, and a lot simpler than I thought it would be.

I’ve put together a sample solution to demonstrate this procedure:

ExectubaleAsResource_Sample

The solution is compiled for .NET 3.0 and has been built using Visual Studio 2008 .

The sample simply rebuilds notepad.exe then executes it. Be sure to virus scan the Zip just encase someone modified my source and finds a way to re-upload it!