public static SmartPtr GetSystemHandleInformation() { int length = 0x10000; while (true) { using (SmartPtr ptr = new SmartPtr()) { ptr.Allocate(length); int returnLength; NtStatus ret = NativeMethods.NtQuerySystemInformation( SystemInformationClass. SystemHandleInformation, ptr.Pointer, length, out returnLength ); if (ret == NtStatus.InfoLengthMismatch) { length *= 2; if (length > 1024 * 1024 * 1024) { throw new OutOfMemoryException("NtQuerySystemInformation fails"); } } else if (ret == NtStatus.Success) { return(ptr.Clone()); } } } }