示例#1
0
        public SharpDomain(string assemblyName, string args)
        {
            try
            {
                var appBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                var ads     = new AppDomainSetup
                {
                    ApplicationBase = appBase,
                    PrivateBinPath  = appBase
                };
                DomainManager.CurrentDomain = AppDomain.CreateDomain("SharpDomain_Internal_" + _rand.Next(0, 100000),
                                                                     AppDomain.CurrentDomain.Evidence, ads);

                AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

                DomainManager.CurrentAssemblyLoader =
                    (WSharpAssemblyLoader)DomainManager.CurrentDomain.CreateInstanceAndUnwrap(
                        Assembly.GetExecutingAssembly().FullName,
                        typeof(WSharpAssemblyLoader).FullName);

                var fileToLoadAndRun = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                                                    assemblyName);
                DomainManager.CurrentAssemblyLoader.LoadAndRun(fileToLoadAndRun, args);
            }
            catch (Exception e)
            {
                SharpLoader.ShowError(e);
                // MessageBox.Show(e.ToString());
            }
            finally
            {
                DomainManager.CurrentAssemblyLoader = null;
                AppDomain.Unload(DomainManager.CurrentDomain);
            }
        }
示例#2
0
        public void LoadAndRun(string file, string args)
        {
            var asm = LoadRemoteAssembly(file);

            var STAMethods = asm.GetTypes()
                             .SelectMany(t => t.GetMethods())
                             .Where(m => m.GetCustomAttributes(typeof(STAThreadAttribute), false).Length > 0)
                             .ToArray();

            if (STAMethods.Length != 1)
            {
                if (!STAMethods.Any())
                {
                    SharpLoader.ShowError(new Exception("Unable to find entry function with [STAThread] Attribute"));
                    //MessageBox.Show("Unable to find entry function with [STAThread] Attribute");
                    return;
                }

                SharpLoader.ShowError(new Exception(
                                          "More than one function with an [STAThread] Attribute was found; injected dlls should only have one"));
                // MessageBox.Show("More than one function with an [STAThread] Attribute was found; injected dlls should only have one");
                return;
            }

            var entry2 = STAMethods[0];

            if (entry2 == null)
            {
                SharpLoader.ShowError(new Exception($"[STAThread] Attribute for {file} not found"));
            }
            // MessageBox.Show("Entry of " + file + " not found.");

            bool hasEntryParameters = entry2.GetParameters().Length > 0;

            if (string.IsNullOrEmpty(args))
            {
                entry2.Invoke(null, hasEntryParameters ? new[] { string.Empty } : null);
            }
            else
            {
                if (!hasEntryParameters)
                {
                    entry2.Invoke(null, new[] { args });
                }
                else
                {
                    throw new Exception("Can't pass parameters to a parameterless method!");
                }
            }
        }
示例#3
0
        public void LoadAndRun(string file, string args)
        {
            Assembly asm = Assembly.Load(file);

            var STAMethods = asm.GetTypes()
                             .SelectMany(t => t.GetMethods())
                             .Where(m => m.GetCustomAttributes(typeof(STAThreadAttribute), false).Length > 0)
                             .ToArray();


            if (STAMethods.Count() != 1)
            {
                if (!STAMethods.Any())
                {
                    SharpLoader.ShowError(new Exception("Unable to find entry function with [STAThread] Attribute"));
                    //MessageBox.Show("Unable to find entry function with [STAThread] Attribute");
                    return;
                }
                SharpLoader.ShowError(new Exception("More than one function with an [STAThread] Attribute was found; injected dlls should only have one"));
                // MessageBox.Show("More than one function with an [STAThread] Attribute was found; injected dlls should only have one");
                return;
            }

            var entry2 = STAMethods[0];

            if (entry2 == null)
            {
                SharpLoader.ShowError(new Exception(string.Format("[STAThread] Attribute for {0} not found", file)));
                // MessageBox.Show("Entry of " + file + " not found.");
            }

            if (string.IsNullOrEmpty(args))
            {
                entry2.Invoke(null, null);
            }
            else
            {
                entry2.Invoke(null, new[] { args });
            }
        }