|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Reflection; |
| 5 | +using System.Security; |
| 6 | +using System.Security.Permissions; |
| 7 | +using System.Security.Policy; |
| 8 | + |
| 9 | +namespace mysql_managed_interface |
| 10 | +{ |
| 11 | + class MySQLHostManager: AppDomainManager, IManagedHost |
| 12 | + { |
| 13 | + |
| 14 | + public static StrongName CreateStrongName(Assembly assembly) |
| 15 | + { |
| 16 | + if (assembly == null) |
| 17 | + throw new ArgumentNullException("assembly"); |
| 18 | + |
| 19 | + AssemblyName assemblyName = assembly.GetName(); |
| 20 | + Debug.Assert(assemblyName != null, "Could not get assembly name"); |
| 21 | + |
| 22 | + // get the public key blob |
| 23 | + byte[] publicKey = assemblyName.GetPublicKey(); |
| 24 | + if (publicKey == null || publicKey.Length == 0) |
| 25 | + throw new InvalidOperationException("Assembly is not strongly named"); |
| 26 | + |
| 27 | + StrongNamePublicKeyBlob keyBlob = new StrongNamePublicKeyBlob(publicKey); |
| 28 | + |
| 29 | + // and create the StrongName |
| 30 | + return new StrongName(keyBlob, assemblyName.Name, assemblyName.Version); |
| 31 | + } |
| 32 | + |
| 33 | + private IUnmanagedHost unmanagedInterface = null; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// A new AppDomain has been created |
| 37 | + /// </summary> |
| 38 | + public override void InitializeNewDomain(AppDomainSetup appDomainInfo) |
| 39 | + { |
| 40 | + // let the unmanaged host know about us |
| 41 | + InitializationFlags = AppDomainManagerInitializationOptions.RegisterWithHost; |
| 42 | + |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + public int CreateAppDomain(string name) |
| 47 | + { |
| 48 | + PermissionSet permissions = new PermissionSet(PermissionState.None); |
| 49 | + permissions.AddPermission(new SecurityPermission(PermissionState.Unrestricted)); |
| 50 | + permissions.AddPermission(new UIPermission(PermissionState.Unrestricted)); |
| 51 | + |
| 52 | + return AppDomain.CreateDomain( |
| 53 | + name, |
| 54 | + AppDomain.CurrentDomain.Evidence, |
| 55 | + AppDomain.CurrentDomain.SetupInformation, |
| 56 | + permissions, |
| 57 | + CreateStrongName(Assembly.GetExecutingAssembly())).Id; |
| 58 | + } |
| 59 | + |
| 60 | + public void Dispose() |
| 61 | + { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + public void SetUnmanagedHost(IUnmanagedHost unmanagedHost) |
| 66 | + { |
| 67 | + Debug.Assert(unmanagedHost != null, "Attempt to set null unmanaged host"); |
| 68 | + Debug.Assert(this.unmanagedInterface == null, "Attempt to reset unmanaged host"); |
| 69 | + |
| 70 | + this.unmanagedInterface = unmanagedHost; |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + public void Write(string message) |
| 75 | + { |
| 76 | + Trace.WriteLine(message); |
| 77 | + Console.WriteLine(message); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + public Int64 Run(Int64 path) |
| 82 | + { |
| 83 | + return (path * 3); |
| 84 | + //new FileIOPermission(PermissionState.Unrestricted).Assert(); |
| 85 | + //string fullPath = Path.Combine( |
| 86 | + // Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), |
| 87 | + // path); |
| 88 | + //CodeAccessPermission.RevertAssert(); |
| 89 | + |
| 90 | + //new FileIOPermission( |
| 91 | + // FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, |
| 92 | + // fullPath).Assert(); |
| 93 | + //AppDomain.CurrentDomain.ExecuteAssembly(fullPath); |
| 94 | + //CodeAccessPermission.RevertAssert(); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments