diff --git a/src/libraries/Common/src/Interop/Windows/NtDll/Interop.NtQueryDirectoryFile.cs b/src/libraries/Common/src/Interop/Windows/NtDll/Interop.NtQueryDirectoryFile.cs index fbbf8b967102c4..a282208fb7a026 100644 --- a/src/libraries/Common/src/Interop/Windows/NtDll/Interop.NtQueryDirectoryFile.cs +++ b/src/libraries/Common/src/Interop/Windows/NtDll/Interop.NtQueryDirectoryFile.cs @@ -17,7 +17,7 @@ public static unsafe partial int NtQueryDirectoryFile( IntPtr ApcRoutine, IntPtr ApcContext, IO_STATUS_BLOCK* IoStatusBlock, - IntPtr FileInformation, + void* FileInformation, uint Length, FILE_INFORMATION_CLASS FileInformationClass, BOOLEAN ReturnSingleEntry, diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEnumerator.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEnumerator.Windows.cs index 6adbe5732a6d07..6068334754ac9f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEnumerator.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEnumerator.Windows.cs @@ -33,7 +33,7 @@ public abstract unsafe partial class FileSystemEnumerator : CriticalFin private Interop.NtDll.FILE_FULL_DIR_INFORMATION* _entry; private TResult? _current; - private IntPtr _buffer; + private void* _buffer; private int _bufferLength; private IntPtr _directoryHandle; private string? _currentPath; @@ -86,17 +86,11 @@ internal FileSystemEnumerator(string directory, bool isNormalized, EnumerationOp private void Init() { - // We'll only suppress the media insertion prompt on the topmost directory as that is the - // most likely scenario and we don't want to take the perf hit for large enumerations. - // (We weren't consistent with how we handled this historically.) - using (default(DisableMediaInsertionPrompt)) - { - // We need to initialize the directory handle up front to ensure - // we immediately throw IO exceptions for missing directory/etc. - _directoryHandle = CreateDirectoryHandle(_rootDirectory); - if (_directoryHandle == IntPtr.Zero) - _lastEntryFound = true; - } + // We need to initialize the directory handle up front to ensure + // we immediately throw IO exceptions for missing directory/etc. + _directoryHandle = CreateDirectoryHandle(_rootDirectory); + if (_directoryHandle == IntPtr.Zero) + _lastEntryFound = true; _currentPath = _rootDirectory; @@ -107,9 +101,10 @@ private void Init() try { // NtQueryDirectoryFile needs its buffer to be 64bit aligned to work - // successfully with FileFullDirectoryInformation on ARM32. AllocHGlobal - // will return pointers aligned as such, new byte[] does not. - _buffer = Marshal.AllocHGlobal(_bufferLength); + // successfully with FileFullDirectoryInformation. + // From https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/ns-ntifs-_file_full_dir_information: + // "This structure must be aligned on a LONGLONG (8-byte) boundary." + _buffer = NativeMemory.AlignedAlloc((nuint)_bufferLength, sizeof(ulong)); } catch { @@ -381,12 +376,12 @@ private void InternalDispose(bool disposing) _pending = null; } - if (_buffer != default) + if (_buffer != null) { - Marshal.FreeHGlobal(_buffer); + NativeMemory.AlignedFree(_buffer); } - _buffer = default; + _buffer = null; } }