• Welcome to Theos PowerBasic Museum 2017.

Getting CPU% usage with Performance Data Helper (PDH.dll)

Started by Bud Meyer, November 17, 2008, 12:29:39 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Bud Meyer

I'm having trouble getting PDH to return a meaningful value. For example, I wish to get the current CPU usage, similar to what Task Manager reports. But the result is always "1242396". Why isn't it returning a value from 0-100%?


Here's a slightly modified version of José's example code, using the v1.08 headers.

#COMPILE EXE
#DIM ALL
#INCLUDE "PDH.INC"

FUNCTION PBMAIN () AS LONG
    LOCAL hr AS LONG
    LOCAL pdhStatus AS LONG
    LOCAL hQuery AS DWORD
    LOCAL szCounterPath AS ASCIIZ * %PDH_MAX_COUNTER_PATH
    LOCAL hCounter AS DWORD
    LOCAL pdhFmtValue AS PDH_FMT_COUNTERVALUE
    LOCAL szBuffer AS ASCIIZ * 256

    TRY
        ' Open query --------------------------------------------------------------
        pdhStatus = PdhOpenQuery(BYVAL %NULL, 0, hQuery)
        IF pdhStatus <> %ERROR_SUCCESS THEN
           MSGBOX "PdhOpenQuery - Error: " & HEX$(pdhStatus)
           ERROR 5
        END IF

        ' Add counter -------------------------------------------------------------
        ' Note: The counter path is a localized string.
        szCounterPath = "\Processor(_Total)\% Processor Time"
        pdhStatus = PdhAddCounter(hQuery, szCounterPath, 0, hCounter)
        IF pdhStatus <> %ERROR_SUCCESS THEN
           MSGBOX "PdhAddCounter - Error: " & HEX$(pdhStatus)
           ERROR 5
        END IF

        ' Get the data ------------------------------------------------------------
        pdhStatus = PdhCollectQueryData(hQuery)
        IF pdhStatus <> %ERROR_SUCCESS THEN
           MSGBOX "PdhCollectQueryData - Error: " & HEX$(pdhStatus)
           ERROR 5
        END IF

SLEEP 1000 'pause a moment between data samples

        ' Get the data ------------------------------------------------------------
        pdhStatus = PdhCollectQueryData(hQuery)
        IF pdhStatus <> %ERROR_SUCCESS THEN
           MSGBOX "PdhCollectQueryData - Error: " & HEX$(pdhStatus)
           ERROR 5
        END IF

        ' Format counter value ----------------------------------------------------
        pdhStatus = PdhGetFormattedCounterValue(hCounter, %PDH_FMT_LONG OR %PDH_FMT_NOSCALE, BYVAL %NULL, pdhFmtValue)
        IF pdhStatus <> %ERROR_SUCCESS THEN
           MSGBOX "PdhFetFormattedCounterValue - Error: " & HEX$(pdhStatus)
           ERROR 5
        END IF
        MSGBOX FORMAT$(pdhFmtValue.longValue)

    CATCH

    FINALLY
       IF hQuery THEN PdhCloseQuery(hQuery)

    END TRY

END FUNCTION


Any help would be appreciated. :)

José Roca

 
The PDH_FMT_COUNTERVALUE_UNION structure in PDH.INC must be QWORD aligned.

Change


UNION PDH_FMT_COUNTERVALUE_UNION
   longValue       AS LONG
   doubleValue     AS DOUBLE
   largeValue      AS QUAD
   AnsiStringValue AS ASCIIZ PTR
   WideStringValue AS WORD PTR
END UNION


to


UNION PDH_FMT_COUNTERVALUE_UNION QWORD
   longValue       AS LONG
   doubleValue     AS DOUBLE
   largeValue      AS QUAD
   AnsiStringValue AS ASCIIZ PTR
   WideStringValue AS WORD PTR
END UNION


Bud Meyer

Thanks. I had a feeling it was going to be something like that.  ;D

The example in the first post has been updated to take two samples instead of one (which is apparently required for certain data so we can get the rate of change).