Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Run A Downloaded File From Memory? [duplicate]

Possible Duplicate:
Load an EXE file and run it from memory using C#

I am using the WebClient class to download a .exe from a web server. Is there a way that I can run the .exe without saving it to disk first?

For the purpose of completeness let me show you what I have so far.

Here is the code I use to start the download:

WebClient webClient = new WebClient();
webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(webClient_DownloadDataCompleted);
webClient.DownloadDataAsync(new Uri("http://www.somewebsite.com/calc.exe"));

And in the (webClient_DownloadDataCompleted) method I simply grab the bytes from the parameter e:

private void webClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
    Byte[] downloadedData = e.Result;
    // how to run this like a .exe?
}

Thank you.

like image 770
Jan Tacci Avatar asked Jan 07 '13 19:01

Jan Tacci


2 Answers

If your .exe is a .NET program, you can load an assembly and run its entry point.

Otherwise, while there are ways to do it, I can't see the problem with saving a file in temporary directory and running it from there which is so much less painful.

like image 185
Dan Abramov Avatar answered Nov 05 '22 22:11

Dan Abramov


Have a look at this thread. I think you can solve it with VirtualAlloc

Is it possible to execute an x86 assembly sequence from within C#?

If your byte array contains a .Net assembly you should be able to do this:

Assembly assembly = AppDomain.Load(byteArray)
Type typeToExecute = assembly.GetType("ClassName");
Object instance = Activator.CreateInstance(typeToExecute);
like image 1
Moriya Avatar answered Nov 05 '22 23:11

Moriya