在VC中使用WMI(英文原版)作者:BugSearcher

原创 2004年09月04日 10:29:00

Introduction

We normally find a lot of ways and a number of resources to use WMI or to get information from “Windows Management Instrumentation” while using Visual Basic 6 and C#, but I could not find a single resource describing the same thing in Visual C++. MSDN resources are also very limited in this context.

Code

Following is the code on how to get the current processor load from a WMI class Win32_Processor defined in a .mof file. .mof files are managed object files which have a typical way of defining classes and methods.

WMI provides the COM service which is used to connect to the WMI services. The important parts of the code include:

  • bstrNamespace : The initialization of this variable is very tricky. The first three forward slashes //./ represent the Host Computer name from which you want to get information from. A “.” Indicates that information is to be obtained from the Same Computer on which you are working. You can give any Network name here but getting information from network depends upon your Access Rights etc. cimv2 is the namespace containing the Win32_Processor class.
  • pIWbemLocator is the argument in which we get the Interface pointer.
  • After that, we call the function ConnectServer of the pIWbemLocator to get a pointer to pWbemServices.
  • WMI uses its own Query Language to get information known as WQL (the acronym for WMI Query Language). So, when calling the function ExecQuery, we have to specify the language as its first argument. Second argument is the Query itself. Last argument is important because here we get a pointer to an Enumeration object through which we can enumerate through the objects available. This enumeration is important because consider the case that we want to know the information about running processes and we are using Win32_Process class for this purpose. Then through this enumeration object, we can go through different processes one by one.
  • By calling the Reset and Next methods of pEnumObject, we are moving through the objects. We get the pointer to an object in pClassObject.
  • The last function through which we get the Actual value of a property is Get. We pass a BSTR to this function to get the value in a Variant.
CoInitialize(NULL);

//Security needs to be initialized in XP first 

if(CoInitializeSecurity( NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
0
) != S_OK)
    return;

IWbemLocator * pIWbemLocator = NULL;
IWbemServices * pWbemServices = NULL;
IEnumWbemClassObject * pEnumObject = NULL;
BSTR bstrNamespace = (L"root//cimv2");
HRESULT hRes = CoCreateInstance (
  CLSID_WbemAdministrativeLocator,
  NULL ,
  CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER , 
  IID_IUnknown ,
  ( void ** ) & pIWbemLocator
  ) ;
if (SUCCEEDED(hRes))
{
  hRes = pIWbemLocator->ConnectServer(
  bstrNamespace, // Namespace
  NULL, // Userid
  NULL, // PW
  NULL, // Locale
  0, // flags
  NULL, // Authority
  NULL, // Context
  &pWbemServices
  );
}
BSTR strQuery = (L"Select * from win32_Processor");
BSTR strQL = (L"WQL");
hRes = pWbemServices->ExecQuery(strQL, strQuery,
  WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject);

ULONG uCount = 1, uReturned;
IWbemClassObject * pClassObject = NULL;
hRes = pEnumObject->Reset();
hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned);
VARIANT v;
BSTR strClassProp = SysAllocString(L"LoadPercentage");
hRes = pClassObject->Get(strClassProp, 0, &v, 0, 0);
SysFreeString(strClassProp);

_bstr_t bstrPath = &v; //Just to convert BSTR to ANSI
char* strPath=(char*)bstrPath;
if (SUCCEEDED(hRes))
MessageBox(strPath);
else
MessageBox(”Error in getting object”);
VariantClear( &v );
pIWbemLocator->Release();
pWbemServices->Release();
pEnumObject->Release();
pClassObject->Release();
CoUninitialize();

Conclusion

This was the shortest method I was able to work out to get information from any WMI class. You can simply change the class name in the Query and Property Name while calling Get method and you will get information from all the classes supported in your OS. I tested this code in Windows 2000 Professional. I hope it will work well for Win XP but probably not in previous versions of windows because they don’t support a lot of WMI classes.

Update

I am updating this Article and the code for Windows XP. As I said in the beginning, this was initially intended for Windows 2K and it was still working fine on Windows 2K machine (I have checked it on at least 10 now coz I was getting so many complaints). Well, for Windows XP there is one major change and that was suggested by igoychev. Many thanks for that. The problem with Windows XP was that it needed security to be initialized first. Plus, I have also changed those confusing slashes in bstrNamespace to some simpler ones. I am using Windows 2003 Advance Server and it is working fine on it. You need to include wbemuuid.lib in Project Settings -> Linker -> Additional Dependencies to get this code working. I have also added some error checking to avoid crashes, hope it works for you. You need to have Platform SDK installed for this code to compile. WMI SDK is not necessary once again. Sorry rbervgm, your work was great using WMI SDK but I thought better to keep it WMI SDK Clean. Hope you don't mind. Now, I hope that downloadable project will work for most of the people. One more thing, it still works for Windows 2K machines.

编程资源 : vc6英文企业版完整版5CD

前言 在csdn中找vc6来用, 看到上传的都是url, 大部分都失效了. 在网上找齐了一份能用的vc6英文企业版完整版5CD, 备了个份到CSDN. 下载点 vc6英文企业版5cd.part01.r...
  • LostSpeed
  • LostSpeed
  • 2015年10月18日 13:35
  • 1523

利用WMI打造完美三无后门(scrcons.exe)

ASEC是WMI中的一个标准永久事件消费者。它的作用是当与其绑定的一个事件到达时,可以执行一段预先设定好的JS/VBS脚本...
  • QHH_QHH
  • QHH_QHH
  • 2015年10月16日 20:26
  • 4623

WMI技术介绍和应用——查询系统信息和补丁包信息

本文使用了《WMI技术介绍和应用——使用VC编写一个半同步查询WMI服务的类》中代码做为基础。本节只是列出了WQL语句,具体使用参看前面的例子。(转载请指明出于breaksoftware的csdn博客...
  • breaksoftware
  • breaksoftware
  • 2013年02月06日 18:30
  • 7914

Vert.x-web的讲解和使用(英文原版)

Vert.x-Web Vert.x-Web is a set of building blocks for building web applications with Vert.x. Th...
  • z714303584
  • z714303584
  • 2015年07月16日 10:46
  • 2638

编程语言编年史(英文原版)

We already know that programming language is a formal language designed to communicate instruction...
  • twlkyao
  • twlkyao
  • 2013年12月16日 10:39
  • 1157

计算机网络_第四版_英文原版作者Andrew.rar

  • 2008年12月23日 20:12
  • 8.59MB
  • 下载

机器学习大家迈克尔.乔丹谈大数据等重大技术探索的迷思(英文原版)

来源:http://spectrum.ieee.org/robotics/artificial-intelligence/machinelearning-maestro-michael-jordan-...
  • mao_kun
  • mao_kun
  • 2014年12月17日 11:01
  • 2146

NMAP迷你使用手册(英文原版)

  • 2013年10月26日 11:56
  • 193KB
  • 下载

OutofControl 失控 英文原版 pdf 作者 KevinKelly 凯文凯利

  • 2017年08月08日 21:14
  • 607KB
  • 下载

VC技术内幕第五版(英文原版)

  • 2008年12月12日 22:32
  • 7.46MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:在VC中使用WMI(英文原版)作者:BugSearcher
举报原因:
原因补充:

(最多只允许输入30个字)