SOL9 Sample: WbemAntiVirusProductQueryApplet

SOL9 2.0 Samples

1 Screenshot



2 Source code

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


//SOL9
// 2012/10/22

#include <sol/wmi/WbemQueryApplet.h>
#include <sol/FileWriter.h>

void _tmain(int argc, const TCHAR** argv)
{
  MultiThreadedModel model;

  try {
    Locale locale;
    FileWriter writer(L".\\AntivirusProduct.xml");

    //1 Create a locator
    SOL::WbemLocator locator;
      
    writer.write(L"<?xml version='1.0'?>\n");

    writer.write(L"<AntiVirusProducts>\n");

    const wchar_t* namespaces[] = {L"ROOT\\SecurityCenter", L"ROOT\\SecurityCenter2"};

    for (int n = 0; n<SizeOf(namespaces); n++) {
      //2 Connect to a server 
      SOL::WbemServices services = locator.connectServer((BSTR)namespaces[n]);
      writer.write(L"\n<Namespace name='%s'>\n", namespaces[n]);

      //3 ExecQuery
      SOL::WbemClassObjectEnumerator enumerator = services.execQuery(L"select * From AntiVirusProduct");

      //4 Display the properites (pairs of name-value) of WbemClassObject 
      bool looping = true;
      while (looping) {
        try {
          SOL::WbemClassObject object = enumerator.next();
          //5 Get SafeArray of names of 'properties' of the object.
          SafeArray sarray = object.getNames();
          long l = sarray.getLBound();
          long h = sarray.getUBound();
          writer.write(L"<AntiVirusProduct>\n");
          for(long i = l; i<h; i++) {
            try {
              //6 Get a name of i-th element of the sarray.
              _bstr_t name = sarray.getString(i);
              //7 GEt a variant-value of the name from the object
              _variant_t variant = object.get(name);
  
              COMTypeConverter converter;
              _bstr_t value = converter.toString(variant);

              //8 Display the name and vaue of the i-th element of the properites of the object.
              writer.write(L"<Property name='%s' value='%s'/>\n", 
                (const wchar_t*)name, (const wchar_t*)value);
            } catch (...) {

            }
          }
          writer.write(L"</AntiVirusProduct>\n");

        } catch (...) {
          break;
        }
      }
      writer.write(L"</Namespace>\n");

    }

    writer.write(L"</AntiVirusProducts>\n");

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



Last modified: 2 May 2016

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