SOL9 Sample: SWbemClassEnumerator

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * SWbemClassEnumerator.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2009/12/22 Modified to save enumerated classes to an xml file by using SOL::ReportWriter class.

// 2009/12/23 Modified to accept a namespace from command line
// For example: SWbemClassEnumerator.exe root\Default

#include <sol/Locale.h>

#include <sol\Thread.h>
#include <sol\com\ApartmentThreadedModel.h>
#include <sol\wmi\SWbemLocator.h>
#include <sol\wmi\SWbemServices.h>
#include <sol\wmi\SWbemObjectSet.h>
#include <sol\wmi\SWbemObjectPath.h>


// For WMI Scripting, see the following page. 
// http://msdn.microsoft.com/en-us/library/ms974592.aspx

namespace SOL {

/**
 * Thread of ClassEnumerator 
 */
class SWbemClassEnumerator: public Thread {
private:
  _bstr_t nameSpace;

public:  
  /**
   * Constructor
   */
  SWbemClassEnumerator(TCHAR* tnameSpace)
  {
    nameSpace = tnameSpace;
  }


private:
  void enumClasses(SWbemLocator& locator, BSTR nameSpace)
  {
    try {
      SWbemServices services = locator.connectServer(L".", nameSpace);

      SWbemObjectSet objectSet = services.subclassesOf();
      wchar_t reportFile[1024];
      StringT<wchar_t> rnameSpace = nameSpace;
      rnameSpace.replace('\\', '_');
      swprintf_s(reportFile, SizeOf(reportFile), L".\\SWbemClassEnumerator_%s_Result.xml", (const wchar_t*)rnameSpace);
      
      FileWriter writer(reportFile);

      writer.writeln(L"<?xml version=\"1.0\" ?>");
      writer.writeln(L"<SWbemClassEnumeratorResult>");

  
      LocalDateTime ldt;
      StringT<wchar_t> now;
      writer.writeln(L"<Generated dateTime=\"%s\"/>", ldt.nowToSeconds(now));

      writer.writeln(L"<Namespace>%s</Namespace>", nameSpace);

      long count = objectSet.getCount();

      writer.writeln(L"<Count>%d</Count>", count);
      HTMLEncoder encoder;
      for (long i = 0; i<count; i++) {
        try {
          SWbemObject object = objectSet.itemIndex(i);
        
          SWbemObjectPath objectPath = object.getPath(); 
          _bstr_t path = objectPath.getPath();
          _bstr_t className = objectPath.getClass();


          printf("Path: No=%d  %S Class=%S\n", i, 
            (const wchar_t*)path, (const wchar_t*)className);
          writer.writeln(L"<Class>");            
    
  
          writer.writeln(L"<ClassName>%s</ClassName>", 
            (const wchar_t*)className);

          writer.writeln(L"<Path>%s</Path>", 
            (const wchar_t*)path);

          _bstr_t displayName = objectPath.getDisplayName();
          StringT<wchar_t> wname;
          encoder.encode((const wchar_t*)displayName, wname);
          writer.writeln(L"<DisplayName>%s</DisplayName>", (const wchar_t*)wname);

          writer.writeln(L"</Class>");            


        } catch (...) {
        
        }   
      }
  
      writer.writeln(L"</SWbemClassEnumeratorResult>");

    } catch (Exception ex) {
      ex.printf();

    } catch (HRESULT hr) {
      WbemErrors errors;
      printf("Exception HRESULT=%0x %s\n", hr, errors.getName(hr));
    }
  }

public:
  /**
   * Thread procedure to enumerate all WMI classes under namespace root\cimv2.
   */
  void run()
  {
    ApartmentThreadedModel apartmentThreadedModel;

    try {

      SWbemLocator locator;

      enumClasses(locator, (BSTR)nameSpace);

    } catch (HRESULT hr) {
      WbemErrors errors;
      printf("SWbemClassEnumerator,Exception:HRESULT=%08x %s\n", hr, errors.getName(hr));
    } catch (...) {
      printf("SWbemClassEnumerator,Unknown Exception\n");
    }
  }

};

}


void _tmain(int argc, TCHAR ** argv)
{
  try {
    printf("SWbemClassEnumerator started\n");

    Locale locale;

    //Default values
    TCHAR* nameSpace = _T("root\\cimv2");

    if (argc== 2) {
      //If specified on command line arguments, we use them
      nameSpace = argv[1];
    }    

    SWbemClassEnumerator enumerator(nameSpace);

    //Start the thread of ClassEnumerator
    enumerator.start();

    enumerator.wait();

  } catch (HRESULT hr) {
    printf("Exception HRESULT=%0x\n", hr);
  } catch (...) {
    printf("Unknown Exception\n");
  }

  printf("\nOK, please hit return key\n");
  getchar();
}


Last modified: 2 May 2016

Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.