ISSUE :
 
Profile photo
Submitted by RaKumar
on August 9th, 2011 at 10:24:33 EST

How to execute embedded application as resource using C#

I have a .NET tool which is compiled as .EXE. Now I need to include this .NET executable application inside another .NET application as embedded resource and execute from memory on the fly. Can I embed executable .exe application inside a .NET application and execute from memory on the fly? I do not want to save the embedded exe into file before execution.
Thanks in advance.


Profile photo
Replied by SBajra
on August 9th, 2011 at 10:42:01 EST
You can do this in C# .NET using reflection. However, the embedded executable application should be .NET application. you can not embedded non .NET application with this technique
Here the code sample:
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ParameterizedThreadStart(ExecuteResourceExe));
t.Start("EmbeddedResource.exe"); //Pass the resource name for example defaultNamespace.foldername.embeddedResource.exe
}

public static void ExecuteResourceExe(object resourceName)
{
//Get the resource stream
using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName.ToString()))
{
//If resourceStream is not null
if (resourceStream != null)
{
//Read from resourceStream to byte[]
byte[] resourceBuffer = new byte[resourceStream.Length];
resourceStream.Read(resourceBuffer, 0, resourceBuffer.Length);

//Load byte[] and execute the application from EntryPoint
Assembly.Load(resourceBuffer).EntryPoint.Invoke(null, null);
}
}
}
Accepted Answer
Accepted
 Answer

Profile photo
Replied by RaKumar
on August 9th, 2011 at 10:51:24 EST
works perfect. Thanks.

Profile photo
Replied by Steve Hoow
on June 26th, 2012 at 17:06:50 EST
Is it possible to capture STDOUT when executing an embedded executable? I am using the above cod but the executable outputs a text string.

Profile photo
Replied by Steve Hoow
on June 26th, 2012 at 17:11:40 EST
Is it possible to capture STDOUT when executing an embedded executable? I am using the above cod but the executable outputs a text string.

Profile photo
Replied by SBajra
on June 27th, 2012 at 09:35:59 EST
I am not sure what you are trying to achieve but the code example that I provided in this thread is to execute embedded .NET executable using reflection. If you want your embedded application to write to STDOUT or do some behavior then it should be implemented embedded application.

Profile photo
Replied by Steve Hoow
on June 27th, 2012 at 12:58:45 EST
I am given someones executable that I need to use for security (call it security.exe). It is sensitive and I am not allowed to put it on the disk of the customer machine but I can embed it in my application. Security.exe is .NET and takes an input parameter and outputs a string. I need to capture the output string in my application. I prefer it execute from memory and not first write to disk and then execute it.

Profile photo
Replied by SBajra
on June 27th, 2012 at 16:22:28 EST
I don't think you can achieve as you described. Here we are simply using reflection to get the entry point of the embedded application and executing it.
At most you will be able to execute the public static method out the embedded resource.

To execute public static method from embedded resource, you can do something like this:
Assembly.Load(resourceBuffer).GetType("Namespace.ClassName").GetMethod("MethodName").Invoke(null, null);

Profile photo
Replied by Steve Hoow
on June 27th, 2012 at 16:38:06 EST
I was afraid it might not be practical. I will try the public static method.
Thanks for your response.