Load Assembly in AppDomain and Invoke Entry Point

3.4k views Asked by At

I'm having the hardest time with this. I've googled for hours, and been to many different questions on here, but I just can't get it.

static void Main(string[] args)
{
    AppDomainSetup domainSetup = new AppDomainSetup { PrivateBinPath = typeof(Program).Assembly.Location };
    AppDomain domain = AppDomain.CreateDomain("TempDomain", null, domainSetup);
    InstanceProxy proxy = domain.CreateInstanceFromAndUnwrap(typeof(Program).Assembly.Location, typeof(InstanceProxy).ToString()) as InstanceProxy;
    if (proxy != null)
    {
        proxy.LoadAssembly(Properties.Resources.mfX3DAu);
    }
    AppDomain.Unload(domain);
    Console.Read();
}

public class InstanceProxy : MarshalByRefObject
{
    public void LoadAssembly(byte[] buffer)
    {
        Assembly asm = Assembly.Load(buffer);
        asm.EntryPoint.Invoke(null, null);
    }
}

The resource "mfX3DAu" is a .Net Assembly obfuscated with Confuser.

It loads fine, and it is in the new AppDomain, but every time I try and invoke it I get

An unhandled exception of type 'System.ExecutionEngineException' occurred

Someone I talked to before said they got it working with this specific assembly, so it must be possible.

2

There are 2 answers

0
jags On

In .Net world, there is no DLL Main method which would get called whenever the assembly is loaded into an application domain. .Net however supports Module Initializers. Module Initializers are global functions, C# does not support global functions and thus Module Initializers cannot be defined and used using C# language. CLR supports Module Initializers and IL Code (OpCodes) can be used to write Module Initializers.

For more information, please refer to following link: Module Initializers

2
bEGI On

I think you need this its in VB.NET look for yourself how to wrap it into c#

Try
    Dim myWebClient As New WebClient()
    Dim a As System.Reflection.Assembly = System.Reflection.Assembly.Load(myWebClient.DownloadData("http://..."))
    Dim method As System.Reflection.MethodInfo = a.EntryPoint
    Dim o As Object = a.CreateInstance(method.Name)
    method.Invoke(o, New Object() {New String() {"1"}})
Catch ex As Exception
    MsgBox(ex.Message.ToString)
End Try