-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a Registry class for reading the registry. Also adds the start of a `SpanReader`. Change the root namespace of the test assembly.
- Loading branch information
1 parent
1df1f32
commit 30206b9
Showing
38 changed files
with
590 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) Jeremy W. Kuhne. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Buffers; | ||
|
||
namespace Windows.Support; | ||
|
||
/// <summary> | ||
/// Simple span reader. Follows <see cref="SequenceReader{T}"/>. | ||
/// </summary> | ||
public ref struct SpanReader<T> where T : unmanaged, IEquatable<T> | ||
{ | ||
public ReadOnlySpan<T> Span { get; } | ||
public int Index { get; private set; } | ||
|
||
public SpanReader(ReadOnlySpan<T> span) => Span = span; | ||
|
||
public readonly ReadOnlySpan<T> Remaining => Span[Index..]; | ||
|
||
/// <summary> | ||
/// Try to read everything up to the given <paramref name="delimiter"/>. | ||
/// </summary> | ||
/// <param name="span">The read data, if any.</param> | ||
/// <param name="delimiter">The delimiter to look for.</param> | ||
/// <param name="advancePastDelimiter"><see langword="true"/> to move past the <paramref name="delimiter"/> if found.</param> | ||
/// <returns><see langword="true"/> if the <paramref name="delimiter"/> was found.</returns> | ||
public bool TryReadTo(out ReadOnlySpan<T> span, T delimiter, bool advancePastDelimiter = true) | ||
{ | ||
bool found = false; | ||
ReadOnlySpan<T> remaining = Remaining; | ||
int index = remaining.IndexOf(delimiter); | ||
|
||
if (index != -1) | ||
{ | ||
span = index == 0 ? default : remaining[..index]; | ||
Index += index + (advancePastDelimiter ? 1 : 0); | ||
found = true; | ||
} | ||
else | ||
{ | ||
span = default; | ||
} | ||
|
||
return found; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Jeremy W. Kuhne. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Runtime.InteropServices; | ||
using Windows.Wdk.System.SystemServices; | ||
using Windows.Win32.System.Registry; | ||
|
||
namespace Windows.Wdk; | ||
|
||
#pragma warning disable SA1313 // Parameter names should begin with lower-case letter | ||
|
||
public static partial class Interop | ||
{ | ||
/// <summary> | ||
/// The NtQueryKey routine provides information about the class of a registry key, and the number and sizes of its subkeys. | ||
/// </summary> | ||
/// <param name="Length">Specifies the size, in bytes, of the <paramref name="KeyInformation"/> buffer.</param> | ||
/// <param name="ResultLength"> | ||
/// Pointer to a variable that receives the size, in bytes, of the requested key information. If NtQueryKey returns | ||
/// <see cref="NTSTATUS.STATUS_SUCCESS"/>, the variable contains the amount of data returned. If NwQueryKey returns | ||
/// <see cref="NTSTATUS.STATUS_BUFFER_OVERFLOW"/> or <see cref="NTSTATUS.STATUS_BUFFER_TOO_SMALL"/>, you can use the | ||
/// value of the variable to determine the required buffer size. | ||
/// </param> | ||
/// <remarks> | ||
/// <para> | ||
/// <see href="https://learn.microsoft.com/windows-hardware/drivers/ddi/wdm/nf-wdm-zwquerykey">Read more on learn.microsoft.com</see>. | ||
/// </para> | ||
/// </remarks> | ||
[DllImport("ntdll.dll", ExactSpelling = true)] | ||
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)] | ||
public static extern unsafe NTSTATUS NtQueryKey( | ||
HKEY KeyHandle, | ||
KEY_INFORMATION_CLASS KeyInformationClass, | ||
void* KeyInformation, | ||
uint Length, | ||
uint* ResultLength); | ||
} | ||
|
||
#pragma warning restore SA1313 // Parameter names should begin with lower-case letter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Jeremy W. Kuhne. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Windows.Support; | ||
|
||
namespace Windows.Win32.System.Registry; | ||
|
||
public partial struct HKEY : IDisposable | ||
{ | ||
private const uint REMOTE_HANDLE_TAG = 0x00000001; | ||
private const uint REG_CLASSES_SPECIAL_TAG = 0x00000002; | ||
|
||
public bool IsPerfKey() | ||
=> this == HKEY_PERFORMANCE_DATA || this == HKEY_PERFORMANCE_NLSTEXT || this == HKEY_PERFORMANCE_TEXT; | ||
|
||
/// <summary> | ||
/// Returns true if the key is from the local machine. | ||
/// </summary> | ||
public bool IsLocalKey => (Value & REMOTE_HANDLE_TAG) == 0; | ||
|
||
/// <summary> | ||
/// Returns true if the key is special (notably in <see cref="HKEY.HKEY_CLASSES_ROOT"/>, where | ||
/// it might be redirected to per user settings). | ||
/// </summary> | ||
public bool IsSpecialKey => (Value & REG_CLASSES_SPECIAL_TAG) != 0; | ||
|
||
public void Dispose() | ||
{ | ||
WIN32_ERROR error = Interop.RegCloseKey(this); | ||
if (error != WIN32_ERROR.ERROR_SUCCESS) | ||
{ | ||
error.Throw(); | ||
} | ||
} | ||
} |
Oops, something went wrong.