标 题: 主机性能监控之wmi 获取进程信息
作 者: itdef
链 接: http://www.cnblogs.com/itdef/p/3990499.html
欢迎转帖 请保持文本完整并注明出处
仅在《主机性能监控之wmi 获取系统信息及内存性能信息》 代码中添加一个函数
使用的结构体位为Win32_Process
class Win32_Process : CIM_Process
{
string Caption;
string CommandLine;
string CreationClassName;
datetime CreationDate;
string CSCreationClassName;
string CSName;
string Description;
string ExecutablePath;
uint16 ExecutionState;
string Handle;
uint32 HandleCount;
datetime InstallDate;
uint64 KernelModeTime;
uint32 MaximumWorkingSetSize;
uint32 MinimumWorkingSetSize;
string Name;
string OSCreationClassName;
string OSName;
uint64 OtherOperationCount;
uint64 OtherTransferCount;
uint32 PageFaults;
uint32 PageFileUsage;
uint32 ParentProcessId;
uint32 PeakPageFileUsage;
uint64 PeakVirtualSize;
uint32 PeakWorkingSetSize;
uint32 Priority;
uint64 PrivatePageCount;
uint32 ProcessId;
uint32 QuotaNonPagedPoolUsage;
uint32 QuotaPagedPoolUsage;
uint32 QuotaPeakNonPagedPoolUsage;
uint32 QuotaPeakPagedPoolUsage;
uint64 ReadOperationCount;
uint64 ReadTransferCount;
uint32 SessionId;
string Status;
datetime TerminationDate;
uint32 ThreadCount;
uint64 UserModeTime;
uint64 VirtualSize;
string WindowsVersion;
uint64 WorkingSetSize;
uint64 WriteOperationCount;
uint64 WriteTransferCount;
};
添加函数为
bool CMyWMI::QueryProcessInfo()
{
HRESULT hres; //定义COM调用的返回
IEnumWbemClassObject* pEnumerator = NULL;
bool bRet = false; try{
hres = pSvc_->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_Process"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator); if (FAILED(hres))
{
throw exception("ExecQuery() error.");
} while (pEnumerator)
{
IWbemClassObject *pclsObj;
ULONG uReturn = ; HRESULT hr = pEnumerator->Next(WBEM_INFINITE, ,
&pclsObj, &uReturn);
if( == uReturn)
{
break;
} GetInfo(L"Caption",pclsObj);
GetInfo(L"Handle",pclsObj);
GetInfo(L"ExecutablePath",pclsObj);
cout << endl; pclsObj->Release();
} }catch(exception& e)
{
cout << e.what() << endl;
if(pEnumerator != NULL)
{
pEnumerator->Release();
pEnumerator = NULL;
}
return bRet;
} if(pEnumerator != NULL)
{
pEnumerator->Release();
pEnumerator = NULL;
} bRet = true;
return bRet;
}
python代码
#!/usr/bin/env python
# -*- coding: cp936 -*- import wmi
import os
import sys
import platform
import time def process_info():
c = wmi.WMI()
for p in c.Win32_Process():
print p.ProcessId, p.Name , p.ExecutablePath def main():
process_info(); if __name__ == '__main__':
main()
效果:
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
0 System Idle Process None
4 System None
316 smss.exe None
448 csrss.exe None
520 wininit.exe None
548 csrss.exe None
596 services.exe None
604 lsass.exe None
612 lsm.exe None
640 winlogon.exe None
760 svchost.exe None
820 BaiduHips.exe None
852 BaiduSdSvc.exe None
932 BaiduAnSvc.exe None
1004 svchost.exe None
352 atiesrxx.exe None
1068 svchost.exe None
1232 svchost.exe None
1260 svchost.exe None