SOL9 Sample: SWbemClassInspector

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


//2009/12/22 Modified to save class information to an xml file by using SOL::ReportWriter class.
//2009/12/22 Modified to accept namespace and classname from command line.
// Example:  SWbemClassInspector.exe root\cimv2 Win32_Process

#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>
#include <sol\wmi\SWbemPropertySet.h>
#include <sol\wmi\SWbemMethodSet.h>
#include <sol\wmi\SWbemQualifierSet.h>


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

namespace SOL {

/**
 * Thread of ClassInspector 
 */
class SWbemClassInspector: public Thread {

private:
  _bstr_t nameSpace;

  _bstr_t className;

public:  
  /**
   * Constructor
   */
  SWbemClassInspector(TCHAR* tnameSpace, TCHAR* tclassName)
  {
    nameSpace = tnameSpace;
    className = tclassName;
  }


private:
  void inspectClass(SWbemLocator& locator, BSTR nameSpace, BSTR clsName)
  {
    printf("SWbemClassInspector started for:%S\\%S\n", nameSpace, clsName);
    try {
      SWbemServices services = locator.connectServer(L".", nameSpace);

      SWbemObjectSet objectSet = services.subclassesOf();

      long count = objectSet.getCount();
      wchar_t reportFile[1024];
      swprintf_s(reportFile, SizeOf(reportFile), L".\\SWbemClassInspector_%s_Report.xml", clsName);

      //Writer writer;
      //ConsoleWriter writer;
      FileWriter writer(reportFile);

      writer.write(L"<?xml version=\"1.0\" ?>\n");
      writer.write(L"<SWbemClassInspectorResult>\n");

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

      writer.write(L"<NameSpace>%s</NameSpace>\n", 
          (const wchar_t*)nameSpace);
      writer.write(L"<ClassName>%s</ClassName>\n", 
          (const wchar_t*)clsName);

      for (long i = 0; i<count; i++) {
        try {
          SWbemObject object = objectSet.itemIndex(i);
        
          SWbemObjectPath objectPath = object.getPath(); 
          //BString path = objectPath.getPath();

          _bstr_t className = objectPath.getClass();
          _bstr_t bname = clsName;
          if (className == bname) {

            SWbemQualifierSet qualSet = object.getQualifiers();
            qualSet.write(writer);

            SWbemPropertySet propSet = object.getProperties();
            propSet.write(writer);

            SWbemMethodSet methodSet = object.getMethods();
            methodSet.write(writer);
            break;
          }
        } catch (...) {
        
        }   
      }
      writer.write(L"</SWbemClassInspectorResult>\n");

      printf("Saved resultSet to a file:%S\n", reportFile);

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

public:
  /**
   * Thread procedure to watch an instance creation of Win32_Process.
   */
  void run()
  {
    ApartmentThreadedModel apartmentThreadedModel;

    try {

      SWbemLocator locator;

      inspectClass(locator, (BSTR)nameSpace, (BSTR)className);

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

};

}


void _tmain(int argc, TCHAR ** argv)
{
  try {
    Locale locale;

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

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

    SWbemClassInspector inspector(nameSpace, className);

    //Start the thread of ClassInspector
    inspector.start();

    inspector.wait();

  } catch (HRESULT hr) {
    printf("Exception HRESULT=0%x\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.