Note(Not my code): SOURCE Bottom of page made by Sayyed Mohsen Zahraee. I made some minor changes in my post but his original code works right off the bat.
While trying to see how this code works, I'm also trying to find a way to have the final output, when meeting a certain parameter through an if statement, trigger an action.
#include <iostream>
#include <windows.h>
#include <stdio.h>
void GetSystemTimesAddress();
CHAR cpuusage();
int main(void)
{
int n;
GetSystemTimesAddress();
for(n=0;n<20;n++)
{
printf("CPU Usage: %3d%%\r",cpuusage());
Sleep(2000);
if ((printf("%3d%%\r",cpuusage())) = '10')//Doesn't work but goal action
{
std::cout << "just fine";
}
if ((printf("%3d%%\r",cpuusage())) = '30')
{
std::cout << "keep an eye on";
}
if ((printf("%3d%%\r",cpuusage())) = '50')
{
std::cout << "getting warm";
}
if ((printf("%3d%%\r",cpuusage())) = '70')
{
std::cout << "pretty dangerous";
}
}
printf("\n");
return 0;
}
//Bellow CPU usage meathods
CHAR cpuusage(void);
typedef BOOL ( __stdcall * pfnGetSystemTimes)( LPFILETIME lpIdleTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime );
static pfnGetSystemTimes s_pfnGetSystemTimes = NULL;
static HMODULE s_hKernel = NULL;
void GetSystemTimesAddress()
{
if( s_hKernel == NULL )
{
s_hKernel = LoadLibrary( L"Kernel32.dll" );
if( s_hKernel != NULL )
{
s_pfnGetSystemTimes = (pfnGetSystemTimes)GetProcAddress( s_hKernel, "GetSystemTimes" );
if( s_pfnGetSystemTimes == NULL )
{
FreeLibrary( s_hKernel ); s_hKernel = NULL;
}
}
}
}
// cpuusage(void)
// Return a CHAR value in the range 0 - 100 representing actual CPU usage in percent.
CHAR cpuusage()
{
FILETIME ft_sys_idle;
FILETIME ft_sys_kernel;
FILETIME ft_sys_user;
ULARGE_INTEGER ul_sys_idle;
ULARGE_INTEGER ul_sys_kernel;
ULARGE_INTEGER ul_sys_user;
static ULARGE_INTEGER ul_sys_idle_old;
static ULARGE_INTEGER ul_sys_kernel_old;
static ULARGE_INTEGER ul_sys_user_old;
CHAR usage = 0;
// we cannot directly use GetSystemTimes on C language
/* add this line :: pfnGetSystemTimes */
s_pfnGetSystemTimes(&ft_sys_idle, /* System idle time */
&ft_sys_kernel, /* system kernel time */
&ft_sys_user); /* System user time */
CopyMemory(&ul_sys_idle , &ft_sys_idle , sizeof(FILETIME)); // Could been optimized away...
CopyMemory(&ul_sys_kernel, &ft_sys_kernel, sizeof(FILETIME)); // Could been optimized away...
CopyMemory(&ul_sys_user , &ft_sys_user , sizeof(FILETIME)); // Could been optimized away...
usage =
(
(
(
(
(ul_sys_kernel.QuadPart - ul_sys_kernel_old.QuadPart)+
(ul_sys_user.QuadPart - ul_sys_user_old.QuadPart)
)
-
(ul_sys_idle.QuadPart-ul_sys_idle_old.QuadPart)
)
*
(100)
)
/
(
(ul_sys_kernel.QuadPart - ul_sys_kernel_old.QuadPart)+
(ul_sys_user.QuadPart - ul_sys_user_old.QuadPart)
)
);
ul_sys_idle_old.QuadPart = ul_sys_idle.QuadPart;
ul_sys_user_old.QuadPart = ul_sys_user.QuadPart;
ul_sys_kernel_old.QuadPart = ul_sys_kernel.QuadPart;
return usage;
}
If someone finds a way to make the if staments work, could you give a brief explanation as to how it does. I understand something like this is difficult to explain in a simple paragraph or two, but even something simple would be a great learning opportunity. Thank You!