예제 #1
0
        /// <summary>
        ///     Writes byte array to destination address
        /// </summary>
        /// <param name="_hProcess">Opened process handle</param>
        /// <param name="_targetAddress">Address to write to</param>
        /// <param name="_bytes">Value to write</param>
        /// <returns></returns>
        public static bool WriteBytes(IntPtr _hProcess, IntPtr _targetAddress, byte[] _bytes)
        {
            if (_bytes == null)
            {
                throw new ArgumentNullException("_bytes");
            }
            if (_hProcess == IntPtr.Zero)
            {
                throw new Exception("Target process handle == IntPtr.Zero, open process first");
            }

            var methodDesc = String.Format(
                "WriteBytes(IntPtr 0x{0:X}, IntPtr 0x:{1:X}, byte[] {2}b)",
                _hProcess.ToInt64(),
                _targetAddress.ToInt64(),
                _bytes.Length);

            int written;

            if (OnyxNative.WriteProcessMemory(_hProcess, _targetAddress, _bytes, (uint)_bytes.Length, out written))
            {
                if (written != _bytes.Length)
                {
                    throw new ApplicationException(string.Format("{2} - Could not write all {0} byte(s), only {1} were written", _bytes.Length, written, methodDesc));
                }
            }
            else
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), methodDesc);
            }
            return(true);
        }