Embedding an External Executable Inside a C# Program

Embedding an external executable inside a C# program

Here is some sample code that would roughly accomplish this, minus error checking of any sort. Also, please make sure that the license of the program to be embedded allows this sort of use.

// extracts [resource] into the the file specified by [path]
void ExtractResource( string resource, string path )
{
Stream stream = GetType().Assembly.GetManifestResourceStream( resource );
byte[] bytes = new byte[(int)stream.Length];
stream.Read( bytes, 0, bytes.Length );
File.WriteAllBytes( path, bytes );
}

string exePath = "c:\temp\embedded.exe";
ExtractResource( "myProj.embedded.exe", exePath );
// run the exe...
File.Delete( exePath );

The only tricky part is getting the right value for the first argument to ExtractResource. It should have the form "namespace.name", where namespace is the default namespace for your project (find this under Project | Properties | Application | Default namespace). The second part is the name of the file, which you'll need to include in your project (make sure to set the build option to "Embedded Resource"). If you put the file under a directory, e.g. Resources, then that name becomes part of the resource name (e.g. "myProj.Resources.Embedded.exe"). If you're having trouble, try opening your compiled binary in Reflector and look in the Resources folder. The names listed here are the names that you would pass to GetManifestResourceStream.

Embedding an external executable inside a C# program and run it without creating new file

Despite Hans' comment I think it is pretty easy* (assuming code have enough privilegies to do that). Essentially you want to fake local drive (or network drive if it works for your evil code).

Network drive can be easiy created by implementing DAV in your own program and that pointing network path to that machine using WebDAV client.

Local drive should be possibible too (also I'm not sure if any .Net implementations are avaialble) by for example implementing Minifilter Driver and setting it up, but I strongly suspect that there is no way to have it run from the only executable...

*Easy as in "does not require significant unsupported hacks", time estimates and necessary level of knowledge is not considered.

Embed a file into an external exe with C#

So after some intense googling I found a library called Mono.Cecil which does exactly what I want. I was able to inject a file into an executable file by using the following code:

string fileName = "Interpreter.exe"; // The file which will have toInject injected in it
string outputName = "Compiled.exe"; // The output file to compile
string toInject = "program.txt"; // The file we will be injecting into fileName
string resourceName = "program.txt"; // The name of the file once it's inside fileName

var module = ModuleDefinition.ReadModule(filename);

var file = new EmbeddedResource(
resourceName,
ManifestResourceAttributes.Private,
File.ReadAllBytes(toInject));

module.Resources.Add(file);

module.Write(outputName);

How to add External .exe to my C# project?

You can add file by right-clicking on the project, or drag-n-drop works.

You can add your exe in your solution and set its Build Action: Content and Copy to output Directory: Copy always.

The installer should automatically include the file.

Hope this helps

How to start an exe from embedded Resources C#

Possibly you have to read its bytes/in binary form from the resource, then save it to the disk - you have to specifiy the path - and call with the Process library.

See: https://social.msdn.microsoft.com/Forums/vstudio/en-US/9ee5a7e0-91b1-4d42-aa97-d8f3d528354d/run-exe-file-as-an-embedded-resource-in-c-on-selected-folder?forum=wpf

byte[] exeBytes = Properties.Resources.OPKMR;
string exeToRun = @"C:\TEST\MyTestExe.exe";

Etc.

Run Exe file as an Embedded Resource in C#

Embedding an external executable inside a C# program and run it without creating new file

Embedding external DLLs in a EXE

If all DLLs are .Net assemblies you could check out ILMerge or add the assemblies as ressource and attach to the AssemblyResolve event like mentioned here.



Related Topics



Leave a reply



Submit