The loop is doubling the size of the buffer parameter and calling the NtQuerySystemInformation method again. The loop depends on the call to succeed and assumes that a failure is the result of the ntStatus being STATUS_FLT_BUFFER_TOO_SMALL (0x801C0001) You have a couple of choices for exiting your loop.
- exit if the buffer reaches a certain size.
- exit if ntStatus is not STATUS_SUCCESS (0x00000000) or STATUS_FLT_BUFFER_TOO_SMALL (0x801C0001)
Also Microsoft advises NOT using the NtQuerySystemInformation method as it might change from Windows version to Windows version. The details and alternative methods can be found at NtQuerySystemInformation function