This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 223
/
User32+SafeHookHandle.cs
53 lines (47 loc) · 1.98 KB
/
User32+SafeHookHandle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright © .NET Foundation and Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace PInvoke
{
using System;
using System.Runtime.InteropServices;
/// <content>
/// Contains the <see cref="SafeHookHandle"/> nested type.
/// </content>
public static partial class User32
{
/// <summary>
/// Represents a windows Hook that can be removed with <see cref="UnhookWindowsHookEx"/>.
/// </summary>
public class SafeHookHandle : SafeHandle
{
/// <summary>
/// A handle that may be used in place of <see cref="IntPtr.Zero"/>.
/// </summary>
public static readonly SafeHookHandle Null = new SafeHookHandle();
/// <summary>
/// Initializes a new instance of the <see cref="SafeHookHandle"/> class.
/// </summary>
public SafeHookHandle()
: base(IntPtr.Zero, true)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SafeHookHandle"/> class.
/// </summary>
/// <param name="preexistingHandle">An object that represents the pre-existing handle to use.</param>
/// <param name="ownsHandle">
/// <see langword="true" /> to have the native handle released when this safe handle is disposed or finalized;
/// <see langword="false" /> otherwise.
/// </param>
public SafeHookHandle(IntPtr preexistingHandle, bool ownsHandle = true)
: base(IntPtr.Zero, ownsHandle)
{
this.SetHandle(preexistingHandle);
}
/// <inheritdoc />
public override bool IsInvalid => this.handle == IntPtr.Zero;
/// <inheritdoc />
protected override bool ReleaseHandle() => UnhookWindowsHookEx(this.handle);
}
}
}