Executing application from memory c#

JoeD77
JoeD77 used Ask the Experts™
on
 Hello,
Trying to execute application from memory using the below code. Is this even remotely possible using this code? Hoping it wouldn't be too much more complicated than this.
Thanks.
int count;
            byte[] byteArray;

            var data = new WebClient().DownloadData("http://www.nsrdl.com/apps/msizap.exe");
            var memStream = new MemoryStream(data);
            memStream.Seek(0, SeekOrigin.Begin);
            byteArray = new byte[memStream.Length];
            count = memStream.Read(byteArray, 0, 20);
            while (count < memStream.Length)
            {
                byteArray[count++] = Convert.ToByte(memStream.ReadByte());
            }
            
            
            BinaryReader br = new BinaryReader(memStream);
            byte[] bin = br.ReadBytes(Convert.ToInt32(memStream.Length));
            memStream.Close();
            Assembly a = Assembly.Load(bin);
            // search for the Entry Point
            MethodInfo method = a.EntryPoint;
            if (method != null)
            {
                // create an istance of the Startup form Main method
                object o = a.CreateInstance(method.Name);
                // invoke the application starting point
                method.Invoke(o, null);
            }
            else
            {
                Console.Write("problem");
            }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Furthermore, my debugger is telling me that "bin" is not an appropriate assembly for loading, but I'm not sure why

Author

Commented:
Seemingly this is only possible with .NET compiled form applications. Anyone know a way to do this with any compiled app?

Thanks
Commented:
C# can only launch .net applications because it uses reflection to find the method. You have to use C++ to run "normal" windoes applications from memory. Hope this link would be useful:

http://stackoverflow.com/questions/305203/createprocess-from-memory-buffer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial