/// <summary>
        /// Creates the <see cref="Wrapper"/> which allows you to call a function with a custom calling
        /// convention as if it were a Microsoft X64 calling convention function.
        /// </summary>
        /// <param name="functionAddress">Address of the function to wrap.</param>
        public static TFunction Create <TFunction>(long functionAddress)
        {
            var    attribute = FunctionAttribute.GetAttribute <TFunction>();
            IntPtr wrapperFunctionPointer = (IntPtr)functionAddress;

            // Hot path: Microsoft X64 functions require no wrapping.
            if (!attribute.Equals(new FunctionAttribute(CallingConventions.Microsoft)))
            {
                wrapperFunctionPointer = Create <TFunction>((IntPtr)functionAddress, attribute, new FunctionAttribute(CallingConventions.Microsoft));
            }

            return(Marshal.GetDelegateForFunctionPointer <TFunction>(wrapperFunctionPointer));
        }
        private static void Create(ReverseWrapper <TFunction> reverseFunctionWrapper, IntPtr functionPtr)
        {
            Mutex.MakeReverseWrapperMutex.WaitOne();
            var reloadedFunctionAttribute = FunctionAttribute.GetAttribute <TFunction>();

            // Microsoft X64 is hot path, as our TFunction will already be Microsoft X64, we marshal if it's anything else.
            if (!reloadedFunctionAttribute.Equals(new FunctionAttribute(CallingConventions.Microsoft)))
            {
                reverseFunctionWrapper.WrapperPointer = Wrapper.Create <TFunction>(functionPtr, new FunctionAttribute(CallingConventions.Microsoft), reloadedFunctionAttribute);
            }

            Mutex.MakeReverseWrapperMutex.ReleaseMutex();
        }
Beispiel #3
0
        private static void Create(ReverseWrapper <TFunction> reverseFunctionWrapper, nuint functionPtr)
        {
            var reloadedFunctionAttribute = FunctionAttribute.GetAttribute <TFunction>();

            // Microsoft X64 is hot path, as our TFunction will already be Microsoft X64, we marshal if it's anything else.
            if (!reloadedFunctionAttribute.Equals(FunctionAttribute.Microsoft))
            {
                reverseFunctionWrapper.WrapperPointer = Wrapper.Create <TFunction>(functionPtr, FunctionAttribute.Microsoft, reloadedFunctionAttribute).ToSigned();
            }
            else
            {
                reverseFunctionWrapper.WrapperPointer = Utilities.CreateJump(functionPtr, true, Constants.MaxAbsJmpSize).ToSigned();
            }
        }
Beispiel #4
0
        /// <summary>
        /// Creates a wrapper function which allows you to call a function with a custom calling convention using the calling convention of
        /// <typeparamref name="TFunction"/>.
        /// </summary>
        /// <param name="functionAddress">Address of the function to wrap.</param>
        /// <param name="wrapperAddress">
        ///     Address of the wrapper used to call the original function.
        ///     If the original function uses the Microsoft calling convention, it is equal to the function address.
        /// </param>
        /// <returns>Address of the wrapper in native memory.</returns>
        public static IntPtr CreatePointer <TFunction>(long functionAddress, out IntPtr wrapperAddress)
        {
            var attribute = FunctionAttribute.GetAttribute <TFunction>();

            wrapperAddress = (IntPtr)functionAddress;

            // Hot path: Microsoft X64 functions require no wrapping.
            if (!attribute.Equals(FunctionAttribute.Microsoft))
            {
                wrapperAddress = Create <TFunction>((IntPtr)functionAddress, attribute, FunctionAttribute.Microsoft);
            }

            return(wrapperAddress);
        }
Beispiel #5
0
        /// <summary>
        /// Creates a wrapper function which allows you to call a function with a custom calling convention using the calling convention of
        /// <typeparamref name="TFunction"/>.
        /// </summary>
        /// <param name="functionAddress">Address of the function to wrap.</param>
        /// <param name="wrapperAddress">
        ///     Address of the wrapper used to call the original function.
        ///     If the original function uses the Microsoft calling convention, it is equal to the function address.
        /// </param>
        /// <returns>Address of the wrapper in native memory.</returns>
        public static nuint CreatePointer <
#if NET5_0_OR_GREATER
            [DynamicallyAccessedMembers(Trimming.ReloadedAttributeTypes)]
#endif
            TFunction>(nuint functionAddress, out nuint wrapperAddress)
        {
            var attribute = FunctionAttribute.GetAttribute <TFunction>();

            wrapperAddress = functionAddress;

            // Hot path: Microsoft X64 functions require no wrapping.
            if (!attribute.Equals(FunctionAttribute.Microsoft))
            {
                wrapperAddress = Create <TFunction>(functionAddress, attribute, FunctionAttribute.Microsoft);
            }

            return(wrapperAddress);
        }