Sign in to follow this  
Followers 0
cypher175

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

6 posts in this topic

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

Share this post


Link to post
Share on other sites



Just dont want to depend on a service to get me the info that I want.. unless there is no other possible way of getting it..

Is there anyway to get the CPU of a Specified Process/PID without using the WMI..??

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

so using MWI is pretty much the simplest way then..??

Can any of you please tell me what the above code I posted is getting..?? is the the total SYSTEM CPU% Usage or something else..??

Share this post


Link to post
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

Share this post


Link to post
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
Sign in to follow this  
Followers 0