Esempio n. 1
0
        public static MethodReplacer Win32FuncWithExportFunc <T, TDelegate>(string win32Dll, string win32FuncName, T funcToReplace, out TDelegate saveOrgFunc) where T : Delegate
            where TDelegate : Delegate
        {
            saveOrgFunc = null;

            IntPtr orgFuncAddr = GetExportFunctionAddressFromEAT(win32Dll, win32FuncName, out IntPtr addressOrgFuncAddr);

            if (orgFuncAddr == IntPtr.Zero || addressOrgFuncAddr == IntPtr.Zero)
            {
                return(null);
            }

            string moduleName = Path.GetFileName(funcToReplace.GetType().Assembly.Location);
            string funcName   = funcToReplace.Method.Name;

            IntPtr proxyFuncAddr = GetExportFunctionAddressFromEAT(moduleName, funcName, out IntPtr addressToProxyFuncAddr);

            if (proxyFuncAddr == IntPtr.Zero || addressToProxyFuncAddr == IntPtr.Zero)
            {
                return(null);
            }

            MethodReplacer mr = new MethodReplacer();

            if (mr.Win32FuncWith <T, TDelegate>(addressOrgFuncAddr, orgFuncAddr, proxyFuncAddr, funcToReplace.Method.ReturnType, out saveOrgFunc) == false)
            {
                return(null);
            }

            return(mr);
        }
Esempio n. 2
0
        public static MethodReplacer Win32FuncWithManagedFunc <T, TDelegate>(string win32Dll, string win32FuncName, T funcToReplace, out TDelegate saveOrgFunc) where T : Delegate
            where TDelegate : Delegate
        {
            saveOrgFunc = null;

            IntPtr orgFuncAddr = GetExportFunctionAddressFromEAT(win32Dll, win32FuncName, out IntPtr addressOrgFuncAddr);

            if (orgFuncAddr == IntPtr.Zero || addressOrgFuncAddr == IntPtr.Zero)
            {
                return(null);
            }

            IntPtr proxyFuncAddr;

            if (typeof(T).IsGenericType == true)
            {
                proxyFuncAddr = funcToReplace.Method.MethodHandle.GetFunctionPointer();
            }
            else
            {
                proxyFuncAddr = Marshal.GetFunctionPointerForDelegate(funcToReplace);
            }

            if (proxyFuncAddr == IntPtr.Zero)
            {
                return(null);
            }

            MethodReplacer mr = new MethodReplacer();

            if (mr.Win32FuncWith <T, TDelegate>(addressOrgFuncAddr, orgFuncAddr, proxyFuncAddr, funcToReplace.Method.ReturnType, out saveOrgFunc) == false)
            {
                return(null);
            }

            return(mr);
        }