ITPub博客

首页 > 应用开发 > IT综合 > 关于用WMI获取系统信息——编程实现(转)

关于用WMI获取系统信息——编程实现(转)

原创 IT综合 作者:RegisterForBlog 时间:2007-09-19 22:17:17 0 删除 编辑
关于用WMI获取系统信息——编程实现(转)[@more@]

  一直以来都没有用过WMI, 都是用的PDH来获取系统信息.感觉两者很些相似,都是类似于性能数据库,基本的一些什么硬件软件信息都可以比较全面的获得.只是用VC来调用相对比较繁杂,用VB和C#就方便多了.这些天到MSDN上找了些资料,总算大致搞清了一些编程思路.对于那些很想知道怎么获取诸如CPUID,硬盘ID,BIOS信息以及主板信息的人来说应该有些帮助吧.试着用MSDN的例子乱写了一通,以下是通过WMI获取BIOS的例子,不对之处还请指正 ^_^:

  --------------------------------------------------------------------------------

  #define _WIN32_DCOM

  #include

  using namespace std ;

  #include

  #include

  #include

  #pragma comment(lib, "Wbemuuid")

  int main(int argc,char**argv)

  {

  HRESULT hres ;

  // Initialize COM.

  hres=CoInitializeEx(0,COINIT_MULTITHREADED);

  if(FAILED(hres))

  {

  cout<<"Failed to initialize COM library. Error code = 0x"

  <

  return 1 ;

  // Program has failed.

  }

  // Initialize

  hres=CoInitializeSecurity(

  NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_DEFAULT,

  RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL);

  if(FAILED(hres))

  {

  cout<<"Failed to initialize security. Error code = 0x"

  <

  CoUninitialize();

  return 1 ;

  // Program has failed.

  }

  // Obtain the initial locator to Windows Management on a particular host computer.

  IWbemLocator*pLoc=0 ;

  hres=CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER,

  IID_IWbemLocator,(LPVOID*)&pLoc);

  if(FAILED(hres))

  {

  cout<<"Failed to create IWbemLocator object. Err code = 0x"

  <

  CoUninitialize();

  return 1 ;

  // Program has failed.

  }

  IWbemServices*pSvc=0 ;

  // Connect to the rootcimv2 namespace with the current user and obtain pointer pSvc

  // to make IWbemServices calls.

  hres=pLoc->ConnectServer(_bstr_t(L"ROOTCIMV2"),

  NULL,NULL,0,NULL,0,0,&pSvc);

  if(FAILED(hres))

  {

  cout<<"Could not connect. Error code = 0x"

  <

  pLoc->Release();

  CoUninitialize();

  return 1 ;

  // Program has failed.

  }

  cout<<"Connected to WMI"<

  // Set the IWbemServices proxy so that impersonation of the user (client) occurs.

  hres=CoSetProxyBlanket(pSvc,RPC_C_AUTHN_WINNT,RPC_C_AUTHZ_NONE,NULL,

  RPC_C_AUTHN_LEVEL_CALL,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE);

  if(FAILED(hres))

  {

  cout<<"Could not set proxy blanket. Error code = 0x"

  <

  pSvc->Release();

  pLoc->Release();

  CoUninitialize();

  return 1 ;

  // Program has failed.

  }

  // Use the IWbemServices pointer to make requests of WMI.

  // Make requests here:

  // For example, query for print queues that have more than 10 jobs

  IEnumWbemClassObject*pEnumerator=NULL ;

  hres=pSvc->ExecQuery(

  bstr_t("WQL"),

  bstr_t("SELECT * from Win32_BIOS"),

  WBEM_FLAG_FORWARD_ONLY|WBEM_FLAG_RETURN_IMMEDIATELY,

  NULL,

  &pEnumerator);

  if(FAILED(hres))

  {

  cout<<"Query for print queues failed. Error code = 0x"

  <

  pSvc->Release();

  pLoc->Release();

  CoUninitialize();

  return 1 ;

  // Program has failed.

  }

  else

  {

  IWbemClassObject*pInstance=NULL ;

  ULONG dwCount ;

  while(pEnumerator->Next(

  WBEM_INFINITE,

  1,

  &pInstance,

  &dwCount)==S_OK)

  {

  SAFEARRAY*pvNames=NULL ;

  if(pInstance->GetNames(NULL,WBEM_FLAG_ALWAYS|WBEM_MASK_CONDITION_ORIGIN,NULL,&pvNames)==S_OK)

  {

  long vbl,vbu ;

  SafeArrayGetLBound(pvNames,1,&vbl);

  SafeArrayGetUBound(pvNames,1,&vbu);

  for(long idx=vbl;idx<=vbu;idx++)

  {

  long aidx=idx ;

  wchar_t *wsName=0 ;

  VARIANT vValue ;

  VariantInit(&vValue);

  SafeArrayGetElement(pvNames,&aidx,&wsName);

  BSTR bs=SysAllocString(wsName);

  hres=pInstance->Get(bs,0,&vValue,NULL,0);

  SysFreeString(bs);

  if(SUCCEEDED(hres))

  {

  char szANSIString[MAX_PATH];

  WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,wsName,-1,

  szANSIString,sizeof(szANSIString),NULL,NULL);

  cout<

  switch(vValue.vt)

  {

  case VT_BSTR :

  wprintf(L"%s",V_BSTR(&vValue));

  break ;

  case VT_I2 :

  wprintf(L"%d",V_I2(&vValue));

  break ;

  case VT_I4 :

  wprintf(L"%d",V_I4(&vValue));

  break ;

  case VT_BOOL :

  wprintf(L"%s",V_BOOL(&vValue)?L"TRUE":L"FALSE");

  break ;

  default:

  /*WCHAR tmp[100];

  wcscpy(tmp, V_BSTR(&vValue));

  char tmp1[MAX_PATH];

  WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,tmp,-1,

  tmp1,sizeof(tmp1),NULL,NULL);

  cout<

  break;

  }

  cout<

  }

  SysFreeString(wsName);

  }

  }

  else

  {

  cout<<"Query for print queues failed. Error code = 0x"

  <

  pSvc->Release();

  pLoc->Release();

  CoUninitialize();

  return 1 ;

  // Program has failed.

  }

  if(pvNames)SafeArrayDestroy(pvNames);

  }

  if(pInstance)pInstance->Release();

  }

  // Cleanup

  // ========

  pEnumerator->Release();

  pSvc->Release();

  pLoc->Release();

  CoUninitialize();

  return 0;

  // Program successfully completed.

  }

  //-----------------------------------

  对于怎么用BCB编, 感觉还容易一点,因为我在类型转换的时候碰到了一些问题,而在BCB中的Variant类做了比较好的封装, 具体代码可以 http://www.cppfans.com上找到.

  void GetWmiInfo(TStrings *lpList, WideString wsClass)

  {

  IWbemLocator *pWbemLocator = NULL;

  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)

  {

  IWbemServices *pWbemServices = NULL;

  WideString wsNamespace = (L"rootcimv2");

  if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)

  {

  IEnumWbemClassObject *pEnumClassObject = NULL;

  WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;

  if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)

  {

  IWbemClassObject *pClassObject = NULL;

  ULONG uCount = 1, uReturned;

  if(pEnumClassObject->Reset() == S_OK)

  {

  int iEnumIdx = 0;

  while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)

  {

  lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");

  SAFEARRAY *pvNames = NULL;

  if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)

  {

  long vbl, vbu;

  SafeArrayGetLBound(pvNames, 1, &vbl);

  SafeArrayGetUBound(pvNames, 1, &vbu);

  for(long idx=vbl; idx<=vbu; idx++)

  {

  

本文来自:http://www.linuxpk.com/30715.html

-->linux电子图书免费下载和技术讨论基地

·上一篇:NAVGateway技术:网络安全的新理念

·下一篇:Guest权限突破——权限提升方法总结
 
     最新更新
·注册表备份和恢复

·低级格式化的主要作用

·如何防范恶意网站

·常见文件扩展名和它们的说明

·专家:警惕骇客骗局,严守企业信息

·PGPforWindows介紹基本设定(2)

·解剖安全帐号管理器(SAM)结构

·“恶作剧之王”揭秘

·绿色警戒

·黑客反击战

·网络四大攻击方法及安全现状描述

·可攻击3种浏览器代码流于互联网

·黑客最新的兴趣点,下个目标会是谁?

·“僵尸”——垃圾邮件的主要传播源

·Lebreat蠕虫惊现3变种

·POSTFIX反病毒反垃圾Ų…

·在FreeBSD上用PHP实现在线添加FTP用户

·简单让你在FreeBSDADSL上…

·安全版本:OpenBSD入门技巧解析

·Internet连接共享上网完全攻略

·关于ADSL上网网速常识

·静态缓存和动态缓存的比较

·最友好的SQL注入防御方法

·令网站提速的7大秘方

·网络基础知识大全

·路由基本知识

·端口映射的几种实现方法

·VLAN经典诠释

·问题分析与解决——ADSL错误代码

·问题分析——关于2条E1的线路绑定


关于我们 | 联系方式 | 广告合作 | 诚聘英才 | 网站地图 | 网址大全 | 友情链接 | 免费注册

Copyright © 2004 - 2007 All Rights Reserved

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10763080/viewspace-970256/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论