Files
mirror-processhacker/trunk/ProcessHacker.Native/Objects/TimerHandle.cs
T
wj32 2199520ed5 * moved native API definitions into separate files
* added remaining NT objects
* added NtStatus enum
* added Semaphore properties
* added ObjectFlags to every object
* other refactoring

git-svn-id: svn://svn.code.sf.net/p/processhacker/code@1301 21ef857c-d57f-4fe0-8362-d861dc6d29cd
2009-05-20 07:05:02 +00:00

124 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using ProcessHacker.Native.Api;
using ProcessHacker.Native.Security;
using System.Runtime.InteropServices;
namespace ProcessHacker.Native.Objects
{
public class TimerHandle : Win32Handle<TimerAccess>
{
public static TimerHandle Create(TimerAccess access, TimerType type)
{
return Create(access, null, type);
}
public static TimerHandle Create(TimerAccess access, string name, TimerType type)
{
return Create(access, name, null, type);
}
public static TimerHandle Create(TimerAccess access, string name, DirectoryHandle rootDirectory, TimerType type)
{
NtStatus status;
ObjectAttributes oa = new ObjectAttributes(name, 0, rootDirectory);
IntPtr handle;
try
{
if ((status = Win32.NtCreateTimer(out handle, access, ref oa, type)) >= NtStatus.Error)
Win32.ThrowLastError(status);
}
finally
{
oa.Dispose();
}
return new TimerHandle(handle, true);
}
private TimerApcRoutine _routine;
private TimerHandle(IntPtr handle, bool owned)
: base(handle, owned)
{ }
public TimerHandle(string name, DirectoryHandle rootDirectory, TimerAccess access)
{
NtStatus status;
ObjectAttributes oa = new ObjectAttributes(name, 0, rootDirectory);
IntPtr handle;
try
{
if ((status = Win32.NtOpenTimer(out handle, access, ref oa)) >= NtStatus.Error)
Win32.ThrowLastError(status);
}
finally
{
oa.Dispose();
}
this.Handle = handle;
}
public TimerHandle(string name, TimerAccess access)
: this(name, null, access)
{ }
public bool Cancel()
{
NtStatus status;
bool currentState;
if ((status = Win32.NtCancelTimer(this, out currentState)) >= NtStatus.Error)
Win32.ThrowLastError(status);
return currentState;
}
public TimerBasicInformation GetBasicInformation()
{
NtStatus status;
TimerBasicInformation tbi;
int retLength;
if ((status = Win32.NtQueryTimer(this, TimerInformationClass.TimerBasicInformation,
out tbi, Marshal.SizeOf(typeof(TimerBasicInformation)), out retLength)) >= NtStatus.Error)
Win32.ThrowLastError(status);
return tbi;
}
public bool Set(long dueTime, TimerApcRoutine routine, IntPtr context, bool resume, int period)
{
NtStatus status;
bool previousState;
_routine = routine;
if ((status = Win32.NtSetTimer(this, ref dueTime, routine, context,
resume, period, out previousState)) >= NtStatus.Error)
Win32.ThrowLastError(status);
return previousState;
}
public bool Set(long dueTime, TimerApcRoutine routine, IntPtr context, int period)
{
return this.Set(dueTime, routine, context, false, period);
}
public bool Set(long dueTime, TimerApcRoutine routine, int period)
{
return this.Set(dueTime, routine, IntPtr.Zero, period);
}
public bool Set(long dueTime, int period)
{
return this.Set(dueTime, null, period);
}
}
}