public static extern NTSTATUS NtQueryDirectoryObject( SafeDirectoryObjectHandle DirectoryHandle, SafeHandle Buffer, uint Length, [MarshalAs(UnmanagedType.U1)] bool ReturnSingleEntry, [MarshalAs(UnmanagedType.U1)] bool RestartScan, ref uint Context, out uint ReturnLength);
public static IEnumerable <ObjectInformation> GetDirectoryEntries(SafeDirectoryObjectHandle directoryHandle) { List <ObjectInformation> infos = new List <ObjectInformation>(); BufferHelper.CachedInvoke((StringBuffer buffer) => { buffer.EnsureCharCapacity(1024); uint context = 0; uint returnLength; NTSTATUS status; do { status = Direct.NtQueryDirectoryObject( DirectoryHandle: directoryHandle, Buffer: buffer, Length: (uint)buffer.ByteCapacity, ReturnSingleEntry: false, RestartScan: false, Context: ref context, ReturnLength: out returnLength); if (status != NTSTATUS.STATUS_SUCCESS && status != NTSTATUS.STATUS_MORE_ENTRIES) { break; } CheckedReader reader = new CheckedReader(buffer); do { UNICODE_STRING name = reader.ReadStruct <UNICODE_STRING>(); if (name.Length == 0) { break; } UNICODE_STRING type = reader.ReadStruct <UNICODE_STRING>(); infos.Add(new ObjectInformation { Name = name.ToString(), TypeName = type.ToString() }); } while (true); } while (status == NTSTATUS.STATUS_MORE_ENTRIES); if (status != NTSTATUS.STATUS_SUCCESS) { throw ErrorHelper.GetIoExceptionForNTStatus(status); } }); return(infos.OrderBy(i => i.Name));; }
public static extern NTSTATUS NtOpenDirectoryObject( out SafeDirectoryObjectHandle DirectoryHandle, ACCESS_MASK DesiredAccess, ref OBJECT_ATTRIBUTES ObjectAttributes);