在外部执行代码中执行外部代码(带域)

时间:2017-08-01 14:23:22

标签: c#

我正在使用此代码:

public class Launcher : MarshalByRefObject
{
    public static void Start(byte[] Exe, string Checksum)
    {
        string[] args = { IP, id, Checksum };
        Console.WriteLine("::Invokeing with: " + IP + ", " + id + ", " + Checksum);
        var ps = new PermissionSet(PermissionState.Unrestricted);
        var setup = new AppDomainSetup();
        setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
        var ev = new Evidence();
        var appDomain = AppDomain.CreateDomain("Sandbox",
            ev,
            setup,
            ps);
        var program = (Launcher)appDomain.CreateInstanceAndUnwrap(
            typeof(Launcher).Assembly.FullName,
            typeof(Launcher).FullName);
        program.Execute(Exe, args);
        AppDomain.Unload(appDomain);
    }

    public void Execute(byte[] bytes, object[] args)
    {
        var assembly = Assembly.Load(bytes);
        var main = assembly.EntryPoint;
        main.Invoke(null, new object[] { args });
    }
}
事实是,我执行此代码的代码也在使用此代码但是我得到了这个错误:

  

System.InvalidCastException:Kan transparentproxy niet converteren   naar型发射器。 bij Embassy.Program.Launcher.Start(Byte [] Exe,   String Checksum)

任何人都可以帮助我 附:如果你不了解芒果,就没有任何不合理的贬低。

  

首先执行作品,第二次失败

1 个答案:

答案 0 :(得分:0)

显然,如果你重写函数并放入一个像这样的外部dll:

public class Launcher : MarshalByRefObject
{
    public void Start(byte[] Exe, string[] args)
    {
        PermissionSet ps = new PermissionSet(PermissionState.Unrestricted);
        AppDomainSetup setup = new AppDomainSetup();
        setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
        Evidence ev = new Evidence();
        AppDomain appDomain = AppDomain.CreateDomain("Sandbox",
            ev,
            setup,
            ps);
        Launcher program = (Launcher)appDomain.CreateInstanceAndUnwrap(
            typeof(Launcher).Assembly.FullName,
            typeof(Launcher).FullName);
        program.Execute(Exe, new object[] { args });
        AppDomain.Unload(appDomain);
    }
    public void Execute(byte[] bytes, object[] args)
    {
        Assembly assembly = Assembly.Load(bytes);
        MethodInfo main = assembly.EntryPoint;
        main.Invoke(null, args);
    }
}

并使用新的Launcher()调用它。开始(' Source',new [] {" args"});你会得到理想的结果。