SOL9 Sample: ServiceWatcher

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


//SOL9
//2009/01/23 

// 2010/04/01 Modified to use MultiThreadedModel, WbemLocator and WbemServices classes.
// 2010/04/01 Modified to use WbemObjectSink class instead of WbemProcessSink class.

#include <sol\Locale.h>
#include <sol\Thread.h>
#include <sol\com\MultiThreadedModel.h>
#include <sol\wmi\WbemLocator.h>

#include <sol\wmi\WbemServices.h>
#include <sol\wmi\WbemServiceSink.h>
#include <sol\wmi\WbemObjectStubSink.h>
#include <sol/Locale.h>

#pragma comment(lib, "wbemuuid.lib")

namespace SOL {

/**
 * Thread of ServiceStatusWatcher 
 */
class ServiceWatcher: public Thread {
private:
  bool looping;

public:  
  /**
   * Constructor
   */
  ServiceWatcher()
    :looping(true) 
  {
  
  }

public:
  /**
   * Thread procedure to watch an instance modification of Win32_Service.
   */
  void run()
  {
    printf("ServiceWatcher#run,1,started\n");
    //ApartmentThreadedModel model;
    MultiThreadedModel model;

    try {
      WbemLocator locator;
      
      WbemServices services = locator.connectServer(L"ROOT\\CIMV2");
      
      WbemObjectSink serviceSink;

      HRESULT hr = S_OK;
      const BSTR query = L"SELECT * FROM __InstanceModificationEvent WITHIN 1 "
          L"WHERE TargetInstance ISA 'Win32_Service'";

      if (SUCCEEDED(hr = services.execNotificationQueryAsync(query,
        &serviceSink))) {

        while(looping) {
          // The services->execNotificationQueryAsync(...) method will call the virtual 
          // WbemServiceSink::Indicate method when an event occurs
          Sleep(500);
        }
      }


    } catch (HRESULT hr) {
      printf("ServiceWatcher#run,2,Exception:%08x", hr);
    }
    printf("ServiceWatcher#run,3,terminated\n");

  }

public:
  void stop() {
    looping = false;
  }
};

}


int _tmain(int argc, TCHAR ** argv)
{
  try {
    //2009/10/22
    Locale locale;

    ServiceWatcher serviceWatcher;

    //Start the thread of ServiceWatcher
    serviceWatcher.start();

    //Wait for 30 minutes.
    //Sleep(1000*60*30);

    //Stop the thread
    //serviceWatcher.stop();
    serviceWatcher.wait();

  } catch (...) {

  }
  return 1;
}




Last modified: 2 May 2016

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