Загрузка памяти и процессора

Пример получения загрузки процессора. Показывает по отдельности загрузку каждого ядра. Так же пример показывает размер общей и свободной памяти компьютера и процент загрузки памяти . Функция GlobalMemoryStatus не может получить настоящее значение памяти если  2 гб < размер ОЗУ < 4 гб  Но процент загрузки выводит верно.

Платформа: Windows
Автор: Станислав Будинов

#INCLUDE "windows.bi"
#INCLIB "ntdll"

Declare Function NtQuerySystemInformation Alias "NtQuerySystemInformation"( _
SystemInformationClass As DWORD , _
SystemInformation  As Any Ptr , _
SystemInformationLength As DWORD , _
ReturnLength As DWORD )  As Integer

#DEFINE SystemProcessorPerformanceInformation 8

Type SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
    IdleTime As LARGE_INTEGER
    KernelTime As LARGE_INTEGER
    UserTime As LARGE_INTEGER
    DpcTime As LARGE_INTEGER
    InterruptTime As LARGE_INTEGER
    InterruptCount As Ulong
End Type

Sub GetCpuInfo()
    
    Dim As SYSTEM_INFO t_Systeminfo
    Dim As Ulong ulBuf
    Dim As SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION t_Spri_old(32)
    Dim As SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION t_Spri(32)
    Dim As MEMORYSTATUS t_Mstatus

    GetSystemInfo(@t_Systeminfo)
    Print "Count core processor = ";t_Systeminfo.dwNumberOfProcessors
    
    NTQuerySystemInformation( _
    SystemProcessorPerformanceInformation, _
    @t_Spri_old(0), _
    Sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * t_Systeminfo.dwNumberOfProcessors, _
    @ulBuf )
    
    Sleep(500)
    
    NTQuerySystemInformation( _
    SystemProcessorPerformanceInformation, _
    @t_Spri(0), _
    Sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * t_Systeminfo.dwNumberOfProcessors, _
    @ulBuf )
    
    For iCountCpu As Integer= 0 To t_Systeminfo.dwNumberOfProcessors-1
        
        Dim As Byte bUseCpu 
        
        bUseCpu = Cast(Byte, 100 - ( _
        ((t_Spri(iCountCpu).IdleTime.QuadPart - t_Spri_old(iCountCpu).IdleTime.QuadPart) * 100) / _
        ((t_Spri(iCountCpu).KernelTime.QuadPart +  t_Spri(iCountCpu).UserTime.QuadPart) - _
        (t_Spri_old(iCountCpu).KernelTime.QuadPart + t_Spri_old(iCountCpu).UserTime.QuadPart))))
        
        Print "CPU " & iCountCpu & " = " & bUseCpu & "%"
        
    Next
    
    t_Mstatus.dwLength=SizeOf(MemoryStatus)
    GlobalMemoryStatus(@t_Mstatus)
    
    Print !"\nAll memory: " & t_Mstatus.dwTotalPhys/1024/1024 & " mb
    Print "Free memory: " & t_Mstatus.dwAvailPhys/1024/1024 & " mb
    Print "percent of memory in use: " & t_Mstatus.dwMemoryLoad & " %
    
End Sub

Do 
    Cls
    GetCpuInfo()
    Sleep(1000)
Loop Until GetAsyncKeyState(&h1B)<0