See More

using System; using System.Collections.Generic; using System.Diagnostics; using Process.NET.Execution; using Process.NET.Memory; using Process.NET.Modules; using Process.NET.Native.Types; using Process.NET.Threads; using Process.NET.Utilities; using Process.NET.Windows; namespace Process.NET { ///

A class that offsers several tools to interact with a process. /// public class ProcessSharp : ProcessSharp, IProcess where TExecDesc : class { public ProcessSharp(System.Diagnostics.Process native, MemoryType type, bool initializeRemoteProcedures = false, Dictionary cachedAddresses = null, params object[] procedureConstructorParams) : base(native, type) { RemoteExecution = new RemoteExecution(this, initializeRemoteProcedures, cachedAddresses, procedureConstructorParams); } public ProcessSharp(string processName, MemoryType type, bool initializeRemoteProcedures = false, Dictionary cachedAddresses = null, params object[] procedureConstructorParams) : this(ProcessHelper.FromName(processName), type, initializeRemoteProcedures, cachedAddresses, procedureConstructorParams) { } public ProcessSharp(int processId, MemoryType type, bool initializeRemoteProcedures = false, Dictionary cachedAddresses = null, params object[] procedureConstructorParams) : this(ProcessHelper.FromProcessId(processId), type, initializeRemoteProcedures, cachedAddresses, procedureConstructorParams) { } public TExecDesc Procedures { get; set; } } /// A class that offsers several tools to interact with a process. /// public class ProcessSharp : MarshalByRefObject, IProcess { #region Properties & Fields - Non-Public protected bool IsDisposed { get; set; } protected bool MustBeDisposed { get; set; } = true; #endregion #region Constructors /// Initializes a new instance of the class. /// The native process. /// The type of memory being manipulated. public ProcessSharp(System.Diagnostics.Process native, MemoryType type) { native.EnableRaisingEvents = true; native.Exited += (s, e) => { ProcessExited?.Invoke(s, e); HandleProcessExiting(); }; Native = native; Handle = MemoryHelper.OpenProcess(ProcessAccessFlags.AllAccess, Native.Id); switch (type) { case MemoryType.Local: Memory = new LocalProcessMemory(Handle); break; case MemoryType.Remote: Memory = new ExternalProcessMemory(Handle); break; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } native.ErrorDataReceived += OutputDataReceived; native.OutputDataReceived += OutputDataReceived; ThreadFactory = new ThreadFactory(this); ModuleFactory = new ModuleFactory(this); MemoryFactory = new MemoryFactory(this); WindowFactory = new WindowFactory(this); } /// Initializes a new instance of the class. /// Name of the process. /// The type of memory being manipulated. public ProcessSharp(string processName, MemoryType type) : this(ProcessHelper.FromName(processName), type) { } /// Initializes a new instance of the class. /// The process id of the process to open with all rights. /// The type of memory being manipulated. public ProcessSharp(int processId, MemoryType type) : this(ProcessHelper.FromProcessId(processId), type) { } /// Releases unmanaged and - optionally - managed resources. public virtual void Dispose() { if (!IsDisposed) { IsDisposed = true; OnDispose?.Invoke(this, EventArgs.Empty); ThreadFactory?.Dispose(); ModuleFactory?.Dispose(); MemoryFactory?.Dispose(); WindowFactory?.Dispose(); Handle?.Close(); GC.SuppressFinalize(this); } } #endregion #region Properties Impl - Public /// Class for reading and writing memory. public IMemory Memory { get; set; } /// Provide access to the opened process. public System.Diagnostics.Process Native { get; set; } /// The process handle opened with all rights. public SafeMemoryHandle Handle { get; set; } /// Factory for manipulating threads. public IThreadFactory ThreadFactory { get; set; } /// Factory for manipulating modules and libraries. public IModuleFactory ModuleFactory { get; set; } /// Factory for manipulating memory space. public IMemoryFactory MemoryFactory { get; set; } /// Factory for manipulating windows. public IWindowFactory WindowFactory { get; set; } /// /// Enable execution of remote procedures by loading IProcedure members from /// public IRemoteExecution RemoteExecution { get; set; } /// Gets the with the specified module name. /// Name of the module. /// IProcessModule. public IProcessModule this[string moduleName] => ModuleFactory[moduleName]; /// Gets the with the specified address. /// The address the pointer is located at in memory. /// IPointer. public IPointer this[IntPtr intPtr] => new MemoryPointer(this, intPtr); public IPointer this[ObjPtr objPtr] => new MemoryPointer(this, objPtr); #endregion #region Methods Impl ~ProcessSharp() { if (MustBeDisposed) Dispose(); } #endregion #region Methods /// Handles the process exiting. /// Created 2012-02-15 protected virtual void HandleProcessExiting() { } private static void OutputDataReceived(object sender, DataReceivedEventArgs e) { Trace.WriteLine(e.Data); } #endregion #region Events /// Raises when the object is disposed. public event EventHandler OnDispose; /// Event queue for all listeners interested in ProcessExited events. public event EventHandler ProcessExited; #endregion } }