Skip to content

Commit

Permalink
remove win2000 workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik committed Aug 17, 2021
1 parent 48f028e commit fc4378f
Showing 1 changed file with 8 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,13 @@ internal static ProcessInfo[] GetProcessInfos(int? processIdFilter = null)
Debug.Assert(bufferSize > 8);
byte* alignedBufferPtr = (byte*)(((nint)bufferPtr + 7) & ~7);
int firstAlignedElementIndex = (int)(alignedBufferPtr - bufferPtr);
bufferSize = buffer.Length - firstAlignedElementIndex;

uint actualSize = 0;
uint status = Interop.NtDll.NtQuerySystemInformation(
Interop.NtDll.SystemProcessInformation,
alignedBufferPtr,
(uint)(buffer.Length - firstAlignedElementIndex),
(uint)bufferSize,
&actualSize);

if (status != Interop.NtDll.STATUS_INFO_LENGTH_MISMATCH)
Expand All @@ -315,12 +316,13 @@ internal static ProcessInfo[] GetProcessInfos(int? processIdFilter = null)
}

Debug.Assert(actualSize > 0 && actualSize <= bufferSize, $"Actual size reported by NtQuerySystemInformation was {actualSize} for a buffer of size={bufferSize}.");
MostRecentSize = GetNewBufferSize(bufferSize, (int)actualSize);
MostRecentSize = GetEstimatedBufferSize(actualSize);
// Parse the data block to get process information
return GetProcessInfos(buffer.AsSpan(firstAlignedElementIndex, (int)actualSize), processIdFilter);
}

bufferSize = GetNewBufferSize(bufferSize, (int)actualSize);
Debug.Assert(actualSize > bufferSize, $"Actual size reported by NtQuerySystemInformation was {actualSize} for a buffer of size={bufferSize}.");
bufferSize = GetEstimatedBufferSize(actualSize);
}
}
}
Expand All @@ -331,31 +333,9 @@ internal static ProcessInfo[] GetProcessInfos(int? processIdFilter = null)
}
}

private static int GetNewBufferSize(int existingBufferSize, int requiredSize)
{
int newSize;

if (requiredSize == 0)
{
// On some old OS like win2000, requiredSize will not be set if the buffer
// passed to NtQuerySystemInformation is not enough.
newSize = existingBufferSize * 2;
}
else
{
// allocating a few more kilo bytes just in case there are some new process
// kicked in since new call to NtQuerySystemInformation
newSize = requiredSize + 1024 * 10;
}

if (newSize < 0)
{
// In reality, we should never overflow.
// Adding the code here just in case it happens.
throw new OutOfMemoryException();
}
return newSize;
}
// allocating a few more kilo bytes just in case there are some new process
// kicked in since new call to NtQuerySystemInformation
private static int GetEstimatedBufferSize(uint actualSize) => (int)actualSize + 1024 * 10;

private static unsafe ProcessInfo[] GetProcessInfos(ReadOnlySpan<byte> data, int? processIdFilter)
{
Expand Down

0 comments on commit fc4378f

Please sign in to comment.