Add dynamic handle information buffer lengths
@@ -5433,7 +5433,38 @@ NTSTATUS PhEnumHandlesEx(
NTSTATUS status;
PVOID buffer;
ULONG bufferSize;
+ ULONG returnLength = 0;
+ ULONG attempts = 0;
+
+ bufferSize = initialBufferSize;
+ buffer = PhAllocate(bufferSize);
+
+ status = NtQuerySystemInformation(
+ SystemExtendedHandleInformation,
+ buffer,
+ bufferSize,
+ &returnLength
+ );
+
+ while (status == STATUS_INFO_LENGTH_MISMATCH && attempts < 10)
+ {
+ PhFree(buffer);
+ bufferSize = returnLength;
+ buffer = PhAllocate(bufferSize);
+ status = NtQuerySystemInformation(
+ SystemExtendedHandleInformation,
+ buffer,
+ bufferSize,
+ &returnLength
+ );
+
+ attempts++;
+ }
+
+ if (!NT_SUCCESS(status))
+ {
+ // Fall back to using the previous code that we've used since Windows XP (dmex)
bufferSize = initialBufferSize;
buffer = PhAllocate(bufferSize);
@@ -5453,6 +5484,7 @@ NTSTATUS PhEnumHandlesEx(
buffer = PhAllocate(bufferSize);
}
+ }
if (!NT_SUCCESS(status))
{
