标 题: 主机性能监控之wmi 获取系统信息及内存性能信息
作 者: itdef
链 接: http://www.cnblogs.com/itdef/p/3990240.html

欢迎转帖 请保持文本完整并注明出处

这里参考了http://www.cnblogs.com/lxcsmallcity/archive/2009/10/11/1580803.html

使用了PYTHON 和 vc 进行了调用WMI的代码编写

通过搜索和查看MSDN 可以找到WMI的基本用法

其实主要是WMI接口的初始化 使用 释放的过程

然后就是查找MSDN各个Win32_OperatingSystem  Win32_Process等的结构的说明 进行相关信息的获取

这里先介绍Win32_OperatingSystem的用法

Win32_OperatingSystem结构(来自MSDN)

class Win32_OperatingSystem : CIM_OperatingSystem
{
string BootDevice;
string BuildNumber;
string BuildType;
string Caption;
string CodeSet;
string CountryCode;
string CreationClassName;
string CSCreationClassName;
string CSDVersion;
string CSName;
sint16 CurrentTimeZone;
boolean DataExecutionPrevention_Available;
boolean DataExecutionPrevention_32BitApplications;
boolean DataExecutionPrevention_Drivers;
uint8 DataExecutionPrevention_SupportPolicy;
boolean Debug;
string Description;
boolean Distributed;
uint32 EncryptionLevel;
uint8 ForegroundApplicationBoost;
uint64 FreePhysicalMemory;
uint64 FreeSpaceInPagingFiles;
uint64 FreeVirtualMemory;
datetime InstallDate;
uint32 LargeSystemCache;
datetime LastBootUpTime;
datetime LocalDateTime;
string Locale;
string Manufacturer;
uint32 MaxNumberOfProcesses;
uint64 MaxProcessMemorySize;
string MUILanguages[];
string Name;
uint32 NumberOfLicensedUsers;
uint32 NumberOfProcesses;
uint32 NumberOfUsers;
uint32 OperatingSystemSKU;
string Organization;
string OSArchitecture;
uint32 OSLanguage;
uint32 OSProductSuite;
uint16 OSType;
string OtherTypeDescription;
Boolean PAEEnabled;
string PlusProductID;
string PlusVersionNumber;
boolean Primary;
uint32 ProductType;
uint8 QuantumLength;
uint8 QuantumType;
string RegisteredUser;
string SerialNumber;
uint16 ServicePackMajorVersion;
uint16 ServicePackMinorVersion;
uint64 SizeStoredInPagingFiles;
string Status;
uint32 SuiteMask;
string SystemDevice;
string SystemDirectory;
string SystemDrive;
uint64 TotalSwapSpaceSize;
uint64 TotalVirtualMemorySize;
uint64 TotalVisibleMemorySize;
string Version;
string WindowsDirectory;
};

初始化WMI 连接 查询其中各个元素就可以获取信息

代码如下:

 // WMI_Sample.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <atlstr.h>
#include <comutil.h>
#include <comdef.h>
#include <Wbemidl.h> using namespace std; #pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "comsuppw.lib") //===================================================
class CMyWMI{
IWbemLocator *pLoc_;
IWbemServices *pSvc_; void GetInfo(WCHAR* wszQueryInfo,IWbemClassObject *pclsObj);
public:
CMyWMI():pLoc_(NULL),pSvc_(NULL){}
~CMyWMI(){ ClearWMI(); }
bool InitWMI();
bool ClearWMI();
bool QuerySystemInfo(); }; void CMyWMI::GetInfo(WCHAR* wszQueryInfo,IWbemClassObject *pclsObj)
{
if(wszQueryInfo == NULL || NULL == pclsObj)
return;
VARIANT vtProp;
char* lpszText = NULL; HRESULT hr = pclsObj->Get(wszQueryInfo, , &vtProp, , );
lpszText = _com_util::ConvertBSTRToString(V_BSTR(&vtProp));
printf_s("%s\n", lpszText); delete[] lpszText;
VariantClear(&vtProp);
} bool CMyWMI::QuerySystemInfo()
{
HRESULT hres; //定义COM调用的返回
IEnumWbemClassObject* pEnumerator = NULL;
bool bRet = false; try{
hres = pSvc_->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_OperatingSystem"),
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"BootDevice",pclsObj);
GetInfo(L"Caption",pclsObj);
GetInfo(L"Manufacturer",pclsObj);
GetInfo(L"CSName",pclsObj);
GetInfo(L"WindowsDirectory",pclsObj);
GetInfo(L"SystemDirectory",pclsObj);
GetInfo(L"TotalVisibleMemorySize",pclsObj);
GetInfo(L"FreePhysicalMemory",pclsObj); 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;
} bool CMyWMI::ClearWMI()
{
bool bRet = false; if( NULL != pSvc_)
pSvc_->Release(); if(pLoc_ != NULL )
pLoc_->Release(); CoUninitialize();
bRet = true;
return bRet;
} bool CMyWMI::InitWMI()
{
HRESULT hres; //定义COM调用的返回
bool bRet = false; try{
hres = CoInitializeEx(, COINIT_MULTITHREADED);
if (FAILED(hres))
{
throw exception("CoInitializeEx() error.");
} hres = CoInitializeSecurity(
NULL,
-, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
); if (FAILED(hres))
{
throw exception("CoInitializeEx() error.");
} hres = CoCreateInstance(
CLSID_WbemLocator,
,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc_); if (FAILED(hres))
{
throw exception("CoCreateInstance() error.");
} // to make IWbemServices calls.
hres = pLoc_->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
, // Locale. NULL indicates current
NULL, // Security flags.
, // Authority (e.g. Kerberos)
, // Context object
&pSvc_ // pointer to IWbemServices proxy
); if (FAILED(hres))
{
throw exception("ConnectServer() error.");
} hres = CoSetProxyBlanket(
pSvc_, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
); if (FAILED(hres))
{
throw exception("CoSetProxyBlanket() error.");
} }catch(exception& e)
{
cout << e.what() << endl;
return bRet;
} bRet = true;
return bRet;
} //=========================================================
int _tmain(int argc, _TCHAR* argv[])
{
CMyWMI myWMI; myWMI.InitWMI();
myWMI.QuerySystemInfo(); return ;
}

python 代码:

#!/usr/bin/env python
# -*- coding: cp936 -*- import wmi
import os
import sys
import platform
import time def sys_version():
c = wmi.WMI ()
#获取操作系统版本
for sys in c.Win32_OperatingSystem():
print "Version:%s" % sys.Caption.encode("UTF8"),"Vernum:%s" % sys.BuildNumber
print sys.OSArchitecture.encode("UTF8")#系统是32位还是64位的
print sys.NumberOfProcesses #当前系统运行的进程总数
print sys.WindowsDirectory
print sys.SystemDirectory
print "system total memory: " ,sys.TotalVisibleMemorySize
print "system free memory: " ,sys.FreePhysicalMemory def main():
sys_version() if __name__ == '__main__':
main()
#print platform.system()

效果图

主机性能监控之wmi 获取系统信息及内存性能信息的更多相关文章

  1. 主机性能监控之wmi 获取磁盘信息

    标 题: 主机性能监控之wmi 获取磁盘信息作 者: itdef链 接: http://www.cnblogs.com/itdef/p/3990541.html 欢迎转帖 请保持文本完整并注明出处 仅 ...

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

    标 题: 主机性能监控之wmi 获取进程信息作 者: itdef链 接: http://www.cnblogs.com/itdef/p/3990499.html 欢迎转帖 请保持文本完整并注明出处 仅 ...

  3. dotnet 通过 WMI 获取系统信息

    本文告诉大家如何通过 WMI 获取系统信息 通过 Win32_OperatingSystem 可以获取系统信息 var mc = "Win32_OperatingSystem"; ...

  4. 获取本机内存使用信息、DataTable占用内存空间

    相当于windows系统中的任务管理器,功能是通过系统的API实现的本机的监视,代码如下 using System;using System.Collections.Generic;using Sys ...

  5. PowerShell 通过 WMI 获取系统信息

    本文告诉大家如何通过 WMI 使用 Win32_OperatingSystem 获取设备厂商 通过下面代码可以获取 系统版本和系统是专业版还是教育版 Get-WmiObject Win32_Opera ...

  6. PHP怎么获取系统信息和服务器详细信息

    https://zhidao.baidu.com/question/1435990326608475859.html 获取系统类型及版本号: php_uname() (例:Windows NT COM ...

  7. GetSystemInfo 和 GlobalMemoryStatus获取系统信息,内存信息

    // GetSystemInfo.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #in ...

  8. 获取CPU和内存呢信息

    #include <stdio.h> #include <stdlib.h> #include <winsock.h> #pragma comment(lib, & ...

  9. Citrix 服务器虚拟化之十三 Xenserver虚拟机内存优化与性能监控

    Citrix 服务器虚拟化之十三   Xenserver虚拟机内存优化与性能监控 XenServer的DMC通过自动调节运行的虚拟机的内存,每个VM分配给指定的最小和最大内存值之间,以保证性能并允许每 ...

随机推荐

  1. perl二维数组

    [转载]出处:http://www.cnblogs.com/visayafan/ 1 数组与引用 2 声明的区别 3 访问的区别 4 添加行元素 5 添加列元素 6 访问与打印 6.1 运算符优先级 ...

  2. 刘志梅201771010115.《面向对象程序设计(java)》第六周学习总结

    实验六 继承定义与使用 实验时间 2018-9-28 1.实验目的与要求 (1) 继承的定义:用已有类来构建新类的一种机制.当定义了一个新类继承了一个类时,这个新类就继承了这个类的方法和域,同时在新类 ...

  3. Activiti流程设计工具

    在Actitivi工程的src/main/resources新建一个文件夹diagrams 然后右键,创建一个activiti Diagram 取名为helloWorld后finish 中间区域,是我 ...

  4. REST framwork之分页器,路由器,响应器

    一 REST framwork分页器: from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination ...

  5. 移动端使用mint-ui loadmore实现下拉刷新上拉显示更多

    前序:在使用vue做一个h5项目的时候,需要上拉分页加载,实践中总结一下: 首先要安装mint-ui npm i mint-ui -S 然后引入,一般在main.js里面 import Vue fro ...

  6. SQL Server事务

    事务全部是关于原子性的.原子性的概念是指可以把一些事情当做一个单元来看待.从数据库的角度看,它是指应全部执行或全部都不执行的一条或多条语句的最小组合.为了理解事务的概念,需要能够定义非常明确的边界.事 ...

  7. Java 8 方法引用

    转自:https://www.runoob.com/java/java8-method-references.html 方法引用通过方法的名字来指向一个方法. 方法引用可以使语言的构造更紧凑简洁,减少 ...

  8. 最适合入门的Laravel中级教程(四)前端开发

    Laravel 使用 npm 安装前端依赖: npm 是一个类似 composer 的工具: 用于管理前端的各种依赖包: 在使用之前需要先安装 node : Windows 下可以在官网下载安装: h ...

  9. Mad Lids游戏 华氏与摄氏温度转换

    name1 = input('请输入一个名字:') name2 = input('请输入一个名字:') vehicle = input('请输入一种车子:') print('\n上近代史的{}刚下课, ...

  10. faster rcnn源码阅读笔记1

    自己保存的源码阅读笔记哈 faster rcnn 的主要识别过程(粗略) (开始填坑了): 一张3通道,1600*1600图像输入中,经过特征提取网络,得到100*100*512的feature ma ...