/*
* Process Hacker -
* windows handle
*
* Copyright (C) 2008-2009 wj32
*
* This file is part of Process Hacker.
*
* Process Hacker is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Process Hacker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Process Hacker. If not, see .
*/
using System;
using System.Threading;
using ProcessHacker.Native.Api;
namespace ProcessHacker.Native.Objects
{
///
/// Represents a generic Windows handle.
///
public class Win32Handle : Win32Handle
{
///
/// Creates a new, invalid handle. You must set the handle using the Handle property.
///
protected Win32Handle()
: base()
{ }
///
/// 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)
: base(handle)
{ }
///
/// 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)
: base(handle, owned)
{ }
///
/// Creates a new handle by duplicating an existing handle.
///
/// The existing handle.
/// The desired access to the object.
public Win32Handle(int handle, int desiredAccess)
: base(handle, desiredAccess)
{ }
///
/// Creates a new handle by duplicating an existing handle from another process.
///
/// A handle to a process. It must have the PROCESS_DUP_HANDLE permission.
/// The existing handle.
/// The desired access to the object.
public Win32Handle(ProcessHandle processHandle, int handle, int desiredAccess)
: base(processHandle, handle, desiredAccess)
{ }
}
///
/// Represents a generic Windows handle.
///
public class Win32Handle : ISynchronizable, IDisposable
where TAccess : struct
{
private object _disposeLock = new object();
private bool _owned = true;
private bool _disposed = false;
private int _handle;
public static implicit operator int(Win32Handle handle)
{
return handle.Handle;
}
public static implicit operator IntPtr(Win32Handle handle)
{
return new IntPtr(handle.Handle);
}
///
/// Creates a new, invalid handle. You must set the handle using the Handle property.
///
protected Win32Handle()
{ }
///
/// 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;
}
///
/// 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;
}
///
/// Creates a new handle by duplicating an existing handle.
///
/// The existing handle.
/// The desired access to the object.
public Win32Handle(int handle, TAccess access)
{
Win32.DuplicateObject(-1, handle, -1, out _handle,
(int)Convert.ChangeType(access, typeof(int)), 0, 0);
_owned = true;
}
///
/// Creates a new handle by duplicating an existing handle from another process.
///
/// A handle to a process. It must have the PROCESS_DUP_HANDLE permission.
/// The existing handle.
/// The desired access to the object.
public Win32Handle(ProcessHandle processHandle, int handle, TAccess access)
{
Win32.DuplicateObject(processHandle, handle, -1, out _handle,
(int)Convert.ChangeType(access, typeof(int)), 0, 0);
_owned = true;
}
///
/// Gets whether this handle is closed.
///
public bool Disposed
{
get { return _disposed; }
}
///
/// 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; }
}
///
/// Duplicates the handle.
///
/// The desired access to the object.
/// A handle.
public Win32Handle Duplicate(TAccess access)
{
return new Win32Handle(ProcessHandle.GetCurrent(), this, access);
}
///
/// Gets certain information about the handle.
///
/// A HANDLE_FLAGS value.
public HandleFlags GetHandleInformation()
{
HandleFlags flags;
if (!Win32.GetHandleInformation(this, out flags))
Win32.ThrowLastError();
return flags;
}
///
/// Gets the handle's name.
///
/// A string.
public string GetHandleName()
{
int status;
int retLength;
status = Win32.NtQueryObject(this, ObjectInformationClass.ObjectNameInformation,
IntPtr.Zero, 0, out retLength);
if (retLength > 0)
{
using (MemoryAlloc oniMem = new MemoryAlloc(retLength))
{
if ((status = Win32.NtQueryObject(this, ObjectInformationClass.ObjectNameInformation,
oniMem.Memory, oniMem.Size, out retLength)) < 0)
Win32.ThrowLastError(status);
ObjectNameInformation oni = oniMem.ReadStruct();
return Utils.ReadUnicodeString(oni.Name);
}
}
else
{
Win32.ThrowLastError(status);
}
return null;
}
///
/// Sets certain information about the handle.
///
/// Specifies which flags to set.
/// The values of the flags to set.
public void SetHandleInformation(HandleFlags mask, HandleFlags flags)
{
if (!Win32.SetHandleInformation(this, mask, flags))
Win32.ThrowLastError();
}
///
/// Waits for the object.
///
public WaitResult Wait()
{
return Win32.WaitForSingleObject(this, 0xffffffff);
}
///
/// Waits for the object with a timeout.
///
/// The timeout of the wait.
public WaitResult Wait(uint timeout)
{
return Win32.WaitForSingleObject(this, timeout);
}
///
/// 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()
{
Win32.CloseHandle(_handle);
}
~Win32Handle()
{
this.Dispose(false);
}
private void Dispose(bool disposing)
{
try
{
if (disposing)
Monitor.Enter(_disposeLock);
if (!_disposed && _owned)
{
this.Close();
_disposed = true;
}
}
finally
{
if (disposing)
Monitor.Exit(_disposeLock);
}
}
///
/// Closes the handle.
///
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
}
}