0

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.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
Banksy
  • 51
  • 2
  • 6
  • @devundef - No internal source (never really used call stacks before so not sure what that means/if it's relevant). External source: mscorlib.dll!System.Threading.ThreadHelper.ThreadStart(object obj) merged!ﱈ㑸购ᷚ븋䕒.㶯맖ꅰ㟏幔款彶(object thread) mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) :::: mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionCon‌​text executionContext, System.Threading.ContextCallback callback, object state) :::: [Appdomain Transition] – Banksy Aug 28 '12 at 22:57
  • @Banski, stack trace often are relevant because they provide information about which method threw the exception. – Marcelo De Zen Aug 28 '12 at 23:04
  • @devundef, I normally find out and fix my problems through other means (or at least try), but I'll keep that in mind. About the problem, any ideas? – Banksy Aug 28 '12 at 23:25
  • Have you tried doing something else with the assembly? e.g. Create an instance of some Type from mfX3DAu assembly and call some method on the type. This will confirm if Entry Point is having the issue or not. – jags Oct 14 '12 at 13:43

2 Answers2

2

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
bEGI
  • 21
  • 2
  • 1
    Hello bEGI and welcome to StackOverflow. Although it seems to be a correct answer, it would be more valuable if you provided your answer on a language the asker needed (C#). Also, a brief description is helpful so that it gave more value to those who will read your answer in the future, and more upvotes to you. – Be Brave Be Like Ukraine Oct 14 '12 at 13:30
  • Instead of "1" you can enter any needed command line arguments like: "first", "second", "3" – Zibri Apr 12 '14 at 11:32
0

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

jags
  • 2,022
  • 26
  • 34