關於用WMI獲取系統資訊——程式設計實現(轉)

RegisterForBlog發表於2007-09-19
關於用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<

  <

  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<

  <

  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<

  <

  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<

  <

  pLoc->Release();

  CoUninitialize();

  return 1 ;

  // Program has failed.

  }

  cout<

  // 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<

  <

  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<

  <

  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<

  <

  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類做了比較好的封裝, 具體程式碼可以 上找到.

  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++)

  {

  

·上一篇:

·下一篇:
 
     最新更新
·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·

·


| | | | | | |

Copyright © 2004 - 2007 All Rights Reserved

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10763080/viewspace-970256/,如需轉載,請註明出處,否則將追究法律責任。

關於用WMI獲取系統資訊——程式設計實現(轉)
請登入後發表評論 登入
全部評論

相關文章