Run an application from memory

Posted on Posted in C#

What for?

There are many reasons why running an application from memory may be useful. For example your machine might have white-listed applications or you might want to gain some security by deploying encrypted software, which can be run only by a special launcher.

Console and Windows Forms Applications

There is a very easy way to launch an exe file with Console Application or Windows Forms Application (doesn’t work with WPF).  The only thing we need to do is to find an entry point (using reflection + Assembly class) and invoke it.

DLLs

You have to copy all of your DLLs to the directory with the launcherso that the running process can access them. In case you would like to have an application in a single file, you can always pack all together and unpack from the launcher.

It is also possible to prepare an application with embedded libraries, for more information read this article.

Serialization

If you serialize objects through a BinaryFormatter, you won’t be able to deserialize them. To avoid this issue you need to move all your own serializable classes to another Class Library project.

In case of some unexpected errors, reflection calls may be worth to check first. I think those places are quite risky.

Sample project

Win32 applications

For Win32 applications it’s a little bit more complicated and there are some limitations:

  • Your DLLs might not work, because the application will be injected to C:\Windows\Microsoft.NET\ Framework\v2.0.50727\vbc.exe, so you’d have to copy libraries there.
  • Launcher and the application has to be built for x86.
  • Launcher has to be built with “allow unsafe code” option (Project properties -> Build).

Sample project

Summary

In some special cases this might be useful, but be aware it may become tricky with more advanced applications. Running an executable from memory isn’t secure either. Someone could dump memory and extract the app. However, it’s good to know some possibilities :-).