views:

679

answers:

3

Hi maestros,

How can I execute a program that is in a MemoryStream so I don't have to save it first to the harddisk. The file may not be saved temperally to the harddisk. The program has to be 100% functional from in memory.

        static   string strTemplate = "MyAPP.SOMEprogram.exe";
        byte[] template;
        int len = 0;

        Assembly assembly = Assembly.GetExecutingAssembly();
        using (Stream stream = assembly.GetManifestResourceStream(strTemplate))
        {

            len = (int)stream.Length;
            BinaryReader reader = new BinaryReader(stream);
            template = reader.ReadBytes(len);


        }

        MemoryStream ms = new MemoryStream(template, true);

Now all 100% working program in MemoryStream (on RAM), can i execute that program? Many Thanks

+1  A: 

I cannot think of an obviouse way to do this. The closest I can think of is using a RAM disc - you will have no hard disc access (besides maybe paging) but this is still very different from what you are asking.

Just to note, I am talking about running arbitrary programs, not loading managed assemblies.

Daniel Brückner
ok, you comment most usefull, how can i make ram disk in c#?
Armen Khachatryan
You will have to install a driver, call the driver API to create a new RAM disc and then you can use it like every ordinary drive - write your program into a file on the RAM disc and execute it. http://www.mydigitallife.info/2007/05/27/free-ramdisk-for-windows-vista-xp-2000-and-2003-server/ lists several RAM disc drivers, some come with source, but how to exactly install a driver and interact with it will probably heavily depend on the driver you choose. You should start reading the documentation and source (if availiable) and then see how to continue.
Daniel Brückner
thanks, but all problem is i do nor want user see frm where program executed, i want copy protection, ram disk here can help?
Armen Khachatryan
Not (easily) because the user can see the RAM disc, too. But it is quite easy for a user to extract the program from a resource file, too.
Daniel Brückner
+2  A: 

You can try Assembly.Load(byte[]), but you may have to work around the code access permissions, and some antivirus may not allow you to do it as well.

Akash Kava
yes you right Assembly.Load(byte[]), but it works only if in that byte[] array contains .net program, else not works, is i am right?
Armen Khachatryan
Yes you are right. Please update your question to mention this if you want to run native applications embedded as resource within a .net app.
SDX2000
+1  A: 

The key here is to build/load an assembly in memory from the byte array see. Once you have a reference to the assembly you can easily get access to its types and instantiate them. Once you have got type instances call a method on it to "run" your program.

SDX2000
Assembly..::.Load Method (array<Byte>[])is this works for programs that writed in c++ and not in c# ??thanks
Armen Khachatryan
Nope works in C# too.
SDX2000
for c++ works???
Armen Khachatryan