【已解决】取单个进程的cpu占用率。
本帖最后由 shuaiw 于 2012-7-11 16:26 编辑在写一个脚本,希望取到和任务管理器上一样的某个进程的cpu占用率。很急,希望帮忙解决,谢谢{:face (319):} 搜索论坛pdh 本帖最后由 xms77 于 2012-6-6 20:42 编辑
以下是‘忘忧含幽’兄弟的代码,请参考借鉴,希望对你有用!
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#Include <Constants.au3>
#include <DateTimeConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <GuiListView.au3> ;列表控件
#include <GuiTreeView.au3> ;树控件
#include <GuiStatusBar.au3> ;任务栏
#include <ListViewConstants.au3>
#include <ProgressConstants.au3>
#include <Process.au3>;进程管理器必须
#include <SliderConstants.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#NoTrayIcon ;不显示托盘
Opt("GUIOnEventMode", 1) ;事件驱动模式开启
;
$strComputer = "." ;选定本机
$wbemServices = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2") ;连接到命名空间
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;任务管理器;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
$Process=ProcessList()
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Tab_Process = GUICtrlCreateTabItem("任务管理器")
Dim $Process_Task=1 ;任务名
Dim $Process_Img=1 ;进程映像名
Dim $Process_PID=1 ;进程PID
Dim $Process_User=1 ;进程启动用户
Dim $Process_Mem=1 ;进程内存
Dim $Process_CPU_Time=1 ;进程CPU时间
Dim $Process_CPU_size=1 ;进程CPU百分比
Dim $Process_Level=1 ;进程级别
Dim $Process_Path=1 ;进程路径
Dim $Process_Module=1 ;进程所调用模块
Dim $Process_SVC=1 ;进程中的服务
Dim $Process_Status=1 ;任务状态,正在运行|没有响应
Dim $Process_Item
;$Process_List = Process_UpdateList()
$Process_List = GUICtrlCreateListView("任务|映像名称|PID|用户名|内存(KB)|CPU(%)|优先级|路径", 8, 32, 609, 353)
GUICtrlSetImage($Process_List, "shell32.dll",22,0);设置进程前的图标
$Pro_Menu=GUICtrlCreateContextMenu($Process_List) ;在进程列表窗口创建右键菜单
$Pro_Menu11= GUICtrlCreateMenuItem("刷新列表", $Pro_Menu)
$Pro_Menu12= GUICtrlCreateMenuItem("结束进程", $Pro_Menu)
$Pro_Menu13= GUICtrlCreateMenuItem("暂停进程", $Pro_Menu)
$Pro_Menu14= GUICtrlCreateMenuItem("删除文件(谨慎使用!)", $Pro_Menu)
$Pro_Menu15=GUICtrlCreateMenu("设置优先级",$Pro_Menu) ;在进程列表窗口创建右键子菜单,设置优先级
$Pro_Menu151= GUICtrlCreateMenuItem("实时", $Pro_Menu15)
$Pro_Menu152= GUICtrlCreateMenuItem("高", $Pro_Menu15)
$Pro_Menu153= GUICtrlCreateMenuItem("高于标准", $Pro_Menu15)
$Pro_Menu154= GUICtrlCreateMenuItem("标准", $Pro_Menu15)
$Pro_Menu155= GUICtrlCreateMenuItem("低于标准", $Pro_Menu15)
$Pro_Menu156= GUICtrlCreateMenuItem("低", $Pro_Menu15)
$Pro_Menuu_f1= GUICtrlCreateMenuItem("", $Pro_Menu)
$Pro_Menu21= GUICtrlCreateMenuItem("Google进程", $Pro_Menu)
$Pro_Menu22= GUICtrlCreateMenuItem("查看属性", $Pro_Menu)
$Pro_Menu23= GUICtrlCreateMenuItem("定位文件", $Pro_Menu)
$Pro_Menu24= GUICtrlCreateMenuItem("复制进程名", $Pro_Menu)
$Pro_Menu25= GUICtrlCreateMenuItem("复制进程路径", $Pro_Menu)
$Pro_Menuu_f2= GUICtrlCreateMenuItem("", $Pro_Menu)
$Pro_Menu31= GUICtrlCreateMenuItem("切换至", $Pro_Menu)
$Pro_Menu32= GUICtrlCreateMenuItem("前置窗口", $Pro_Menu)
$Pro_Menu33= GUICtrlCreateMenuItem("最小化窗口", $Pro_Menu)
$Pro_Menu34= GUICtrlCreateMenuItem("最大化窗口", $Pro_Menu)
$Pro_Menu_f3= GUICtrlCreateMenuItem("", $Pro_Menu) ;右键菜单上的分隔符
$Pro_Menu41= GUICtrlCreateMenuItem("文件属性", $Pro_Menu)
$Pro_Menu42= GUICtrlCreateMenuItem("文件版本", $Pro_Menu)
$Process_UpdateList = GUICtrlCreateButton("刷新列表", 16, 389, 73, 25, 0)
$Process_SaveList = GUICtrlCreateButton("保存列表", 104, 389, 73, 25, 0)
$Process_Kill = GUICtrlCreateButton("结束进程", 200, 389, 73, 25, 0)
$Process_Query = GUICtrlCreateButton("查询进程", 312, 389, 73, 25, 0)
$Process_Print = GUICtrlCreateButton("截图", 520, 389, 57, 25, 0)
$Process_Copy = GUICtrlCreateButton("复制路径", 424, 389, 73, 25, 0)
GUICtrlSetBkColor(-1, 0x3A6EA5)
$colProcesses = $wbemServices.ExecQuery ("Select * from Win32_Process") ;获取进程相关信息集合
For $objProcess in $colProcesses
$Process_Img=$objProcess.Name ;进程映像名
$Process_PID=$objProcess.ProcessID
$Process_Mem=$objProcess.WorkingSetSize
$Process_Mem=$Process_Mem/1024
;$Process_Task=$objProcess. ;任务名
;$objProcess.ThreadCount为user对象
;$objProcess.CommandLine为启动参数
$Process_User=1 ;进程启动用户
$Process_CPU_Time=1 ;进程CPU时间
$Process_CPU_size=1 ;进程CPU百分比
$Process_Level=1 ;进程级别
$Process_Path=$objProcess.ExecutablePath ;进程路径
$Process_Module=1 ;进程所调用模块
$Process_SVC=1;进程中的服务
$Process_Item=GUICtrlCreateListViewItem( $Process_Task&"|"&$Process_Img&"|"&$Process_PID&"|"&$Process_User&"|"&$Process_Mem&"|"&$Process_CPU_Time&"|"&$Process_Level&"|"&$Process_Path,$Process_List)
next
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;回复 1# shuaiw 这段代码是执行不了哦,我连接到命名空间会报错 可是我用这个取的cpu是不多的啊 好长啊 看的都糊涂了 还有什么办法可以取到cpu占用率哦,要和任务管理器上的一样哦
唉 ;working only with explore.exe and its subprocesses!!!
#include <WindowsConstants.au3>
Opt('MustDeclareVars', 1)
Opt("TrayIconHide", 1)
HotKeySet("{ESC}", "_Exit")
Global Const $Process_All_Access = 0x1F0FFF
Global $CreateTime = DllStructCreate("dword;dword")
Global $ExitTime = DllStructCreate("dword;dword")
Global $KernelTime = DllStructCreate("dword;dword")
Global $UserTime = DllStructCreate("dword;dword")
Global $FileTime = DllStructCreate("dword;dword")
Global $SystemTime = DllStructCreate("ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort")
Global $IdleTime = DllStructCreate("dword;dword")
Global $sUserTime, $sKernelTime, $eUserTime, $eKernelTime
Global $ret, $hour, $minutes, $seconds, $milliseconds
Global $ProcHandle, $Process_CPU_Usage
Global $PID, $CPUTime, $mem, $prg
Global $logical_cpus = CPU()
Global $hProc
Global $GUI, $info
$prg = "calc.exe"
Run($prg)
Sleep(500)
$PID = ProcessExists($prg)
If $PID = 0 Then
ConsoleWrite(@CRLF & "ERROR! Process " & $prg & " not found! Aborting..." & @CRLF)
Exit
EndIf
Global $iMemo, $hDebugGUI
$GUI = GUICreate("Process Information v0.30 Beta by UEZ - Press ESC to quit!", 740, 25, -1, -1, BitOR($WS_CAPTION,$WS_POPUP,$WS_BORDER,$WS_CLIPSIBLINGS))
$iMemo = GUICtrlCreateEdit("", -1, -1, 820)
GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
GUICtrlSetBkColor($iMemo, 0xFFFFFF)
GUISetState()
$ProcHandle = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", $Process_All_Access, "int", False, "dword", $PID)
$ProcHandle = $ProcHandle
;~ ConsoleWrite(@CRLF & @CRLF & "Process Information v0.30 Beta by UEZ" & @CRLF)
;~ ConsoleWrite("=====================================" & @CRLF & @CRLF)
;~ ConsoleWrite("(Press ESC to exit)" & @CRLF)
While 1
$CPUTime = _GetProcTime($ProcHandle)
$mem = _ProcessGetMem($ProcHandle) / 1024
$hour = Int($CPUTime / 10000000 / 3600)
$minutes = Int($CPUTime / 10000000 / 60)
If $minutes < 10 Then $minutes = "0" & $minutes
$seconds = Round((Int($CPUTime / 10000000) / 60 - Int($CPUTime / 10000000 / 60)) * 60, 0)
If $seconds < 10 Then $seconds = "0" & $seconds
$milliseconds = Round((Int($CPUTime / 10000) / 1000 - Int(Int($CPUTime / 10000) / 1000)) * 1000, 0)
If $milliseconds < 10 Then $milliseconds = "00" & $milliseconds
If $milliseconds > 9 And $milliseconds < 100 Then $milliseconds = "0" & $milliseconds
$info = @CR & $prg & " | PID: " & $PID & " | CPU time: " & $hour & ":" & $minutes & ":" & $seconds & "." & $milliseconds & " (h:m:s.ms) | Mem usage: " & $mem & " KB | CPU usage: " & $Process_CPU_Usage & " % " & @CR
MemoWrite($info)
;~ ConsoleWrite($prg & " | PID: " & $PID & " | CPU time: " & $hour & ":" & $minutes & ":" & $seconds & "." & $milliseconds & " (h:m:s.ms) | Mem usage: " & $mem & " KB | CPU usage: " & $Process_CPU_Usage & " % " & @CR)
Sleep(1000)
If Not ProcessExists($PID) Then _Exit()
WEnd
Func MemoWrite($sMessage = "")
GUICtrlSetData($iMemo, $sMessage)
EndFunc ;==>MemoWrite
Func _GetProcTime($ProcessHandle)
$ret = DllCall("kernel32.dll", "int", "GetProcessTimes", "int", $ProcessHandle, "ptr", DllStructGetPtr($CreateTime), "ptr", DllStructGetPtr($ExitTime), "ptr", DllStructGetPtr($KernelTime), "ptr", DllStructGetPtr($UserTime))
If $ret = 0 Then
ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessTimes call" & @CRLF)
SetError(1, 0, $ret)
EndIf
$sKernelTime = DllStructGetData($KernelTime, 1)
$sUserTime = DllStructGetData($UserTime, 1)
$Process_CPU_Usage = Floor(($sKernelTime - $eKernelTime + $sUserTime - $eUserTime) / 100000 / $logical_cpus)
If $Process_CPU_Usage > 100 Then $Process_CPU_Usage = "100"
$eKernelTime = $sKernelTime
$eUserTime = $sUserTime
Return $sUserTime + $sKernelTime
EndFunc ;==>_GetProcTime
Func _ProcessGetMem($ProcessHandle) ;get physical memory of the process -> http://msdn.microsoft.com/en-us/library/ms683219%28VS.85%29.aspx
Local $structPROCESS_MEMORY_COUNTERS, $structPROCESS_MEMORY_COUNTERS_EX, $nSize, $aRet
If @OSVersion <> "WIN_7" Then
$structPROCESS_MEMORY_COUNTERS = DllStructCreate("dword cb; dword PageFaultCount; uint PeakWorkingSetSize; uint WorkingSetSize; " & _
"uint QuotaPeakPagedPoolUsage; uint QuotaPagedPoolUsage; uint QuotaPeakNonPagedPoolUsage; " & _
"uint QuotaNonPagePoolUsage; uint PagefileUsage; uint PeakPagefileUsage") ;http://msdn.microsoft.com/en-us/library/ms684877%28VS.85%29.aspx
$nSize = DllStructGetSize($structPROCESS_MEMORY_COUNTERS)
$aRet = DllCall("psapi.dll", "int", "GetProcessMemoryInfo", "hwnd", $ProcessHandle, "ptr", DllStructGetPtr($structPROCESS_MEMORY_COUNTERS), "dword", $nSize) ;call GetProcessMemoryInfo
If $aRet = 0 Then
ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessMemoryInfo call" & @LF)
SetError(1, 0, $aRet)
EndIf
Return DllStructGetData($structPROCESS_MEMORY_COUNTERS, "WorkingSetSize")
Else
$structPROCESS_MEMORY_COUNTERS_EX = DllStructCreate("dword cb; dword PageFaultCount; uint PeakWorkingSetSize; uint WorkingSetSize; " & _
"uint QuotaPeakPagedPoolUsage; uint QuotaPagedPoolUsage; uint QuotaPeakNonPagedPoolUsage; " & _
"uint QuotaNonPagePoolUsage; uint PagefileUsage; uint PeakPagefileUsage; " & _
"uint PrivateUsage") ;http://msdn.microsoft.com/en-us/library/ms684877%28VS.85%29.aspx
$nSize = DllStructGetSize($structPROCESS_MEMORY_COUNTERS_EX)
$aRet = DllCall("Kernel32.dll", "int", "K32GetProcessMemoryInfo", "hwnd", $ProcessHandle, "ptr", DllStructGetPtr($structPROCESS_MEMORY_COUNTERS_EX), "dword", $nSize) ;call GetProcessMemoryInfo
If $aRet = 0 Then
ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessMemoryInfo call" & @LF)
SetError(1, 0, $aRet)
EndIf
;~ConsoleWrite("WorkingSetSize: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "WorkingSetSize") / 1024, 0) & @CRLF & _
;~"PrivateUsage: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "PrivateUsage") / 1024, 0) & @CRLF & @CRLF)
Return DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "PrivateUsage")
EndIf
EndFunc ;==>_ProcessGetMem
Func CPU() ;get logical CPU(s)
Local $i, $j, $x, $os, $colItems, $HW_Processor_Description, $HW_Processor_Manufacturer, $HW_Processor_MaxClockSpeed, $HW_Processor_Name, $HW_Processor_SocketDesignation
Local $system = "Localhost"
Local $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $system & "\root\cimv2")
$colItems = $objWMIService.ExecQuery("SELECT Caption from Win32_OperatingSystem", "WQL", 0x30) ;get OS version
If IsObj($colItems) Then
For $objItem In $colItems
$os = $objItem.Caption
Next
EndIf
If StringInStr($os, "2003") > 0 Or StringInStr($os, "2000") > 0 Or StringInStr(StringUpper($os), "XP") > 0 Then
$colItems = $objWMIService.ExecQuery("Select Name, Description, Manufacturer, MaxClockSpeed, SocketDesignation from Win32_Processor", "WQL", 0x30)
Else
$colItems = $objWMIService.ExecQuery("Select Name, Description, Manufacturer, MaxClockSpeed, SocketDesignation, NumberOfCores, NumberOfLogicalProcessors from Win32_Processor", "WQL", 0x30)
EndIf
If IsObj($colItems) Then
$i = 0
$j = 0
$HW_Processor_SocketDesignation = ""
For $objItem In $colItems ;get amount of logical CPUs
If StringInStr(StringLower($HW_Processor_SocketDesignation), StringLower($objItem.SocketDesignation)) = 0 Then
$HW_Processor_SocketDesignation &= $objItem.SocketDesignation & ", "
EndIf
$i = $i + 1
Next
If StringInStr($os, "2003") > 0 Or StringInStr($os, "2000") > 0 Or StringInStr(StringUpper($os), "XP") > 0 Then
$x = StringSplit(Remove_Last_Comma($HW_Processor_SocketDesignation), ",")
$j = UBound($x) - 1
Else
$i = $objItem.NumberOfCores
$j = $objItem.NumberOfLogicalProcessors / $i
EndIf
If $j > $i Then $j = $i
EndIf
Return ($i)
EndFunc ;==>CPU
Func Remove_Last_Comma($str)
If StringRight($str, 2) = ", " Then $str = StringMid($str, 1, StringLen($str) - 2)
Return $str
EndFunc ;==>Remove_Last_Comma
Func _Exit()
DllCall("kernel32.dll", "int", "CloseHandle", "int", $ProcHandle)
DllCall("psapi.dll", "int", "CloseHandle", "hwnd", $ProcHandle)
ProcessClose($pid)
Exit
EndFunc ;==>_Exit 回复 3# xms77
cputime直接是赋值的。。。恕我愚钝,没看出来这个是怎么获取某进程的cpu时间百分比的,求指教 解决后的代码贴上来瞧瞧
页:
[1]