Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #440 from mckunda/feature/add-get-set-handle-info
Browse files Browse the repository at this point in the history
Add `kernel32!SetHandleInformation` and `kernel32!GetHandleInformation`
  • Loading branch information
AArnott authored Oct 27, 2019
2 parents 69ac737 + 5562534 commit 63daa40
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Kernel32.Tests/Kernel32Facts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using PInvoke;
using Xunit;
using static PInvoke.Kernel32;
Expand Down
17 changes: 17 additions & 0 deletions src/Kernel32.Tests/storebanned/Kernel32Facts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,23 @@ public void MAKELANGID_Simple()
Assert.Equal(0x0409, MAKELANGID(LANGID.PrimaryLanguage.LANG_ENGLISH, LANGID.SubLanguage.SUBLANG_ENGLISH_US).Data);
}

[Fact]
public void GetHandleInformation_DoesNotThrow()
{
var manualResetEvent = new ManualResetEvent(false);
Assert.True(GetHandleInformation(manualResetEvent.SafeWaitHandle, out var lpdwFlags));
}

[Fact]
public void SetHandleInformation_DoesNotThrow()
{
var manualResetEvent = new ManualResetEvent(false);
Assert.True(SetHandleInformation(
manualResetEvent.SafeWaitHandle,
HandleFlags.HANDLE_FLAG_INHERIT | HandleFlags.HANDLE_FLAG_PROTECT_FROM_CLOSE,
HandleFlags.HANDLE_FLAG_NONE));
}

private ArraySegment<byte> GetRandomSegment(int size)
{
var result = new ArraySegment<byte>(new byte[size]);
Expand Down
35 changes: 35 additions & 0 deletions src/Kernel32/Kernel32+HandleFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) All contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace PInvoke
{
using System;

/// <content>
/// Contains the nested <see cref="HandleFlags"/> type.
/// </content>
public static partial class Kernel32
{
[Flags]
public enum HandleFlags
{
/// <summary>
/// Indicates that none of the flags are set.
/// </summary>
/// <remarks>
/// This value is not defined in Win32 API headers.
/// </remarks>
HANDLE_FLAG_NONE = 0,

/// <summary>
/// If this flag is set, a child process created with the bInheritHandles parameter of CreateProcess set to TRUE will inherit the object handle.
/// </summary>
HANDLE_FLAG_INHERIT = 0x00000001,

/// <summary>
/// If this flag is set, calling the <see cref="CloseHandle"/> function will not close the object handle.
/// </summary>
HANDLE_FLAG_PROTECT_FROM_CLOSE = 0x00000002
}
}
}
8 changes: 8 additions & 0 deletions src/Kernel32/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ PInvoke.Kernel32.STARTUPINFO.lpReserved_IntPtr.set -> void
PInvoke.Kernel32.STARTUPINFO.lpTitle -> char*
PInvoke.Kernel32.STARTUPINFO.lpTitle_IntPtr.get -> System.IntPtr
PInvoke.Kernel32.STARTUPINFO.lpTitle_IntPtr.set -> void
PInvoke.Kernel32.HandleFlags
PInvoke.Kernel32.HandleFlags.HANDLE_FLAG_NONE = 0 -> PInvoke.Kernel32.HandleFlags
PInvoke.Kernel32.HandleFlags.HANDLE_FLAG_INHERIT = 1 -> PInvoke.Kernel32.HandleFlags
PInvoke.Kernel32.HandleFlags.HANDLE_FLAG_PROTECT_FROM_CLOSE = 2 -> PInvoke.Kernel32.HandleFlags
static PInvoke.Kernel32.GetStartupInfo(System.IntPtr lpStartupInfo) -> void
static PInvoke.Kernel32.GetStartupInfo(ref PInvoke.Kernel32.STARTUPINFO lpStartupInfo) -> void
static extern PInvoke.Kernel32.GetProcessId(System.IntPtr Process) -> int
Expand All @@ -71,3 +75,7 @@ static extern PInvoke.Kernel32.VerSetConditionMask(long ConditionMask, PInvoke.K
static extern PInvoke.Kernel32.VerifyVersionInfo(PInvoke.Kernel32.OSVERSIONINFOEX* lpVersionInformation, PInvoke.Kernel32.VER_MASK dwTypeMask, long dwlConditionMask) -> PInvoke.NTSTATUS
static PInvoke.Kernel32.VerifyVersionInfo(ref PInvoke.Kernel32.OSVERSIONINFOEX lpVersionInformation, PInvoke.Kernel32.VER_MASK dwTypeMask, long dwlConditionMask) -> PInvoke.NTSTATUS
static PInvoke.Kernel32.VerifyVersionInfo(System.IntPtr lpVersionInformation, PInvoke.Kernel32.VER_MASK dwTypeMask, long dwlConditionMask) -> PInvoke.NTSTATUS
static extern PInvoke.Kernel32.SetHandleInformation(System.Runtime.InteropServices.SafeHandle hObject, PInvoke.Kernel32.HandleFlags dwMask, PInvoke.Kernel32.HandleFlags dwFlags) -> bool
static extern PInvoke.Kernel32.GetHandleInformation(System.Runtime.InteropServices.SafeHandle hObject, PInvoke.Kernel32.HandleFlags* lpdwFlags) -> bool
static PInvoke.Kernel32.GetHandleInformation(System.Runtime.InteropServices.SafeHandle hObject, System.IntPtr lpdwFlags) -> bool
static PInvoke.Kernel32.GetHandleInformation(System.Runtime.InteropServices.SafeHandle hObject, out PInvoke.Kernel32.HandleFlags lpdwFlags) -> bool
58 changes: 58 additions & 0 deletions src/Kernel32/storebanned/Kernel32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2968,5 +2968,63 @@ public static extern unsafe bool DeviceIoControl(
[DllImport(api_ms_win_core_sysinfo_l1_2_0, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern unsafe bool SetSystemTime([Friendly(FriendlyFlags.In)] SYSTEMTIME* lpSystemTime);

/// <summary>
/// Sets certain properties of an object handle.
/// </summary>
/// <param name="hObject">
/// <para>
/// A handle to an object whose information is to be set.
/// </para>
/// <para>
/// You can specify a handle to one of the following types of objects: access token, console input buffer, console screen buffer, event, file, file mapping, job, mailslot, mutex, pipe, printer, process, registry key, semaphore, serial communication device, socket, thread, or waitable timer.
/// </para>
/// </param>
/// <param name="dwMask">
/// A mask that specifies the bit flags to be changed. Use the same constants shown in the description of <paramref name="dwFlags"/>.
/// </param>
/// <param name="dwFlags">
/// Set of bit flags that specifies properties of the object handle. This parameter can be 0 or one or more of the values of <see cref="HandleFlags"/>.
/// </param>
/// <returns>
/// <para>
/// If the function succeeds, the return value is nonzero.
/// </para>
/// <para>
/// If the function fails, the return value is zero. To get extended error information, call <see cref="GetLastError"/>.
/// </para>
/// </returns>
/// <remarks>
/// To set or clear the associated bit flag in <paramref name="dwFlags"/>, you must set a change mask bit flag in <paramref name="dwMask"/>.
/// </remarks>
[DllImport(api_ms_win_core_handle_l1_1_0, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetHandleInformation(SafeHandle hObject, HandleFlags dwMask, HandleFlags dwFlags);

/// <summary>
/// Retrieves certain properties of an object handle.
/// </summary>
/// <param name="hObject">
/// <para>
/// A handle to an object whose information is to be retrieved.
/// </para>
/// <para>
/// You can specify a handle to one of the following types of objects: access token, console input buffer, console screen buffer, event, file, file mapping, job, mailslot, mutex, pipe, printer, process, registry key, semaphore, serial communication device, socket, thread, or waitable timer.
/// </para>
/// </param>
/// <param name="lpdwFlags">
/// A pointer to a variable that receives a set of bit flags that specify properties of the object handle or 0. The values are defined at <see cref="HandleFlags"/>.
/// </param>
/// <returns>
/// <para>
/// If the function succeeds, the return value is nonzero.
/// </para>
/// <para>
/// If the function fails, the return value is zero. To get extended error information, call <see cref="GetLastError"/>.
/// </para>
/// </returns>
[DllImport(api_ms_win_core_handle_l1_1_0, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern unsafe bool GetHandleInformation(SafeHandle hObject, [Friendly(FriendlyFlags.Out)] HandleFlags* lpdwFlags);
}
}

0 comments on commit 63daa40

Please sign in to comment.