Jump to content

Getting The CPU% of a Specified Process/PID..?


Recommended Posts

I was searching for some Code/UDF to give me the ( CPU% ) of a Specified Process/PID, and came across a few UDF's but most of them seem to use the WMI in their script..

I came across this thread and saw this code that seems to get the total CPU of the System I thnk without using the WMI..

http://www.autoitscript.com/forum/index.ph...mp;#entry269843

So I was wondering if there would be anyway to get the CPU% of a Specified Process/PID somehow using the code below, I dont want to use the WMI in my script.. So would anything like this be possible at all..??

Func CurrentCPU($init = 0)
    Global $liOldIdleTime = 0
    Global $liOldSystemTime = 0
    Local $SYS_BASIC_INFO = 0
    Local $SYS_PERFORMANCE_INFO = 2
    Local $SYS_TIME_INFO = 3
    
    $SYSTEM_BASIC_INFORMATION = DllStructCreate("int;uint;uint;uint;uint;uint;uint;ptr;ptr;uint;byte;byte;short")
    $status = DllCall("ntdll.dll", "int", "NtQuerySystemInformation", "int", $SYS_BASIC_INFO, _
            "ptr", DllStructGetPtr($SYSTEM_BASIC_INFORMATION), _
            "int", DllStructGetSize($SYSTEM_BASIC_INFORMATION), _
            "int", 0)
    
    If $status[0]Then Return -1
    
    While 1
        $SYSTEM_PERFORMANCE_INFORMATION = DllStructCreate("int64;int[76]")
        $SYSTEM_TIME_INFORMATION = DllStructCreate("int64;int64;int64;uint;int")
        
        $status = DllCall("ntdll.dll", "int", "NtQuerySystemInformation", "int", $SYS_TIME_INFO, _
                "ptr", DllStructGetPtr($SYSTEM_TIME_INFORMATION), _
                "int", DllStructGetSize($SYSTEM_TIME_INFORMATION), _
                "int", 0)
        
        If $status[0]Then Return -2
        
        $status = DllCall("ntdll.dll", "int", "NtQuerySystemInformation", "int", $SYS_PERFORMANCE_INFO, _
                "ptr", DllStructGetPtr($SYSTEM_PERFORMANCE_INFORMATION), _
                "int", DllStructGetSize($SYSTEM_PERFORMANCE_INFORMATION), _
                "int", 0)
        
        If $status[0]Then Return -3
        
        If $init = 1 Or $liOldIdleTime = 0 Then
            $liOldIdleTime = DllStructGetData($SYSTEM_PERFORMANCE_INFORMATION, 1)
            $liOldSystemTime = DllStructGetData($SYSTEM_TIME_INFORMATION, 2)
            Sleep(1000)
            If $init = 1 Then Return -99
        Else
            $dbIdleTime = DllStructGetData($SYSTEM_PERFORMANCE_INFORMATION, 1) - $liOldIdleTime
            $dbSystemTime = DllStructGetData($SYSTEM_TIME_INFORMATION, 2) - $liOldSystemTime
            $liOldIdleTime = DllStructGetData($SYSTEM_PERFORMANCE_INFORMATION, 1)
            $liOldSystemTime = DllStructGetData($SYSTEM_TIME_INFORMATION, 2)
            
            $dbIdleTime = $dbIdleTime / $dbSystemTime
            
            $dbIdleTime = 100.0 - $dbIdleTime * 100.0 / DllStructGetData($SYSTEM_BASIC_INFORMATION, 11) + 0.5
            Return $dbIdleTime
        EndIf
        $SYSTEM_PERFORMANCE_INFORMATION = 0
        $SYSTEM_TIME_INFORMATION = 0
    WEnd
EndFunc   ;==>CurrentCPU
Link to comment
Share on other sites

I think, you have to work with Performance Counters, but this is difficult :)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

The code in Post #1 returns the total CPU usage like the graph in teh Taskmanager.

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...