為何不用wmi呢?因為執行很慢,為啥不用winreg?因為winreg在批量獲取及遍歷服務方面很不方便,於是采用這方法
該方法同命令行下的wmic執行
獲取服務信息
#coding=utf8 from win32com.client import GetObject mywmi = GetObject("winmgmts:") objs = mywmi.InstancesOf("Win32_Service") for obj in objs: print obj.PathName
獲取進程信息
#coding=utf8
from win32com.client import GetObject mywmi = GetObject("winmgmts:") objs = mywmi.InstancesOf("Win32_Process") for obj in objs: #print obj.executablepath print obj.CommandLine
執行代碼的靈感來源於以下這段文字:
VB使用Wmi獲取系統當前進程詳細信息/列表,VB使用Wmi獲取系統當前進程信息/列表,VB使用Wmi獲取系統進程信息/列表,VB使用Wmi獲取當前進程信息/列表,VB使用Wmi獲取進程信息/列表,VB Wmi 獲取當前進程信息/列表,VB Wmi 系統進程詳細信息/列表,VB Wmi 進程列表,VB Wmi 進程信息,VB Wmi 進程,VB Wmi,GetObject("winmgmts://./root/cimv2:win32_process").instances,winmgmts://./root/cimv2:win32_process,VB winmgmts://./root/cimv2,VB win32_process,VB GetObject,進程名稱:Wmi.Name,進程路徑:Wmi.executablepath,線程數量:Wmi.ThreadCount,PID:Wmi.ProcessID,父PID:Wmi.ParentProcessID,優先級:Wmi.Priority,命令行:Wmi.CommandLine,占用內存:Wmi.WorkingSetSize / 1024 & "K",用戶名稱:Wmi.ExecMethod_("GetOwner").User,啟動時間:Wmi.CreationDate,另外一種獲取PID函數:Wmi.Handle。 VB使用Wmi獲取系統當前進程詳細信息: Dim Wmi, Jic As String For Each Wmi In GetObject("winmgmts://./root/cimv2:win32_process").instances_ '循環進程 Jic = Jic & " " & _ "進程:" & Wmi.Name & " " & _ "路徑:" & Wmi.executablepath & " " & _ "線程:" & Wmi.ThreadCount & " " & _ "PID:" & Wmi.ProcessID & " " & _ "父PID:" & Wmi.ParentProcessID & " " & _ "優先級:" & Wmi.Priority & " " & _ "命令行:" & Wmi.CommandLine & " " & _ "內存:" & Wmi.WorkingSetSize / 1024 & "K" & " " & _ "用戶:" & Wmi.ExecMethod_("GetOwner").User & " " & _ "啟動時間:" & Wmi.CreationDate & " " & vbCrLf '"PID:" & Wmi.Handle '另外一種獲取PID函數 Next MsgBox Jic VB "winmgmts://./root/CIMV2"對象的詳細用法: ·GetOwner() 取得進程用戶名。用法:objGetOwner(變量名) Terminate() 結束進程。用法:obj.Terminate() 屬性: ProcessID 進程的PID。 ThreadCount 進程的線程數 ExecutablePath 進程的完整路徑
服務狀態 State
可用參數如下
[Dynamic, Provider("CIMWin32"), SupportsUpdate, UUID("{8502C4D9-5FBB-11D2-AAC1-006008C78BC7}"), DisplayName("Services"), AMENDMENT]
class Win32_Service : Win32_BaseService
{
boolean AcceptPause;
boolean AcceptStop;
string Caption;
uint32 CheckPoint;
string CreationClassName;
boolean DelayedAutoStart;
string Description;
boolean DesktopInteract;
string DisplayName;
string ErrorControl;
uint32 ExitCode;
datetime InstallDate;
string Name;
string PathName;
uint32 ProcessId;
uint32 ServiceSpecificExitCode;
string ServiceType;
boolean Started;
string StartMode;
string StartName;
string State;
string Status;
string SystemCreationClassName;
string SystemName;
uint32 TagId;
uint32 WaitHint;
};
參見msdn:https://docs.microsoft.com/zh-cn/windows/desktop/CIMWin32Prov/win32-service
