/// <summary> /// Get the ids for all processes that have a handle to this file system object. /// Does not include the current process. /// </summary> public unsafe static IEnumerable <UIntPtr> GetProcessIds(SafeFileHandle fileHandle) { return(BufferHelper.BufferInvoke((HeapBuffer buffer) => { NTStatus status = NTStatus.STATUS_INFO_LENGTH_MISMATCH; while (status == NTStatus.STATUS_INFO_LENGTH_MISMATCH) { status = Imports.NtQueryInformationFile(fileHandle, out IO_STATUS_BLOCK statusBlock, buffer.VoidPointer, (uint)buffer.ByteCapacity, FileInformationClass.FileProcessIdsUsingFileInformation); switch (status) { case NTStatus.STATUS_SUCCESS: break; case NTStatus.STATUS_INFO_LENGTH_MISMATCH: // Not a big enough buffer buffer.EnsureByteCapacity((ulong)statusBlock.Information); break; default: throw status.GetException(); } } return ((FILE_PROCESS_IDS_USING_FILE_INFORMATION *)buffer.VoidPointer)->ProcessIdList.ToArray(); })); }
private void GetData() { NTStatus status = Imports.NtQueryDirectoryFile( FileHandle: _directory, Event: IntPtr.Zero, ApcRoutine: null, ApcContext: IntPtr.Zero, IoStatusBlock: out IO_STATUS_BLOCK statusBlock, FileInformation: _buffer.VoidPointer, Length: (uint)_buffer.ByteCapacity, FileInformationClass: FileInformationClass.FileFullDirectoryInformation, ReturnSingleEntry: false, FileName: null, RestartScan: false); switch (status) { case NTStatus.STATUS_NO_MORE_FILES: NoMoreFiles(); return; case NTStatus.STATUS_SUCCESS: Debug.Assert(statusBlock.Information.ToInt64() != 0); break; default: throw status.GetException(); } }
public static void ThrowIfFailed(this NTStatus status, string path = null) { if (status.Failed()) { throw status.GetException(path); } }
public static void Throw(this NTStatus status) => throw status.GetException();