I'm currently working on a kind of surveillance tool. It's basically like the taskmanager, and I'm just doing this because I want to get in touch with C++ and learn new stuff.
The core of the CPU-usage part is GetSystemTimes(). This function returns the pointers to 3 values, the time the CPU has been idle, the time the CPU has been in kernel mode, and the time the CPU has been in user mode. I call the function twice with 250ms sleep in between, and calculate the percentage with the differences of the values.
I have two problems, though. The function returns pointers to FILETIME structures, but I need the actual value as an integer, float, double, or similar, because I need to calculate (int would be enough for me, but I don't know how large the values are). I know that a pointer tells me where the data is saved, but I don't know how I can actually get that data. And how can I get from FILETIME to something else, once I've got it.
#include <iostream>
#define _WIN32_WINNT 0x0602
#include <windows.h>
#include <stdlib.h>
class Processor{};
class Usage: public Processor
{
public:
int now()
{
FILETIME a0, a1, a2, b0, b1, b2;
GetSystemTimes(&a0, &a1, &a2);
SleepEx(250, false);
GetSystemTimes(&b0, &b1, &b2);
// attempt to get the actual value instead of the pointer and convert it to float/double/int
float idle0 = a0;
float idle1 = b0;
float kernel0 = a1;
float kernel1 = b1;
float user0 = a2;
float user1 = b2;
float idl = idle1 - idle0;
float ker = kernel0 - kernel1;
float usr = user0 - user1;
float cpu = (ker - idl + usr) * 100 / (ker + usr);
return cpu;
}
};
int main()
{
using namespace std;
Usage Usage;
for(int i = 0; i < 10; i++)
{
cout << "CPU:\t" << Usage.now() << endl;
}
cout << "\nFinished!\nPress any key to exit!\n";
cin.clear();
cin.get();
return 0;
}
Thanks for the help! :)