SOL9 Sample: SWbemNameSpaceEnumerator

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


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

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

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


namespace SOL {

/**
 * Thread of NameSpaceEnumerator 
 */
class SWbemNameSpaceEnumerator: public Thread {

public:  
  /**
   * Constructor
   */
  SWbemNameSpaceEnumerator()
  {
  
  }

private:
  void enumNameSpaces(SWbemLocator& locator,BSTR base, BSTR child, Writer& writer)
  {
    size_t blen = SysStringLen(base) + SysStringLen(child) + 1 + 1;
    wchar_t* nameSpace = new wchar_t[blen];
    swprintf_s(nameSpace, blen, L"%s\\%s", base, child);

    try {
      // Server is this local machine (".").
      SWbemServices services = locator.connectServer(L".", nameSpace);
      SWbemObjectSet objectSet = services.instancesOf( 
          L"__NAMESPACE",
          0,
          NULL);

      long count = objectSet.getCount();

      writer.write(L"<%s>\n", (const wchar_t*)child);

      for (long i = 0; i<count; i++) {
        try {
          SWbemObject object = objectSet.itemIndex(i);
          VARIANT vt;
          VariantInit(&vt);
        
          object.getProperty(L"Name", &vt);
          BSTR subNameSpace = vt.bstrVal;

          printf("NameSpace: %S\\%S\n", nameSpace, (const wchar_t*)subNameSpace);
          
          //Recursively call enumNameSpaces for child namespace of subNameSpace
          enumNameSpaces(locator, nameSpace, subNameSpace, writer);

          VariantClear(&vt);
          
        } catch (...) {

        }   
      }
      writer.write(L"</%s>\n", (const wchar_t*)child);
  
    } catch (HRESULT hr) {
      ; //Don't care this IException, simply catching it.
    }
    delete [] nameSpace;
  }

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

    try {
      SWbemLocator locator;

      const wchar_t* reportFile = L".\\SWbemNameSpaceEnumearatorResult.xml";    
      FileWriter writer(reportFile);
      writer.write(L"<?xml version=\"1.0\" ?>\n");
      writer.write(L"<SWbemNameSpaceEnumearatorResult>\n");

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

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

};

}


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

    printf("SWbemNameSpaceEnumerator started\n");
    SWbemNameSpaceEnumerator enumerator;

    //Start the thread of NameSpaceEnumerator
    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.