mercredi 11 février 2015

C# AppDomain: Execute assembly from entry point

I am trying to create a Windows sandbox application, building upon the "How to" found here: "http://ift.tt/1DhB6ji"


In the example it loads a specific type from a DLL whereas I would like to be able to execute an assembly from its entry point with restricted permissions.


The program I am using for testing purposes is a simple hello world application.



using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.Read();
}
}
}


I have tried two different methods to try and achieve this.


Method 1.


Use the "MethodInfo.Invoke" method on the entry point of the assembly.



MethodInfo target = Assembly.Load(assemblyName).EntryPoint;
target.Invoke(null, parameters);


This produces a Method Access Exception due to the main method being non-public. A simple way around this would be to make the main method public but I will not have this sort of access to the assemblies that are intended to be used with this application.


Method 2.


Use the "AppDomain.ExecuteAssembly" method as shown below.



newDomain.ExecuteAssembly(filePath, parameters);


This requires that the app domain has both File IO Permission and UI Permission for the assembly to be executed but I want to be able to restrict the assembly from having these permissions.


Is there a way to execute an assembly from it's entry point within a permission restricted app domain?






Aucun commentaire:

Enregistrer un commentaire