当前位置:编程学习 > C/C++ >>

C++ Builder通过WMI获取系统信息

答案: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"root\\cimv2");
     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++)
                     {
                       long aidx = idx;
                       wchar_t *wsName = 0;
                       VARIANT vValue;
                       VariantInit(&vValue);
                       SafeArrayGetElement(pvNames, &aidx, &wsName);

                       BSTR bs = SysAllocString(wsName);
                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
                       SysFreeString(bs);

                       if(hRes == S_OK)
                        {
                          AnsiString s;
                          Variant v = *(Variant*)&vValue;
                          if(v.IsArray())
                           {
                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
                              {
                                Variant a = v.GetElement(i);
                                if(!s.IsEmpty())
                                  s+=", ";
                                s+=VarToStr(a);
                              }
                           }
                          else
                           {
                             s = VarToStr(v);
                           }
                          lpList->Add(AnsiString(wsName)+"="+s);
            &n

上一个:需一个栈解迷宫的c++程序
下一个:将下面C++代码翻译成Pascal代码

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,