From: Christian ASTOR on
On 18 jan, 11:08, "Noixe" <no...(a)TOGLIMIemail.it> wrote:

> I obtain only a lot of error a compile time. I have correct some of it. This
> source compile but get a bad result:

Compile it with VS, it works.
And I have tested the resultant exe on several stations (XP)...
From: Noixe on
"Christian ASTOR" <castorix(a)club-internet.fr> ha scritto:

> Compile it with VS, it works.
> And I have tested the resultant exe on several stations (XP)...

I have C++ builder and gcc.

I don't understand why I obtain only strange value. Is it possible that the
cause is compiler?

For example, the function about RAM work correctly, but function about CPU
no.

From: Noixe on
Miracle!

This work correctly for ram and cpu, but only on gcc.

If adapt this code for C++ Builder, work only ram beacuse
CPUTotalTimeDiff.QuadPart is always 0.
Do you know the reason?

#include <iostream>
#include <windows.h>
#include <ntsecapi.h>



typedef enum _SYSTEM_INFORMATION_CLASS {
SystemBasicInformation,
SystemProcessorInformation,
SystemPerformanceInformation,
SystemTimeOfDayInformation,
SystemPathInformation,
SystemProcessInformation,
SystemCallCountInformation,
SystemDeviceInformation,
SystemProcessorPerformanceInformation,
SystemFlagsInformation,
SystemCallTimeInformation,
SystemModuleInformation,
SystemLocksInformation,
SystemStackTraceInformation,
SystemPagedPoolInformation,
SystemNonPagedPoolInformation,
SystemHandleInformation,
SystemObjectInformation,
SystemPageFileInformation,
SystemVdmInstemulInformation,
SystemVdmBopInformation,
SystemFileCacheInformation,
SystemPoolTagInformation,
SystemInterruptInformation,
SystemDpcBehaviorInformation,
SystemFullMemoryInformation,
SystemLoadGdiDriverInformation,
SystemUnloadGdiDriverInformation,
SystemTimeAdjustmentInformation,
SystemSummaryMemoryInformation,
SystemNextEventIdInformation,
SystemEventIdsInformation,
SystemCrashDumpInformation,
SystemExceptionInformation,
SystemCrashDumpStateInformation,
SystemKernelDebuggerInformation,
SystemContextSwitchInformation,
SystemRegistryQuotaInformation,
SystemExtendServiceTableInformation,
SystemPrioritySeperation,
SystemPlugPlayBusInformation,
SystemDockInformation,
SystemPowerInformation_,
SystemProcessorSpeedInformation,
SystemCurrentTimeZoneInformation,
SystemLookasideInformation
} SYSTEM_INFORMATION_CLASS;

typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
LARGE_INTEGER IdleTime;
LARGE_INTEGER KernelTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER Reserved1[2];
ULONG Reserved2;
} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;



typedef NTSTATUS (WINAPI *
PFN_NTQUERYSYSTEMINFORMATION)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG,
PULONG);
HINSTANCE hNtDll = LoadLibraryW(L"ntdll.dll");
PFN_NTQUERYSYSTEMINFORMATION NTQSI =
PFN_NTQUERYSYSTEMINFORMATION(GetProcAddress(hNtDll,
"NtQuerySystemInformation"));

SYSTEM_INFORMATION_CLASS SystemInformationClass =
SystemProcessorPerformanceInformation;

SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION ProcPerfInfo[4];


int main() {

SYSTEM_INFO SysInfo;
GetSystemInfo(&SysInfo);

MEMORYSTATUS MemInfo;
GlobalMemoryStatus(&MemInfo);

const int NumberOfProcessors = SysInfo.dwNumberOfProcessors;
ULONG SystemInformationLength = NumberOfProcessors *
sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);

LARGE_INTEGER CPUIdleTimeOld[MAXIMUM_PROCESSORS],
CPUTotalTimeOld[MAXIMUM_PROCESSORS],
CPUKernelTimeOld[MAXIMUM_PROCESSORS],
CPUIdleTime[MAXIMUM_PROCESSORS],
CPUTotalTime[MAXIMUM_PROCESSORS],
CPUKernelTime[MAXIMUM_PROCESSORS];

while (true) {

NTQSI(SystemInformationClass, ProcPerfInfo, SystemInformationLength,
0);
LARGE_INTEGER CPUIdleTimeDiff = {0, 0};
LARGE_INTEGER CPUTotalTimeDiff = {0, 0};
LARGE_INTEGER CPUKernelTimeDiff = {0, 0};

for (int i = 0; i < NumberOfProcessors; i++) {

CPUIdleTime[i].QuadPart = ProcPerfInfo[i].IdleTime.QuadPart;
CPUKernelTime[i].QuadPart = ProcPerfInfo[i].KernelTime.QuadPart -
ProcPerfInfo[i].IdleTime.QuadPart;
CPUTotalTime[i].QuadPart = ProcPerfInfo[i].KernelTime.QuadPart +
ProcPerfInfo[i].UserTime.QuadPart;

CPUIdleTimeDiff.QuadPart = CPUIdleTime[i].QuadPart -
CPUIdleTimeOld[i].QuadPart;
CPUKernelTimeDiff.QuadPart = CPUKernelTime[i].QuadPart -
CPUKernelTimeOld[i].QuadPart;
CPUTotalTimeDiff.QuadPart = CPUTotalTime[i].QuadPart -
CPUTotalTimeOld[i].QuadPart;

unsigned int CurrentCPU;

if (CPUTotalTimeDiff.QuadPart != 0)
CurrentCPU = static_cast<unsigned int>(100 -
((CPUIdleTimeDiff.QuadPart * 100) / CPUTotalTimeDiff.QuadPart));
else
CurrentCPU = 0;

std::cout << "CPU " << (i + 1) << ": " << CurrentCPU << "%" <<
std::endl;

CPUTotalTimeOld[i].QuadPart = CPUTotalTime[i].QuadPart;
CPUIdleTimeOld[i].QuadPart = CPUIdleTime[i].QuadPart;
CPUKernelTimeOld[i].QuadPart = CPUKernelTime[i].QuadPart;
};


std::cout << "RAM: " << MemInfo.dwMemoryLoad << "%" << std::endl;
std::cout << "MB RAM: " << (MemInfo.dwTotalPhys -
MemInfo.dwAvailPhys) / 1048576 << std::endl;
std::cout << std::endl;

Sleep(1000);
};

return 0;
}