See More

using System; using System.Diagnostics; 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 : IProcess { /// /// 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) { } /// /// Raises when the object is disposed. /// public event EventHandler OnDispose; /// /// 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; } /// /// 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); protected bool IsDisposed { get; set; } protected bool MustBeDisposed { get; set; } = true; /// /// 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); } } /// /// Handles the process exiting. /// /// Created 2012-02-15 protected virtual void HandleProcessExiting() { } /// /// Event queue for all listeners interested in ProcessExited events. /// public event EventHandler ProcessExited; private static void OutputDataReceived(object sender, DataReceivedEventArgs e) { Trace.WriteLine(e.Data); } ~ProcessSharp() { if (MustBeDisposed) { Dispose(); } } } }