主机性能监控之wmi 获取进程信息

标 题: 主机性能监控之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;
};
View Code
复制代码

 

添加函数为

复制代码
 1 bool CMyWMI::QueryProcessInfo()
 2 {
 3     HRESULT hres; //定义COM调用的返回  
 4     IEnumWbemClassObject* pEnumerator = NULL;  
 5     bool bRet = false;
 6 
 7     try{
 8         hres = pSvc_->ExecQuery(  
 9             bstr_t("WQL"),     
10             bstr_t("SELECT * FROM Win32_Process"),  
11             WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,   
12             NULL,  
13             &pEnumerator);  
14 
15         if (FAILED(hres))  
16         {  
17             throw exception("ExecQuery() error.");
18         }  
19 
20         while (pEnumerator)  
21         {
22             IWbemClassObject *pclsObj;  
23             ULONG uReturn = 0;  
24 
25             HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,   
26                 &pclsObj, &uReturn);  
27             if(0 == uReturn)  
28             {  
29                 break;  
30             }  
31 
32             GetInfo(L"Caption",pclsObj);
33             GetInfo(L"Handle",pclsObj);
34             GetInfo(L"ExecutablePath",pclsObj);
35             cout << endl;
36 
37             pclsObj->Release(); 
38         }
39 
40 
41     }catch(exception& e)
42     {
43         cout << e.what() << endl;
44         if(pEnumerator != NULL)
45         {
46             pEnumerator->Release(); 
47             pEnumerator = NULL;
48         }
49         return bRet;
50     }
51 
52 
53     if(pEnumerator != NULL)
54     {
55         pEnumerator->Release(); 
56         pEnumerator = NULL;
57     }
58 
59     bRet = true;
60     return bRet;
61 }
View Code
复制代码

 

 

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() 
View Code
复制代码

效果:

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







posted on   itdef  阅读(2380)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示