Пример #1
0
        public ProcessMemoryStream(int processId, ProcessAccess desiredAccess = ProcessAccess.ReadWrite, int bufferSize = 4096, bool leaveOpen = false)
        {
            if (processId < 0)
                throw new ArgumentOutOfRangeException("Process ID must be a positive value");

            if (bufferSize < 1)
                throw new ArgumentOutOfRangeException("Buffer size must be at least 1 byte");

            var win32Flags = Win32ProcessAccess.VmOperation;

            // If read mode was requested, bitwise OR the flag
            if (desiredAccess.HasFlag(ProcessAccess.Read))
                win32Flags |= Win32ProcessAccess.VmRead;

            // If write mode was requested, bitwise OR the flag
            if (desiredAccess.HasFlag(ProcessAccess.Write))
                win32Flags |= Win32ProcessAccess.VmWrite;

            // Open the process and check if the handle is valid
            this.processAccess = desiredAccess;
            this.processHandle = NativeMethods.OpenProcess(win32Flags, false, processId);
            this.leaveOpen = leaveOpen;

            // Check if handle is valid
            if (this.processHandle.IsInvalid)
            {
                var errorCode = NativeMethods.GetLastError();
                throw new IOException("Unable to open process", errorCode);
            }

            // Allocate read and write buffers
            this.readBuffer = new byte[bufferSize];
            this.writeBuffer = new byte[bufferSize];
        }
Пример #2
0
        internal static ProcessAccessFlags ToWin32Flags(this ProcessAccess access)
        {
            var flags = ProcessAccessFlags.VmOperation | ProcessAccessFlags.QueryInformation;

            if (access.HasFlag(ProcessAccess.Read))
            {
                flags |= ProcessAccessFlags.VmRead;
            }

            if (access.HasFlag(ProcessAccess.Write))
            {
                flags |= ProcessAccessFlags.VmWrite;
            }

            return(flags);
        }
Пример #3
0
        public ProcessMemoryStream(int processId, ProcessAccess desiredAccess = ProcessAccess.ReadWrite, int bufferSize = 4096, bool leaveOpen = false)
        {
            if (processId < 0)
            {
                throw new ArgumentOutOfRangeException("Process ID must be a positive value");
            }

            if (bufferSize < 1)
            {
                throw new ArgumentOutOfRangeException("Buffer size must be at least 1 byte");
            }

            var win32Flags = Win32ProcessAccess.VmOperation;

            // If read mode was requested, bitwise OR the flag
            if (desiredAccess.HasFlag(ProcessAccess.Read))
            {
                win32Flags |= Win32ProcessAccess.VmRead;
            }

            // If write mode was requested, bitwise OR the flag
            if (desiredAccess.HasFlag(ProcessAccess.Write))
            {
                win32Flags |= Win32ProcessAccess.VmWrite;
            }

            // Open the process and check if the handle is valid
            this.processAccess = desiredAccess;
            this.processHandle = NativeMethods.OpenProcess(win32Flags, false, processId);
            this.leaveOpen     = leaveOpen;

            // Check if handle is valid
            if (this.processHandle.IsInvalid)
            {
                var errorCode = NativeMethods.GetLastError();
                throw new IOException("Unable to open process", errorCode);
            }

            // Allocate read and write buffers
            this.readBuffer  = new byte[bufferSize];
            this.writeBuffer = new byte[bufferSize];
        }