/*
* Process Hacker -
* event pair handle
*
* Copyright (C) 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 ProcessHacker.Native.Api;
using ProcessHacker.Native.Security;
namespace ProcessHacker.Native.Objects
{
///
/// Represents an event pair, an object consisting of two events, high and low.
///
public sealed class EventPairHandle : NativeHandle
{
///
/// Creates an unnamed event pair.
///
/// The desired access to the event pair.
/// A handle to an event pair.
public static EventPairHandle Create(EventPairAccess access)
{
return Create(access, null, 0, null);
}
///
/// Creates an event pair.
///
/// The desired access to the event pair.
///
/// The name of the event pair. If rootDirectory is null, you must specify a fully
/// qualified name. Example: \BaseNamedObjects\MyEventPair.
///
/// The flags to use when creating the object.
///
/// The directory in which to place the event pair. This can be null.
///
/// A handle to an event pair.
public static EventPairHandle Create(EventPairAccess access, string name, ObjectFlags objectFlags, DirectoryHandle rootDirectory)
{
NtStatus status;
ObjectAttributes oa = new ObjectAttributes(name, objectFlags, rootDirectory);
IntPtr handle;
try
{
if ((status = Win32.NtCreateEventPair(out handle, access, ref oa)) >= NtStatus.Error)
Win32.Throw(status);
}
finally
{
oa.Dispose();
}
return new EventPairHandle(handle, true);
}
public static EventPairHandle FromHandle(IntPtr handle)
{
return new EventPairHandle(handle, false);
}
private EventPairHandle(IntPtr handle, bool owned)
: base(handle, owned)
{ }
///
/// Opens a named event pair.
///
///
/// The name of the event pair. If rootDirectory is null,
/// you must specify a fully qualified name.
/// The flags to use when opening the object.
/// The directory object in which the event pair can be found.
/// The desired access to the event pair.
public EventPairHandle(string name, ObjectFlags objectFlags, DirectoryHandle rootDirectory, EventPairAccess access)
{
NtStatus status;
ObjectAttributes oa = new ObjectAttributes(name, objectFlags, rootDirectory);
IntPtr handle;
try
{
if ((status = Win32.NtOpenEventPair(out handle, access, ref oa)) >= NtStatus.Error)
Win32.Throw(status);
}
finally
{
oa.Dispose();
}
this.Handle = handle;
}
public EventPairHandle(string name, EventPairAccess access)
: this(name, 0, null, access)
{ }
///
/// Sets the high event.
///
public void SetHigh()
{
NtStatus status;
if ((status = Win32.NtSetHighEventPair(this)) >= NtStatus.Error)
Win32.Throw(status);
}
///
/// Sets the high event and waits for the low event.
///
public NtStatus SetHighWaitLow()
{
NtStatus status;
if ((status = Win32.NtSetHighWaitLowEventPair(this)) >= NtStatus.Error)
Win32.Throw(status);
return status;
}
///
/// Sets the low event.
///
public void SetLow()
{
NtStatus status;
if ((status = Win32.NtSetLowEventPair(this)) >= NtStatus.Error)
Win32.Throw(status);
}
///
/// Sets the low event and waits for the high event.
///
public NtStatus SetLowWaitHigh()
{
NtStatus status;
if ((status = Win32.NtSetLowWaitHighEventPair(this)) >= NtStatus.Error)
Win32.Throw(status);
return status;
}
///
/// Waits for the high event.
///
public NtStatus WaitHigh()
{
NtStatus status;
if ((status = Win32.NtWaitHighEventPair(this)) >= NtStatus.Error)
Win32.Throw(status);
return status;
}
///
/// Waits for the low event.
///
public NtStatus WaitLow()
{
NtStatus status;
if ((status = Win32.NtWaitLowEventPair(this)) >= NtStatus.Error)
Win32.Throw(status);
return status;
}
}
}