예제 #1
0
파일: Loader.cs 프로젝트: Cappta/EasyHook
        public static int Load([MarshalAs(UnmanagedType.LPWStr)]String inParam)
        {
            try
            {
                lock (_lock)
                {
                    try
                    {
                        _injectCount++;
                        if (_easyHookDomain == null)
                        {
                            System.Security.PermissionSet ps = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
                            ps.AddPermission(new System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityPermissionFlag.AllFlags));
                            // Evidence of null means use the current appdomain evidence
                            _easyHookDomain = AppDomain.CreateDomain("EasyHook", null, new AppDomainSetup()
                            {
                                ApplicationBase = Path.GetDirectoryName(typeof(Loader).Assembly.Location),
                                // ShadowCopyFiles = "true", // copies assemblies from ApplicationBase into cache, leaving originals unlocked and updatable
                            }, ps);
                        }
                    }
                    catch (OutOfMemoryException ome)
                    {
                        // Creation of AppDomain failed, so fall back to using default domain (means it cannot be unloaded)
                        
                        // The reason is there could be an issue with the target application's stack commit size.
                        // The default stack commit size must be <= 253952 (or 0x3E000) - due to bug in .NET Framework, 
                        // this can be checked with dumpbin.exe and edited with editbin.exe.

                        // Load EasyHook and the target assembly
                        LoadEasyHookProxy lp = new LoadEasyHookProxy();
                        lp.Load(inParam);
                        return 1;
                    }
                }

                int result = -1;

                // Load the EasyHook assembly in the child AppDomain by using the proxy class
                Type proxyType = typeof(LoadEasyHookProxy);
                if (proxyType.Assembly != null)
                {
                    // This is where the currentDomain.AssemblyResolve that we setup within the static 
                    // constructor is required.
                    var proxy = (LoadEasyHookProxy)_easyHookDomain.CreateInstanceFrom(
                                    proxyType.Assembly.Location,
                                    proxyType.FullName).Unwrap();

                    // Loads EasyHook.dll into the AppDomain, which in turn loads the target assembly
                    result = proxy.Load(inParam);
                }

                //result = new LoadEasyHookProxy().Load(inParam);
                return result;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception occurred within Loader.Load: " + e.ToString());
            }
            finally
            {
                lock (_lock)
                {
                    _injectCount--;
                }
            }

            // Failed
            return -1;
        }
예제 #2
0
        public static int Load([MarshalAs(UnmanagedType.LPWStr)] String inParam)
        {
            try
            {
                lock (_lock)
                {
                    try
                    {
                        _injectCount++;
                        if (_easyHookDomain == null)
                        {
                            System.Security.PermissionSet ps = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
                            ps.AddPermission(new System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityPermissionFlag.AllFlags));
                            // Evidence of null means use the current appdomain evidence
                            _easyHookDomain = AppDomain.CreateDomain("EasyHook", null, new AppDomainSetup()
                            {
                                ApplicationBase = Path.GetDirectoryName(typeof(Loader).Assembly.Location),
                                // ShadowCopyFiles = "true", // copies assemblies from ApplicationBase into cache, leaving originals unlocked and updatable
                            }, ps);
                        }
                    }
                    catch (OutOfMemoryException ome)
                    {
                        // Creation of AppDomain failed, so fall back to using default domain (means it cannot be unloaded)

                        // The reason is there could be an issue with the target application's stack commit size.
                        // The default stack commit size must be <= 253952 (or 0x3E000) - due to bug in .NET Framework,
                        // this can be checked with dumpbin.exe and edited with editbin.exe.

                        // Load EasyHook and the target assembly
                        LoadEasyHookProxy lp = new LoadEasyHookProxy();
                        return(lp.Load(inParam));
                    }
                }

                int result = -1;

                // Load the EasyHook assembly in the child AppDomain by using the proxy class
                Type proxyType = typeof(LoadEasyHookProxy);
                if (proxyType.Assembly != null)
                {
                    // This is where the currentDomain.AssemblyResolve that we setup within the static
                    // constructor is required.
                    var proxy = (LoadEasyHookProxy)_easyHookDomain.CreateInstanceFrom(
                        proxyType.Assembly.Location,
                        proxyType.FullName).Unwrap();

                    // Loads EasyHook.dll into the AppDomain, which in turn loads the target assembly
                    result = proxy.Load(inParam);
                }

                //result = new LoadEasyHookProxy().Load(inParam);
                return(result);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception occurred within Loader.Load: " + e.ToString());
            }
            finally
            {
                lock (_lock)
                {
                    _injectCount--;
                }
            }

            // Failed
            return(-1);
        }