diff --git a/PInvoke/NtDll/NtDll.cs b/PInvoke/NtDll/NtDll.cs index dd671775..63c79a91 100644 --- a/PInvoke/NtDll/NtDll.cs +++ b/PInvoke/NtDll/NtDll.cs @@ -1,11 +1,6 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using Vanara.Extensions; using Vanara.Extensions.Reflection; -using Vanara.InteropServices; namespace Vanara.PInvoke; @@ -1571,19 +1566,19 @@ public static partial class NtDll /// /// The requested SystemInformationClass does not match the return type requested. or Reported size of object does not match query. /// - public static T NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass) + public static T? NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass) { var tType = typeof(T); if (CorrespondingTypeAttribute.GetCorrespondingTypes(SystemInformationClass).Any() && !CorrespondingTypeAttribute.CanGet(SystemInformationClass, tType)) throw new InvalidCastException("The requested SystemInformationClass does not match the return type requested."); var status = NtQuerySystemInformation(SystemInformationClass, SafeHGlobalHandle.Null, 0, out var len); - if (status != NTStatus.STATUS_INFO_LENGTH_MISMATCH && status != NTStatus.STATUS_BUFFER_OVERFLOW && status != NTStatus.STATUS_BUFFER_TOO_SMALL) - throw status.GetException(); + if ((int)status is not NTStatus.STATUS_INFO_LENGTH_MISMATCH and not NTStatus.STATUS_BUFFER_OVERFLOW and not NTStatus.STATUS_BUFFER_TOO_SMALL) + throw status.GetException()!; var mem = new SafeHGlobalHandle(SystemInformationClass == SYSTEM_INFORMATION_CLASS.SystemProcessInformation ? (int)len * 2 : (int)len); NtQuerySystemInformation(SystemInformationClass, mem, (uint)mem.Size, out len).ThrowIfFailed(); if (tType.IsArray) { - var aType = tType.GetElementType(); + var aType = tType.GetElementType()!; if (aType == typeof(SYSTEM_PROCESS_INFORMATION) && SystemInformationClass == SYSTEM_INFORMATION_CLASS.SystemProcessInformation) { var retList = new List(); @@ -1599,7 +1594,7 @@ public static partial class NtDll } var cnt = Math.DivRem(len, Marshal.SizeOf(aType), out var res); if (res != 0) throw new InvalidCastException("Reported size of object does not match query."); - return (T)mem.InvokeGenericMethod("ToArray", new[] { aType }, new[] { typeof(int), typeof(int) }, new object[] { (int)cnt, 0 }); + return (T?)mem.InvokeGenericMethod("ToArray", new[] { aType }, new[] { typeof(int), typeof(int) }, new object[] { (int)cnt, 0 }); } return mem.ToStructure(); } @@ -1615,10 +1610,10 @@ public static partial class NtDll public static IList> NtQuerySystemInformation_Process() { var status = NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS.SystemProcessInformation, SafeHGlobalHandle.Null, 0, out var len); - if (status != NTStatus.STATUS_INFO_LENGTH_MISMATCH && status != NTStatus.STATUS_BUFFER_OVERFLOW && status != NTStatus.STATUS_BUFFER_TOO_SMALL) - throw status.GetException(); + if ((int)status is not NTStatus.STATUS_INFO_LENGTH_MISMATCH and not NTStatus.STATUS_BUFFER_OVERFLOW and not NTStatus.STATUS_BUFFER_TOO_SMALL) + throw status.GetException()!; var mem = new SafeHGlobalHandle((int)len * 2); - NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS.SystemProcessInformation, mem, (uint)mem.Size, out len).ThrowIfFailed(); + NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS.SystemProcessInformation, mem, (uint)mem.Size, out _).ThrowIfFailed(); var retList = new List>(); var pi = new SYSTEM_PROCESS_INFORMATION(); var ptr = mem.DangerousGetHandle(); @@ -1626,7 +1621,7 @@ public static partial class NtDll do { pi = ptr.ToStructure(); - retList.Add(new Tuple(pi, ptr.Offset(pSz).ToArray((int)pi.NumberOfThreads))); + retList.Add(new Tuple(pi, ptr.Offset(pSz).ToArray((int)pi.NumberOfThreads)!)); ptr = ptr.Offset(pi.NextEntryOffset); } while (pi.NextEntryOffset > 0); return retList; diff --git a/PInvoke/NtDll/Ntifs.cs b/PInvoke/NtDll/Ntifs.cs index 708d063a..82fa1f5d 100644 --- a/PInvoke/NtDll/Ntifs.cs +++ b/PInvoke/NtDll/Ntifs.cs @@ -1,6 +1,4 @@ -using System; -using System.IO; -using System.Runtime.InteropServices; +using System.IO; using static Vanara.PInvoke.Kernel32; namespace Vanara.PInvoke; @@ -869,7 +867,7 @@ public static partial class NtDll // POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock, PLARGE_INTEGER AllocationSize, uint FileAttributes, uint // ShareAccess, uint CreateDisposition, uint CreateOptions, IntPtr EaBuffer, uint EaLength); public static extern NTStatus NtCreateFile(out SafeHFILE FileHandle, ACCESS_MASK DesiredAccess, in OBJECT_ATTRIBUTES ObjectAttributes, out IO_STATUS_BLOCK IoStatusBlock, - in long AllocationSize, FileFlagsAndAttributes FileAttributes, FileShare ShareAccess, NtFileMode CreateDisposition, NtFileCreateOptions CreateOptions, [In, Optional] IntPtr EaBuffer, uint EaLength); + in long AllocationSize, FileFlagsAndAttributes FileAttributes, FileShare ShareAccess, NtFileMode CreateDisposition, NtFileCreateOptions CreateOptions, IntPtr EaBuffer = default, uint EaLength = 0); /// /// The NtCreateFile routine creates a new file or opens an existing file. @@ -1349,7 +1347,7 @@ public static partial class NtDll // ShareAccess, uint CreateDisposition, uint CreateOptions, IntPtr EaBuffer, uint EaLength); public static extern NTStatus NtCreateFile(out SafeHFILE FileHandle, ACCESS_MASK DesiredAccess, in OBJECT_ATTRIBUTES ObjectAttributes, out IO_STATUS_BLOCK IoStatusBlock, [In, Optional] IntPtr AllocationSize, FileFlagsAndAttributes FileAttributes, FileShare ShareAccess, NtFileMode CreateDisposition, NtFileCreateOptions CreateOptions, - [In, Optional] IntPtr EaBuffer, uint EaLength); + IntPtr EaBuffer = default, uint EaLength = 0); /// /// The NtCreateSection routine creates a section object. @@ -1848,7 +1846,7 @@ public static partial class NtDll // Flags, PVOID HeapBase, SIZE_T ReserveSize, SIZE_T CommitSize, PVOID Lock, PRTL_HEAP_PARAMETERS Parameters ); [DllImport(Lib.NtDll, SetLastError = false, ExactSpelling = true)] [PInvokeData("ntifs.h", MSDNShortId = "NF:ntifs.RtlCreateHeap")] - public static extern IntPtr RtlCreateHeap(HeapFlags Flags, [In, Optional] IntPtr HeapBase, [In, Optional] SizeT ReserveSize, + public static extern IntPtr RtlCreateHeap([In, Optional] HeapFlags Flags, [In, Optional] IntPtr HeapBase, [In, Optional] SizeT ReserveSize, [In, Optional] SizeT CommitSize, [In, Optional] IntPtr Lock, [In, Optional] IntPtr Parameters); /// @@ -2001,7 +1999,7 @@ public static partial class NtDll // Flags, PVOID HeapBase, SIZE_T ReserveSize, SIZE_T CommitSize, PVOID Lock, PRTL_HEAP_PARAMETERS Parameters ); [DllImport(Lib.NtDll, SetLastError = false, ExactSpelling = true)] [PInvokeData("ntifs.h", MSDNShortId = "NF:ntifs.RtlCreateHeap")] - public static extern IntPtr RtlCreateHeap(HeapFlags Flags, [In, Optional] IntPtr HeapBase, [In, Optional] SizeT ReserveSize, + public static extern IntPtr RtlCreateHeap([In, Optional] HeapFlags Flags, [In, Optional] IntPtr HeapBase, [In, Optional] SizeT ReserveSize, [In, Optional] SizeT CommitSize, [In, Optional] IntPtr Lock, in RTL_HEAP_PARAMETERS Parameters); /// @@ -2110,7 +2108,7 @@ public static partial class NtDll /// If HeapBase is NULL, CommitRoutine must also be NULL. /// [MarshalAs(UnmanagedType.FunctionPtr)] - public PRTL_HEAP_COMMIT_ROUTINE CommitRoutine; + public PRTL_HEAP_COMMIT_ROUTINE? CommitRoutine; /// Reserved for system use. Drivers must set this parameter to zero. private SizeT Reserved1; diff --git a/PInvoke/NtDll/Wdm.cs b/PInvoke/NtDll/Wdm.cs index 7905e099..41413a38 100644 --- a/PInvoke/NtDll/Wdm.cs +++ b/PInvoke/NtDll/Wdm.cs @@ -1,15 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using Vanara.Extensions; -using Vanara.Extensions.Reflection; -using Vanara.InteropServices; -using static Vanara.PInvoke.Kernel32; - -namespace Vanara.PInvoke; +namespace Vanara.PInvoke; /// Platform invokable enumerated types, constants and functions from ntdll.h public static partial class NtDll @@ -900,7 +889,7 @@ public static partial class NtDll [DllImport(Lib.NtDll, SetLastError = false, ExactSpelling = true)] [PInvokeData("wdm.h", MSDNShortId = "5ffd8262-10b3-4c40-bd3e-050271338508")] public static extern NTStatus NtCreateEnlistment(out SafeEnlistmentHandle EnlistmentHandle, ACCESS_MASK DesiredAccess, [In] SafeResourceManagerHandle ResourceManagerHandle, - [In] SafeTransactionHandle TransactionHandle, in OBJECT_ATTRIBUTES ObjectAttributes, [Optional] uint CreateOptions, NOTIFICATION_MASK NotificationMask, [In, Optional] IntPtr EnlistmentKey); + [In] SafeTransactionHandle TransactionHandle, in OBJECT_ATTRIBUTES ObjectAttributes, [Optional] uint CreateOptions, [In, Optional] NOTIFICATION_MASK NotificationMask, [In, Optional] IntPtr EnlistmentKey); /// /// The ZwCreateEnlistment routine creates a new enlistment object for a transaction. @@ -1103,7 +1092,7 @@ public static partial class NtDll [DllImport(Lib.NtDll, SetLastError = false, ExactSpelling = true)] [PInvokeData("wdm.h", MSDNShortId = "5ffd8262-10b3-4c40-bd3e-050271338508")] public static extern NTStatus NtCreateEnlistment(out SafeEnlistmentHandle EnlistmentHandle, ACCESS_MASK DesiredAccess, [In] SafeResourceManagerHandle ResourceManagerHandle, - [In] SafeTransactionHandle TransactionHandle, [In, Optional] IntPtr ObjectAttributes, [Optional] uint CreateOptions, NOTIFICATION_MASK NotificationMask, [In, Optional] IntPtr EnlistmentKey); + [In] SafeTransactionHandle TransactionHandle, [In, Optional] IntPtr ObjectAttributes, [Optional] uint CreateOptions, [In, Optional] NOTIFICATION_MASK NotificationMask, [In, Optional] IntPtr EnlistmentKey); /// /// The ZwCreateResourceManager routine creates a resource manager object. @@ -1476,7 +1465,7 @@ public static partial class NtDll // RmGuid, POBJECT_ATTRIBUTES ObjectAttributes, ULONG CreateOptions, PUNICODE_STRING Description ); [DllImport(Lib.NtDll, SetLastError = false, ExactSpelling = true)] [PInvokeData("wdm.h", MSDNShortId = "4812eeb4-134f-4ecb-870b-dbab04c1137b")] - public static extern NTStatus NtCreateResourceManager(out SafeResourceManagerHandle ResourceManagerHandle, ACCESS_MASK DesiredAccess, SafeTransactionManagerHandle TmHandle, in Guid RmGuid, + public static extern NTStatus NtCreateResourceManager(out SafeResourceManagerHandle ResourceManagerHandle, ACCESS_MASK DesiredAccess, SafeTransactionManagerHandle TmHandle, [In, Optional] IntPtr RmGuid, [In, Optional] IntPtr ObjectAttributes, [Optional] uint CreateOptions, [In, Optional, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UnicodeStringMarshaler))] string? Description); /// diff --git a/PInvoke/NtDll/Winternl.UnicodeString.cs b/PInvoke/NtDll/Winternl.UnicodeString.cs index a671debc..6a38fc38 100644 --- a/PInvoke/NtDll/Winternl.UnicodeString.cs +++ b/PInvoke/NtDll/Winternl.UnicodeString.cs @@ -1,8 +1,4 @@ -using System; -using System.Runtime.InteropServices; -using Vanara.Extensions; -using Vanara.InteropServices; -using static Vanara.PInvoke.Kernel32; +using static Vanara.PInvoke.Kernel32; namespace Vanara.PInvoke; @@ -92,7 +88,7 @@ public static partial class NtDll public string ToString(HPROCESS hProc) { using var mem = new SafeCoTaskMemString(MaximumLength); - return ReadProcessMemory(hProc, Buffer, mem, mem.Size, out _) ? mem : string.Empty; + return ReadProcessMemory(hProc, Buffer, mem, mem.Size, out _) ? (string)mem! : string.Empty; } } @@ -129,7 +125,7 @@ public static partial class NtDll public string ToString(HPROCESS hProc) { using var mem = new SafeCoTaskMemString(MaximumLength); - return NtWow64ReadVirtualMemory64(hProc, Buffer, ((IntPtr)mem).ToInt32(), mem.Size, out _).Succeeded ? mem : string.Empty; + return NtWow64ReadVirtualMemory64(hProc, Buffer, ((IntPtr)mem).ToInt32(), mem.Size, out _).Succeeded ? (string)mem! : string.Empty; } } @@ -144,13 +140,13 @@ public static partial class NtDll /// Initializes a new instance of the class with a string value. /// The string value. - public SafeUNICODE_STRING(string value) : base(IntPtr.Zero, true) + public SafeUNICODE_STRING(string? value) : base(IntPtr.Zero, true) { var structLen = GetStructSize(GetCurrentProcess()); if (string.IsNullOrEmpty(value)) SetHandle(Marshal.AllocCoTaskMem(size = structLen)); else - SetHandle(InitMemForString(value, structLen, out size)); + SetHandle(InitMemForString(value!, structLen, out size)); } internal SafeUNICODE_STRING(IntPtr ptr, bool own) : base(ptr, own) @@ -173,19 +169,19 @@ public static partial class NtDll /// Performs an implicit conversion from to . /// The string value. /// The resulting instance from the conversion. - public static implicit operator SafeUNICODE_STRING(string value) => new(value); + public static implicit operator SafeUNICODE_STRING(string? value) => new(value); /// Performs an implicit conversion from to . /// The value. /// The resulting instance from the conversion. - public static implicit operator string(SafeUNICODE_STRING value) => value?.ToString(default); + public static implicit operator string?(SafeUNICODE_STRING value) => value?.ToString(default); /// Extracts the string value from this structure by reading process specific memory. /// The process handle of the process from which to read the memory. /// A that has the value. public string ToString(HPROCESS hProc) { - if (IsInvalid) return null; + if (IsInvalid) return string.Empty; var maxlen = unchecked((ushort)Marshal.ReadInt16(handle, 2)); hProc = hProc == default ? GetCurrentProcess() : hProc; var bufOffset = GetBufferOffset(hProc); @@ -194,8 +190,8 @@ public static partial class NtDll return StringHelper.GetString(bufPtr, CharSet.Unicode, MaximumLength) ?? string.Empty; using var mem = new SafeCoTaskMemString(maxlen); if (hProc.IsWow64()) - return NtWow64ReadVirtualMemory64(hProc, bufPtr.ToInt64(), ((IntPtr)mem).ToInt32(), mem.Size, out _).Succeeded ? mem : string.Empty; - return ReadProcessMemory(hProc, bufPtr, mem, mem.Size, out _) ? mem : string.Empty; + return NtWow64ReadVirtualMemory64(hProc, bufPtr.ToInt64(), ((IntPtr)mem).ToInt32(), mem.Size, out _).Succeeded ? (string)mem! : string.Empty; + return ReadProcessMemory(hProc, bufPtr, mem, mem.Size, out _) ? (string)mem! : string.Empty; } /// Extracts the string value from this structure by reading process specific memory. diff --git a/PInvoke/NtDll/Winternl.cs b/PInvoke/NtDll/Winternl.cs index 1add6d06..8c791762 100644 --- a/PInvoke/NtDll/Winternl.cs +++ b/PInvoke/NtDll/Winternl.cs @@ -1,7 +1,4 @@ -using System; -using System.Linq; -using System.Runtime.InteropServices; -using Vanara.InteropServices; +using System.Linq; namespace Vanara.PInvoke; @@ -548,7 +545,7 @@ public static partial class NtDll NtQueryInformationProcess(ProcessHandle, ProcessInformationClass, mem, mem.Size, out _).ThrowIfFailed(); return mem; - bool TypeIsWow() => typeof(T).Name.EndsWith("WOW64"); + static bool TypeIsWow() => typeof(T).Name.EndsWith("WOW64"); } /// A call to NtQueryInformationProcess for the supplied process requires WOW64 structs. diff --git a/PInvoke/Shared/FieldValueHash.cs b/PInvoke/Shared/FieldValueHash.cs index 15f8fd01..6ebc0bbf 100644 --- a/PInvoke/Shared/FieldValueHash.cs +++ b/PInvoke/Shared/FieldValueHash.cs @@ -34,7 +34,7 @@ public class StaticFieldValueHash /// The type of the enum to added. /// The optional library name. public static void AddFields(string? lib = null) where TFieldType : struct, IComparable => - AddFields(Enum.GetValues(typeof(TEnum)).Cast().Select(v => (v, Enum.GetName(typeof(TEnum), v))), lib); + AddFields(Enum.GetValues(typeof(TEnum)).Cast().Select(v => (v, Enum.GetName(typeof(TEnum), v)!)), lib); /// Tries to get the name of a value's library. /// The type of the type. diff --git a/UnitTests/PInvoke/NtDll/NtDllTests.cs b/UnitTests/PInvoke/NtDll/NtDllTests.cs index 97404cb7..7d3dea8f 100644 --- a/UnitTests/PInvoke/NtDll/NtDllTests.cs +++ b/UnitTests/PInvoke/NtDll/NtDllTests.cs @@ -1,11 +1,5 @@ using NUnit.Framework; -using System; -using System.Collections.Generic; using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; -using Vanara.InteropServices; using static Vanara.PInvoke.NtDll; namespace Vanara.PInvoke.Tests; @@ -23,11 +17,11 @@ public partial class NtDllTests var qi = NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS.SystemRegistryQuotaInformation); Assert.That(qi.RegistryQuotaUsed, Is.Not.Zero); var ppi = NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS.SystemProcessorPerformanceInformation); - Assert.That(ppi.Length, Is.EqualTo(bi.NumberOfProcessors)); + Assert.That(ppi?.Length, Is.EqualTo(bi.NumberOfProcessors)); var arr = NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS.SystemProcessInformation); var pti = NtQuerySystemInformation_Process(); - Assert.That(arr.Length, Is.EqualTo(pti.Count)); + Assert.That(arr?.Length, Is.EqualTo(pti.Count)); TestContext.WriteLine($"{bi.NumberOfProcessors} Cores; {pti.Count} Processes; {pti.Sum(t => t.Item2.Length)} Threads"); } @@ -36,11 +30,11 @@ public partial class NtDllTests public void SafeUNICODE_STRING_Test() { const string testStr = "Testing. 1. 2. 3."; - SafeUNICODE_STRING sstr = null; + SafeUNICODE_STRING? sstr = null; try { Assert.That(() => sstr = testStr, Throws.Nothing); - Assert.That((string)sstr, Is.EqualTo(testStr)); + Assert.That((string?)sstr!, Is.EqualTo(testStr)); } finally { diff --git a/UnitTests/PInvoke/NtDll/WinternlTests.cs b/UnitTests/PInvoke/NtDll/WinternlTests.cs index 53aa45ac..056aff9d 100644 --- a/UnitTests/PInvoke/NtDll/WinternlTests.cs +++ b/UnitTests/PInvoke/NtDll/WinternlTests.cs @@ -1,10 +1,4 @@ using NUnit.Framework; -using NUnit.Framework.Constraints; -using System; -using System.Diagnostics; -using System.Linq; -using Vanara.Extensions; -using Vanara.InteropServices; using static Vanara.PInvoke.NtDll; namespace Vanara.PInvoke.Tests; @@ -36,27 +30,27 @@ public partial class WinternlTests TestContext.WriteLine($"Img: {upp.ImagePathName.ToString(hProc)}; CmdLine: {upp.CommandLine.ToString(hProc)}"); } - NtQueryResult pdp = null; + NtQueryResult? pdp = null; Assert.That(() => pdp = NtQueryInformationProcess(hProc, PROCESSINFOCLASS.ProcessDebugPort), Throws.Nothing); Assert.That(pdp, ResultIs.ValidHandle); - TestContext.WriteLine($"DbgPort: {pdp.Value.ToInt64()}"); + TestContext.WriteLine($"DbgPort: {pdp?.Value.ToInt64()}"); - NtQueryResult pwi = null; + NtQueryResult? pwi = null; Assert.That(() => pwi = NtQueryInformationProcess(hProc, PROCESSINFOCLASS.ProcessWow64Information), Throws.Nothing); Assert.That(pwi, ResultIs.ValidHandle); - Assert.That(pwi.Value.Value, Is.True); + Assert.That(pwi?.Value.Value, Is.True); - NtQueryResult pfn = null; + NtQueryResult? pfn = null; Assert.That(() => pfn = NtQueryInformationProcess(hProc, PROCESSINFOCLASS.ProcessImageFileName), Throws.Nothing); Assert.That(pfn, ResultIs.ValidHandle); - TestContext.WriteLine($"Fn: {pfn.Value.ToString(hProc)}"); + TestContext.WriteLine($"Fn: {pfn?.Value.ToString(hProc)}"); - NtQueryResult pbt = null; + NtQueryResult? pbt = null; Assert.That(() => pbt = NtQueryInformationProcess(hProc, PROCESSINFOCLASS.ProcessBreakOnTermination), Throws.Nothing); Assert.That(pbt, ResultIs.ValidHandle); - Assert.That(pbt.Value.Value, Is.False); + Assert.That(pbt?.Value.Value, Is.False); - NtQueryResult psi = null; + NtQueryResult? psi = null; // This is documented, but fails on Win10 Assert.That(() => psi = NtQueryInformationProcess(hProc, PROCESSINFOCLASS.ProcessSubsystemInformation), Throws.ArgumentException); //Assert.That(psi, ResultIs.ValidHandle); @@ -64,9 +58,9 @@ public partial class WinternlTests //TestContext.WriteLine($"SubSys: {psi.Value}"); // Try undocumented fetch - NtQueryResult ppb = null; + NtQueryResult? ppb = null; Assert.That(() => ppb = NtQueryInformationProcess(hProc, PROCESSINFOCLASS.ProcessPriorityBoost), Throws.Nothing); - TestContext.WriteLine($"Priority boost: {ppb.Value}"); + TestContext.WriteLine($"Priority boost: {ppb?.Value}"); } [Test]