-2

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!

Community
  • 1
  • 1
Laina
  • 29
  • 1
  • 7
  • Your if statements aren't doing what you think they are doing. You need to look into comparing the value returned by `cpuusage()` rather than trying to print it and compare the return value of `printf` – GWW Aug 28 '14 at 18:46
  • Ok I see. Rewrote as {if (cpuusage() < 10)} and got an output, but it's location seemed to have 'broke it' while running. When run, the string "Just fine" is replaced where "CPU Usage:" is located and not underneath. I'll keep experimenting. – Laina Aug 28 '14 at 18:53

1 Answers1

1

You are assigning when you mean to be comparing. This is a common problem here on the Stack. You're also attempting to compare whatever is returned from printf. Furthermore, you're attmepting to make a character out of two characters. '10' is invalid. It could be a C-String, "10", but more than likely you want an int. Change the if's from:

if ((printf("%3d%%\r",cpuusage())) = '10')

To

if(cpuusage() == 10)
scohe001
  • 15,110
  • 2
  • 31
  • 51
  • Made the changes, getting good results, and fixed the location of the output. For some reason, I've been dismissing the fact that cpuusage() is an ever changing value, and IF I'm right, to have the if statement run, it causes the changing value to freeze in order to trigger the output, making the whole program break during run time. If this is the problem, what should I do to allow the cpuusage() value to continue changing even after a statement has been triggered? Should I be looking in the area of a loop? – Laina Aug 28 '14 at 19:15
  • @Laina How was `cpuusage()` called in the code where you got it from? – scohe001 Aug 28 '14 at 20:09
  • That is a tricky one. For CHAR cpuusage(), after looking into FILETIME, ULARGE_INTEGER, and QuadPart, I think at the end of the function it makes a space in memory with 64bits open for the numbers 0-100. The cpuusage(void) meathod on the other hand is another story. Same as the other method, after googling, Parts of it make some sence but once it goes to pfnGetSystemTimes and LoadLibrary("Kernl32.dll") I get lost. GetSystemTimes is explained on Microsofts site but pfnGetSystemTimes confuses me. – Laina Aug 28 '14 at 21:41