Beispiel #1
0
        /// <summary>
        /// Writes memory of an object passed to the process specified at the address provided.
        /// </summary>
        /// <param name="hProcess">Pointer to process where write memory.</param>
        /// <param name="address">Address of the process at which memory writing should occur.</param>
        /// <param name="value"><see cref="IConvertible"/> to write.</param>
        /// <returns><see cref="InjectResult"/> of the memory writing.</returns>
        public static InjectResult WriteMemory(IntPtr hProcess, uint address, IConvertible value)
        {
            var array = value.GetMemory();

            if (array == null)
            {
                return(InjectResult.ByteCastFailure);
            }
            NativeCall.VirtualProtect(hProcess, (IntPtr)address, (UIntPtr)array.Length, 4, out var old);
            NativeCall.WriteProcessMemory(hProcess, (IntPtr)address, array, (uint)array.Length, out var num);
            NativeCall.VirtualProtect(hProcess, (IntPtr)address, (UIntPtr)array.Length, old, out old);
            return(array.Length == num ? InjectResult.Success : InjectResult.WritingFailed);
        }
Beispiel #2
0
        /// <summary>
        /// Writes memory of an object passed to the process specified at the address provided.
        /// </summary>
        /// <param name="process">Process where write memory.</param>
        /// <param name="address">Address of the process at which memory writing should occur.</param>
        /// <param name="value"><see cref="IConvertible"/> to write.</param>
        /// <returns><see cref="InjectResult"/> of the memory writing.</returns>
        public static InjectResult WriteMemory(Process process, uint address, IConvertible value)
        {
            var hProcess = NativeCall.OpenProcess(ProcessAccessFlags.All, false, process.Id);
            var array    = value.GetMemory();

            if (array == null)
            {
                return(InjectResult.ByteCastFailure);
            }
            NativeCall.VirtualProtect(hProcess, (IntPtr)address, (UIntPtr)array.Length, 4, out var old);
            NativeCall.WriteProcessMemory(hProcess, (IntPtr)address, array, (uint)array.Length, out var num);
            NativeCall.VirtualProtect(hProcess, (IntPtr)address, (UIntPtr)array.Length, old, out old);
            NativeCall.CloseHandle(hProcess);
            return(array.Length == num ? InjectResult.Success : InjectResult.WritingFailed);
        }