标 题: 主机性能监控之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. BottomNavigationView 使用

    <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.Cons ...

  2. Percona XtraDB Cluster Strict Mode(PXC 5.7)

    在Percona XtraDB Cluster集群架构中,为了避免多主节点导致的数据异常,或者说一些不被支持的特性引发的数据不一致的情形,PXC集群可以通过配置pxc_strict_mode这个变量来 ...

  3. 03-封装BeanUtil工具类(javabean转map和map转javabean对象)

    package com.oa.test; import java.beans.BeanInfo; import java.beans.IntrospectionException; import ja ...

  4. C语言 链表(VS2012版)

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

  5. i++ 是线程安全的吗?

    相信很多中高级的 Java 面试者都遇到过这个问题,很多对这个不是很清楚的肯定是一脸蒙逼.内心肯定还在质疑,i++ 居然还有线程安全问题?只能说自己了解的不够多,自己的水平有限. 先来看下面的示例来验 ...

  6. Python中的正则表达式(re)

    import re re.match #从开始位置开始匹配,如果开头没有则无 re.search #搜索整个字符串 re.findall #搜索整个字符串,返回一个list 举例: r(raw)用在p ...

  7. [Unity优化]UI优化(三):GraphicRebuild

    参考链接: https://blog.csdn.net/jingangxin666/article/details/80143176 调试过程: 1.修改Image的颜色 2.Graphic.SetV ...

  8. 《算法导论》——矩阵乘法的Strassen算法

    前言: 很多朋友看到我写的<算法导论>系列,可能会觉得云里雾里,不知所云.这里我再次说明,本系列博文时配合<算法导论>一书,给出该书涉及的算法的c++实现.请结合<算法导 ...

  9. Java 原子语义同步的底层实现

    原子语义同步的底层实现 volatile volatile只能保证变量对各个线程的可见性,但不能保证原子性.关于 Java语言 volatile 的使用方法就不多说了,我的建议是 除了 配合packa ...

  10. servlet cdi注入

    @WebServlet("/cdiservlet")//url映射,即@WebServlet告诉容器,如果请求的URL是"/cdiservlet",则由NewS ...