From 89a3546876ea8d6876ddc76fee85a91c7c8c6e89 Mon Sep 17 00:00:00 2001 From: wj32 Date: Thu, 18 Dec 2008 05:31:23 +0000 Subject: [PATCH] add lots of documentation git-svn-id: svn://svn.code.sf.net/p/processhacker/code@316 21ef857c-d57f-4fe0-8362-d861dc6d29cd --- trunk/ProcessHacker/Win32/Win32.cs | 116 +++++++++++++++++++++++++++-- 1 file changed, 110 insertions(+), 6 deletions(-) diff --git a/trunk/ProcessHacker/Win32/Win32.cs b/trunk/ProcessHacker/Win32/Win32.cs index 2f1233541..35cd803e3 100644 --- a/trunk/ProcessHacker/Win32/Win32.cs +++ b/trunk/ProcessHacker/Win32/Win32.cs @@ -27,6 +27,9 @@ using System.ComponentModel; namespace ProcessHacker { + /// + /// Provides interfacing to the Win32 and Native APIs. + /// public partial class Win32 { public unsafe class Unsafe @@ -69,32 +72,78 @@ namespace ProcessHacker } } + /// + /// Represents a generic Windows handle. + /// public class Win32Handle : IDisposable { private bool _owned = true; private bool _closed = false; private int _handle; - public Win32Handle() + public static implicit operator int(Win32Handle handle) + { + return handle.Handle; + } + + /// + /// Creates a new, invalid handle. You must set the handle using the Handle property. + /// + protected Win32Handle() { } - public Win32Handle(int Handle) + /// + /// Creates a new handle using the specified value. The handle will be closed when + /// this object is disposed or garbage-collected. + /// + /// The handle value. + public Win32Handle(int handle) { - _handle = Handle; + _handle = handle; } - public Win32Handle(int Handle, bool Owned) + /// + /// Creates a new handle using the specified value. If owned is set to false, the + /// handle will not be closed automatically. + /// + /// The handle value. + /// Specifies whether the handle will be closed automatically. + public Win32Handle(int handle, bool owned) { - _handle = Handle; - _owned = Owned; + _handle = handle; + _owned = owned; } + /// + /// Gets whether this handle is closed. + /// + public bool Closed + { + get { return _closed; } + } + + /// + /// Gets whether the handle will be automatically closed. + /// + public bool Owned + { + get { return _owned; } + } + + /// + /// Gets the handle value. + /// public int Handle { get { return _handle; } protected set { _handle = value; } } + /// + /// Closes the handle. This method must not be called directly; instead, + /// override this method in a derived class if your handle must be closed + /// with a method other than CloseHandle. + /// protected virtual void Close() { CloseHandle(_handle); @@ -105,6 +154,9 @@ namespace ProcessHacker this.Dispose(); } + /// + /// Closes the handle. + /// public void Dispose() { if (!_closed && _owned) @@ -115,14 +167,36 @@ namespace ProcessHacker } } + /// + /// Represents a Windows object that contains a token. + /// public interface IWithToken { + /// + /// Opens and returns the object's token. + /// + /// A handle to the token. TokenHandle GetToken(); + + /// + /// Opens and returns the object's token. + /// + /// Specifies the desired access to the token. + /// A handle to the token. TokenHandle GetToken(TOKEN_RIGHTS access); } + /// + /// Represents a handle to a Windows process. + /// public class ProcessHandle : Win32Handle, IWithToken { + /// + /// Creates a process handle using an existing handle. + /// The handle will not be closed automatically. + /// + /// The handle value. + /// public static ProcessHandle FromHandle(int Handle) { return new ProcessHandle(Handle, false); @@ -132,10 +206,19 @@ namespace ProcessHacker : base(Handle, Owned) { } + /// + /// Creates a new process handle. + /// + /// The ID of the process to open. public ProcessHandle(int PID) : this(PID, PROCESS_RIGHTS.PROCESS_ALL_ACCESS) { } + /// + /// Creates a new process handle. + /// + /// The ID of the process to open. + /// The desired access to the process. public ProcessHandle(int PID, PROCESS_RIGHTS access) { this.Handle = OpenProcess(access, 0, PID); @@ -144,27 +227,48 @@ namespace ProcessHacker throw new Exception(GetLastErrorMessage()); } + /// + /// Waits for the process. + /// + /// The timeout of the wait. + /// Either WAIT_OBJECT_0, WAIT_TIMEOUT or WAIT_FAILED. public int Wait(int Timeout) { return WaitForSingleObject(this.Handle, Timeout); } + /// + /// Terminates the process. + /// public void Terminate() { this.Terminate(0); } + /// + /// Terminates the process, specifying the exit code. + /// + /// The exit code. public void Terminate(int ExitCode) { if (TerminateProcess(this.Handle, ExitCode) == 0) throw new Exception(GetLastErrorMessage()); } + /// + /// Opens and returns a handle to the process' token. + /// + /// A handle to the process' token. public TokenHandle GetToken() { return GetToken(TOKEN_RIGHTS.TOKEN_ALL_ACCESS); } + /// + /// Opens and returns a handle to the process' token. + /// + /// The desired access to the token. + /// A handle to the process' token. public TokenHandle GetToken(TOKEN_RIGHTS access) { return new TokenHandle(this, access);