Beispiel #1
0
        public static HookHandle On(MethodBase target, MethodBase substitute)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            if (substitute == null)
            {
                throw new ArgumentNullException("subst");
            }

            CheckSignatures(target, substitute);

            if (HookedMethods.ContainsKey(target))
            {
                throw new InvalidOperationException();
            }

            HookedMethods.Add(target, null);

            var targetMethod = new Method(target);
            var destMethod   = new Method(substitute);

            var reloc       = Emitter.EmitMethod(target);
            var relocMethod = new Method(reloc);

            var targetAddr = targetMethod.GetCompiledCodeAddress();
            var destAddr   = destMethod.GetCompiledCodeAddress();
            var relocAddr  = relocMethod.GetCompiledCodeAddress();

            var inject = CodeInject.Create(targetAddr, destAddr, relocAddr);

            inject.Inject();

            return(new HookHandle(target, reloc, inject));
        }
 internal HookHandle(MethodBase target, MethodBase reloc, CodeInject inject)
 {
     _target = target;
     _inject = inject;
     _reloc  = reloc;
 }