/* * 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.Runtime.InteropServices; namespace ProcessHacker { public partial class Win32 { /// /// Represents a generic Windows handle. /// public class Win32Handle : IDisposable { private bool _owned = true; private bool _closed = 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; } /// /// 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; } } /// /// Gets certain information about the handle. /// /// A HANDLE_FLAGS value. public HANDLE_FLAGS GetHandleInformation() { HANDLE_FLAGS flags; if (!Win32.GetHandleInformation(this, out flags)) ThrowLastWin32Error(); return flags; } /// /// Sets certain information about the handle. /// /// Specifies which flags to set. /// The values of the flags to set. public void SetHandleInformation(HANDLE_FLAGS mask, HANDLE_FLAGS flags) { if (!Win32.SetHandleInformation(this, mask, flags)) ThrowLastWin32Error(); } /// /// 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); } ~Win32Handle() { this.Dispose(); } /// /// Closes the handle. /// public void Dispose() { lock (this) { if (!_closed && _owned) { _closed = true; Close(); GC.SuppressFinalize(this); } } } } } }