Newsgroup: it.comp.programmare.win32
Titolo: Rilevare l'utilizzo delle risorse

Salve,
vorrei rilevare la percentuale d'uso dei core del mio processore ed anche 
l'utilizzo della ram.
Ci sono API apposta per tale scopo, oppure C++ Builder offre delle funzioni 
ad hoc?
Saluti 

Ho trovato qualcosa su PDHQuery, ma non ho capito come usarla, ci sono 
troppe funzioni.
Qualcuno conosce queste API? 

Se qualcuno fosse interessato ecco il sorgente che calcola la percentuale 
d'uso di ogni core della cpu e della ram.
Per� gira solo su gcc. Se lo adatto a C++ Builder rileva correttamente solo 
la ram perch� CPUTotalTimeDiff.QuadPart � sempre 0.
Sapreste dirmi il motivo?
#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;
}