mirror of
https://github.com/mirror/processhacker
synced 2026-06-08 16:03:24 +00:00
improved synchronization methods, added UserHandle
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@1311 21ef857c-d57f-4fe0-8362-d861dc6d29cd
This commit is contained in:
@@ -28,7 +28,7 @@ using ProcessHacker.Native.Security;
|
||||
|
||||
namespace ProcessHacker.Native.Objects
|
||||
{
|
||||
public class DesktopHandle : Win32Handle<DesktopAccess>
|
||||
public class DesktopHandle : UserHandle<DesktopAccess>
|
||||
{
|
||||
public static DesktopHandle GetCurrent()
|
||||
{
|
||||
|
||||
@@ -32,9 +32,11 @@ namespace ProcessHacker.Native.Objects
|
||||
{
|
||||
IntPtr Handle { get; }
|
||||
|
||||
NtStatus SignalAndWait(ISynchronizable waitObject);
|
||||
NtStatus SignalAndWait(ISynchronizable waitObject, bool alertable);
|
||||
NtStatus SignalAndWait(ISynchronizable waitObject, bool alertable, long timeout);
|
||||
NtStatus Wait();
|
||||
NtStatus Wait(long timeout);
|
||||
NtStatus Wait(bool alertable);
|
||||
NtStatus Wait(bool alertable, long timeout);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,50 +86,66 @@ namespace ProcessHacker.Native.Objects
|
||||
: this(name, null, 0, access)
|
||||
{ }
|
||||
|
||||
public void Release(IntPtr key, bool alertable, long timeout)
|
||||
public NtStatus ReleaseKey(int key)
|
||||
{
|
||||
return this.ReleaseKey(key, -1);
|
||||
}
|
||||
|
||||
public NtStatus ReleaseKey(int key, long timeout)
|
||||
{
|
||||
return this.ReleaseKey(key, false, timeout);
|
||||
}
|
||||
|
||||
public NtStatus ReleaseKey(int key, bool alertable, long timeout)
|
||||
{
|
||||
return this.ReleaseKey(new IntPtr(key), alertable, timeout, true);
|
||||
}
|
||||
|
||||
public NtStatus ReleaseKey(IntPtr key, bool alertable, long timeout, bool relative)
|
||||
{
|
||||
NtStatus status;
|
||||
long realTimeout = relative ? -timeout : timeout;
|
||||
|
||||
if ((status = Win32.NtReleaseKeyedEvent(this, key, alertable, ref timeout)) >= NtStatus.Error)
|
||||
if ((status = Win32.NtReleaseKeyedEvent(
|
||||
this,
|
||||
key,
|
||||
alertable,
|
||||
ref realTimeout
|
||||
)) >= NtStatus.Error)
|
||||
Win32.ThrowLastError(status);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
public void Release(int key, bool alertable, long timeout)
|
||||
public NtStatus WaitKey(int key)
|
||||
{
|
||||
this.Release(new IntPtr(key), alertable, timeout);
|
||||
return this.WaitKey(key, -1);
|
||||
}
|
||||
|
||||
public void Release(int key, long timeout)
|
||||
public NtStatus WaitKey(int key, long timeout)
|
||||
{
|
||||
this.Release(key, false, timeout);
|
||||
return this.WaitKey(key, false, timeout);
|
||||
}
|
||||
|
||||
public void Release(int key)
|
||||
public NtStatus WaitKey(int key, bool alertable, long timeout)
|
||||
{
|
||||
this.Release(key, -1);
|
||||
return this.WaitKey(new IntPtr(key), alertable, timeout, true);
|
||||
}
|
||||
|
||||
public void Wait(IntPtr key, bool alertable, long timeout)
|
||||
public NtStatus WaitKey(IntPtr key, bool alertable, long timeout, bool relative)
|
||||
{
|
||||
NtStatus status;
|
||||
long realTimeout = relative ? -timeout : timeout;
|
||||
|
||||
if ((status = Win32.NtWaitForKeyedEvent(this, key, alertable, ref timeout)) >= NtStatus.Error)
|
||||
if ((status = Win32.NtWaitForKeyedEvent(
|
||||
this,
|
||||
key,
|
||||
alertable,
|
||||
ref realTimeout
|
||||
)) >= NtStatus.Error)
|
||||
Win32.ThrowLastError(status);
|
||||
}
|
||||
|
||||
public void Wait(int key, bool alertable, long timeout)
|
||||
{
|
||||
this.Wait(new IntPtr(key), alertable, timeout);
|
||||
}
|
||||
|
||||
public void Wait(int key, long timeout)
|
||||
{
|
||||
this.Wait(key, false, timeout);
|
||||
}
|
||||
|
||||
public void Wait(int key)
|
||||
{
|
||||
this.Wait(key, -1);
|
||||
return status;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,6 +104,11 @@ namespace ProcessHacker.Native.Objects
|
||||
return sbi;
|
||||
}
|
||||
|
||||
public int Release()
|
||||
{
|
||||
return this.Release(1);
|
||||
}
|
||||
|
||||
public int Release(int count)
|
||||
{
|
||||
NtStatus status;
|
||||
@@ -114,10 +119,5 @@ namespace ProcessHacker.Native.Objects
|
||||
|
||||
return previousCount;
|
||||
}
|
||||
|
||||
public int Release()
|
||||
{
|
||||
return this.Release(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Process Hacker -
|
||||
* USER 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using ProcessHacker.Native.Api;
|
||||
using ProcessHacker.Native.Security;
|
||||
|
||||
namespace ProcessHacker.Native.Objects
|
||||
{
|
||||
public abstract class UserHandle<TAccess> : Win32Handle<TAccess>
|
||||
where TAccess : struct
|
||||
{
|
||||
protected UserHandle()
|
||||
: base()
|
||||
{ }
|
||||
|
||||
protected UserHandle(IntPtr handle, bool owned)
|
||||
: base(handle, owned)
|
||||
{ }
|
||||
|
||||
public override SecurityDescriptor GetSecurity()
|
||||
{
|
||||
return this.GetSecurity(SeObjectType.WindowObject);
|
||||
}
|
||||
|
||||
public override void SetSecurity(SecurityDescriptor securityDescriptor)
|
||||
{
|
||||
this.SetSecurity(SeObjectType.WindowObject, securityDescriptor);
|
||||
}
|
||||
|
||||
public override void SetSecurity(SecurityInformation securityInformation, SecurityDescriptor securityDescriptor)
|
||||
{
|
||||
this.SetSecurity(SeObjectType.WindowObject, securityInformation, securityDescriptor);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,13 +28,13 @@ using ProcessHacker.Native.Security;
|
||||
namespace ProcessHacker.Native.Objects
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a generic Windows handle.
|
||||
/// Represents a generic Windows handle which acts as a kernel handle by default.
|
||||
/// </summary>
|
||||
public class Win32Handle : Win32Handle<int>
|
||||
{
|
||||
public static NtStatus WaitAll(ISynchronizable[] objects, bool alertable, long timeout)
|
||||
public static NtStatus WaitAll(ISynchronizable[] objects)
|
||||
{
|
||||
return WaitForMultipleObjects(objects, WaitType.WaitAll, alertable, timeout);
|
||||
return WaitAll(objects, long.MinValue);
|
||||
}
|
||||
|
||||
public static NtStatus WaitAll(ISynchronizable[] objects, long timeout)
|
||||
@@ -42,19 +42,14 @@ namespace ProcessHacker.Native.Objects
|
||||
return WaitAll(objects, false, timeout);
|
||||
}
|
||||
|
||||
public static NtStatus WaitAll(ISynchronizable[] objects)
|
||||
public static NtStatus WaitAll(ISynchronizable[] objects, bool alertable, long timeout)
|
||||
{
|
||||
return WaitAll(objects, long.MinValue);
|
||||
return WaitAll(objects, alertable, timeout, true);
|
||||
}
|
||||
|
||||
public static NtStatus WaitAny(ISynchronizable[] objects, bool alertable, long timeout)
|
||||
public static NtStatus WaitAll(ISynchronizable[] objects, bool alertable, long timeout, bool relative)
|
||||
{
|
||||
return WaitForMultipleObjects(objects, WaitType.WaitAny, alertable, timeout);
|
||||
}
|
||||
|
||||
public static NtStatus WaitAny(ISynchronizable[] objects, long timeout)
|
||||
{
|
||||
return WaitAny(objects, false, timeout);
|
||||
return WaitForMultipleObjects(objects, WaitType.WaitAll, alertable, timeout, relative);
|
||||
}
|
||||
|
||||
public static NtStatus WaitAny(ISynchronizable[] objects)
|
||||
@@ -62,10 +57,26 @@ namespace ProcessHacker.Native.Objects
|
||||
return WaitAny(objects, long.MinValue);
|
||||
}
|
||||
|
||||
private static NtStatus WaitForMultipleObjects(ISynchronizable[] objects, WaitType waitType, bool alertable, long timeout)
|
||||
public static NtStatus WaitAny(ISynchronizable[] objects, long timeout)
|
||||
{
|
||||
return WaitAny(objects, false, timeout);
|
||||
}
|
||||
|
||||
public static NtStatus WaitAny(ISynchronizable[] objects, bool alertable, long timeout)
|
||||
{
|
||||
return WaitAny(objects, alertable, timeout, true);
|
||||
}
|
||||
|
||||
public static NtStatus WaitAny(ISynchronizable[] objects, bool alertable, long timeout, bool relative)
|
||||
{
|
||||
return WaitForMultipleObjects(objects, WaitType.WaitAny, alertable, timeout, relative);
|
||||
}
|
||||
|
||||
private static NtStatus WaitForMultipleObjects(ISynchronizable[] objects, WaitType waitType, bool alertable, long timeout, bool relative)
|
||||
{
|
||||
NtStatus status;
|
||||
IntPtr[] handles = new IntPtr[objects.Length];
|
||||
long realTimeout = relative ? -timeout : timeout;
|
||||
|
||||
for (int i = 0; i < objects.Length; i++)
|
||||
handles[i] = objects[i].Handle;
|
||||
@@ -75,7 +86,7 @@ namespace ProcessHacker.Native.Objects
|
||||
handles,
|
||||
waitType,
|
||||
alertable,
|
||||
ref timeout
|
||||
ref realTimeout
|
||||
)) >= NtStatus.Error)
|
||||
Win32.ThrowLastError(status);
|
||||
|
||||
@@ -129,7 +140,7 @@ namespace ProcessHacker.Native.Objects
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a generic Windows handle.
|
||||
/// Represents a generic Windows handle which acts as a kernel handle by default.
|
||||
/// </summary>
|
||||
public class Win32Handle<TAccess> : IDisposable, ISecurable, ISynchronizable
|
||||
where TAccess : struct
|
||||
@@ -285,13 +296,18 @@ namespace ProcessHacker.Native.Objects
|
||||
}
|
||||
|
||||
public virtual SecurityDescriptor GetSecurity()
|
||||
{
|
||||
return this.GetSecurity(SeObjectType.KernelObject);
|
||||
}
|
||||
|
||||
protected SecurityDescriptor GetSecurity(SeObjectType objectType)
|
||||
{
|
||||
int result;
|
||||
IntPtr dummy, securityDescriptor;
|
||||
|
||||
if ((result = Win32.GetSecurityInfo(
|
||||
this,
|
||||
SeObjectType.KernelObject,
|
||||
objectType,
|
||||
0,
|
||||
out dummy, out dummy, out dummy, out dummy,
|
||||
out securityDescriptor
|
||||
@@ -336,6 +352,16 @@ namespace ProcessHacker.Native.Objects
|
||||
}
|
||||
|
||||
public virtual void SetSecurity(SecurityDescriptor securityDescriptor)
|
||||
{
|
||||
this.SetSecurity(SeObjectType.KernelObject, securityDescriptor);
|
||||
}
|
||||
|
||||
public virtual void SetSecurity(SecurityInformation securityInformation, SecurityDescriptor securityDescriptor)
|
||||
{
|
||||
this.SetSecurity(SeObjectType.KernelObject, securityInformation, securityDescriptor);
|
||||
}
|
||||
|
||||
protected void SetSecurity(SeObjectType objectType, SecurityDescriptor securityDescriptor)
|
||||
{
|
||||
int result;
|
||||
IntPtr owner, group, dacl, sacl;
|
||||
@@ -353,7 +379,7 @@ namespace ProcessHacker.Native.Objects
|
||||
|
||||
if ((result = Win32.SetSecurityInfo(
|
||||
this,
|
||||
SeObjectType.KernelObject,
|
||||
objectType,
|
||||
si,
|
||||
owner,
|
||||
group,
|
||||
@@ -363,7 +389,7 @@ namespace ProcessHacker.Native.Objects
|
||||
Win32.ThrowLastError(result);
|
||||
}
|
||||
|
||||
public virtual void SetSecurity(SecurityInformation securityInformation, SecurityDescriptor securityDescriptor)
|
||||
protected void SetSecurity(SeObjectType objectType, SecurityInformation securityInformation, SecurityDescriptor securityDescriptor)
|
||||
{
|
||||
int result;
|
||||
IntPtr owner, group, dacl, sacl;
|
||||
@@ -376,7 +402,7 @@ namespace ProcessHacker.Native.Objects
|
||||
|
||||
if ((result = Win32.SetSecurityInfo(
|
||||
this,
|
||||
SeObjectType.KernelObject,
|
||||
objectType,
|
||||
securityInformation,
|
||||
owner,
|
||||
group,
|
||||
@@ -386,12 +412,47 @@ namespace ProcessHacker.Native.Objects
|
||||
Win32.ThrowLastError(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Signals the object and waits for another.
|
||||
/// </summary>
|
||||
public virtual NtStatus SignalAndWait(ISynchronizable waitObject)
|
||||
{
|
||||
return this.SignalAndWait(waitObject, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Signals the object and waits for another.
|
||||
/// </summary>
|
||||
public virtual NtStatus SignalAndWait(ISynchronizable waitObject, bool alertable)
|
||||
{
|
||||
return this.SignalAndWait(waitObject, alertable, long.MinValue, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Signals the object and waits for another.
|
||||
/// </summary>
|
||||
public virtual NtStatus SignalAndWait(ISynchronizable waitObject, bool alertable, long timeout)
|
||||
{
|
||||
return Win32.NtSignalAndWaitForSingleObject(this, waitObject.Handle, alertable, ref timeout);
|
||||
return this.SignalAndWait(waitObject, alertable, timeout, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Signals the object and waits for another.
|
||||
/// </summary>
|
||||
public virtual NtStatus SignalAndWait(ISynchronizable waitObject, bool alertable, long timeout, bool relative)
|
||||
{
|
||||
NtStatus status;
|
||||
long realTimeout = relative ? -timeout : timeout;
|
||||
|
||||
if ((status = Win32.NtSignalAndWaitForSingleObject(
|
||||
this,
|
||||
waitObject.Handle,
|
||||
alertable,
|
||||
ref timeout
|
||||
)) >= NtStatus.Error)
|
||||
Win32.ThrowLastError(status);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -410,26 +471,16 @@ namespace ProcessHacker.Native.Objects
|
||||
/// </param>
|
||||
public virtual NtStatus Wait(bool alertable)
|
||||
{
|
||||
return this.Wait(alertable, long.MinValue);
|
||||
return this.Wait(alertable, long.MinValue, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Waits for the object to be signaled.
|
||||
/// </summary>
|
||||
/// <param name="timeout">The timeout value.</param>
|
||||
public virtual NtStatus Wait(long timeout)
|
||||
public NtStatus Wait(long timeout)
|
||||
{
|
||||
return this.Wait(timeout, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Waits for the object to be signaled.
|
||||
/// </summary>
|
||||
/// <param name="timeout">The timeout value.</param>
|
||||
/// <param name="relative">Whether the timeout value is relative.</param>
|
||||
public virtual NtStatus Wait(long timeout, bool relative)
|
||||
{
|
||||
return this.Wait(false, relative ? -timeout : timeout);
|
||||
return this.Wait(false, timeout);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -438,18 +489,39 @@ namespace ProcessHacker.Native.Objects
|
||||
/// <param name="alertable">
|
||||
/// Whether user-mode APCs can be delivered during the wait.
|
||||
/// </param>
|
||||
/// <param name="timeout">
|
||||
/// Zero for no timeout (immediate return if the object is not signaled),
|
||||
/// a negative relative timeout or a positive absolute timeout.
|
||||
/// </param>
|
||||
/// <param name="timeout">The timeout value.</param>
|
||||
public virtual NtStatus Wait(bool alertable, long timeout)
|
||||
{
|
||||
return this.Wait(alertable, timeout, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Waits for the object to be signaled.
|
||||
/// </summary>
|
||||
/// <param name="timeout">The timeout value.</param>
|
||||
/// <param name="relative">Whether the timeout value is relative.</param>
|
||||
public NtStatus Wait(long timeout, bool relative)
|
||||
{
|
||||
return this.Wait(false, timeout, relative);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Waits for the object to be signaled.
|
||||
/// </summary>
|
||||
/// <param name="alertable">
|
||||
/// Whether user-mode APCs can be delivered during the wait.
|
||||
/// </param>
|
||||
/// <param name="timeout">The timeout value.</param>
|
||||
/// <param name="relative">Whether the timeout value is relative.</param>
|
||||
public virtual NtStatus Wait(bool alertable, long timeout, bool relative)
|
||||
{
|
||||
NtStatus status;
|
||||
long realTimeout = relative ? -timeout : timeout;
|
||||
|
||||
if ((status = Win32.NtWaitForSingleObject(
|
||||
this,
|
||||
alertable,
|
||||
ref timeout
|
||||
ref realTimeout
|
||||
)) >= NtStatus.Error)
|
||||
Win32.ThrowLastError(status);
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ using ProcessHacker.Native.Security;
|
||||
|
||||
namespace ProcessHacker.Native.Objects
|
||||
{
|
||||
public class WindowStationHandle : Win32Handle<WindowStationAccess>
|
||||
public class WindowStationHandle : UserHandle<WindowStationAccess>
|
||||
{
|
||||
public static WindowStationHandle GetCurrent()
|
||||
{
|
||||
|
||||
@@ -69,6 +69,7 @@
|
||||
<Compile Include="Objects\SemaphoreHandle.cs" />
|
||||
<Compile Include="Objects\SymbolicLinkHandle.cs" />
|
||||
<Compile Include="Objects\TimerHandle.cs" />
|
||||
<Compile Include="Objects\UserHandle.cs" />
|
||||
<Compile Include="Security\DirectoryAccess.cs" />
|
||||
<Compile Include="Security\ISecurable.cs" />
|
||||
<Compile Include="Security\ObjectSecurityInformation.cs" />
|
||||
|
||||
Reference in New Issue
Block a user