SOL9 2.0 Class: WbemNotificationQueryApplet

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * WbemNotificationQueryApplet.h 
 * Copyright (c) 2012 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2010/02/24
// 2012/03/09 Updated.

#pragma once

#include <sol/wmi/WbemQueryApplet.h>


namespace SOL {

/**
 * Thread for WbemServices::ExecNotificationQuery method call.
 * This is a semi-ascyncronus query.
 */
class WbemNotificationQueryApplet: public WbemQueryApplet {

public:    
    /**
     * Constructor
     */
    WbemNotificationQueryApplet()
    :WbemQueryApplet()
    {
    }


public:
    //Please redefine this method in your own sublcass to inherit this class.
    
    virtual void display(WbemClassObjectEnumerator& enumerator)
    {
        WbemQueryProperties properties = getQueryProperties();

        _bstr_t reportFile = properties.getReportFile();

        FileWriter writer(reportFile);

        _bstr_t query = properties.getQuery();

        writer.write(L"[WbemQuery result for \"%s\"]\n", (const wchar_t*)query);

        LocalDateTime ldt;
        StringT<wchar_t> now;
        writer.write(L"(Generated on %s)\n", ldt.nowToSeconds(now));
        
        int i = 0;
        while (true) {
            ULONG iret = 0;
            try {
                WbemClassObject object = enumerator.next(100, 1, &iret);
                if (iret == 0) {
                    continue;
                }
                
                _bstr_t text = object.getObjectText(0);

                printf("\n\nObjectText No:%d %S\n", i, (const wchar_t*)text);
                
                writer.write(L"\n\nObjectText No:%d %s\n", i, (const wchar_t*)text);
                
            } catch (HRESULT hr) {
                WbemErrors errors;
                printf("WbemQueryApplet::display Exception HRESULT=%x description%s\n",
                    hr, errors.getName(hr));
                //break;

            } catch (Exception ex) {
                ex.printf();
                WbemErrors errors;
                HRESULT hr = ex.getHRESULT();
                printf("WbemNotificationQueryAsyncApplet,Exception:HRESULT=%08x %s", hr, errors.getName(hr));
                //break;
            } catch (...) {
                printf("WbemQueryApplet::display Unknown Exception \n");
                //break;
            }     
        }
    }


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

        MultiThreadedModel   multiThreaded;

        try {
            printf("1 Locator\n");
            WbemLocator locator;
                
            WbemQueryProperties& properties = getQueryProperties();

            _bstr_t server = properties.getServer();
            _bstr_t nspace = properties.getNamespace();
            _bstr_t query  = properties.getQuery();


            printf("2 ConnectServer\n");
            _bstr_t networkPath = L"";
            if (server.length()>0) {
                networkPath = L"\\\\";
                networkPath = networkPath + server;
                networkPath = networkPath + _bstr_t("\\");
                networkPath = networkPath + nspace;
            } else {
                networkPath = (BSTR)nspace;
            }

            WbemServices services = locator.connectServer((BSTR)networkPath);

            printf("3 ExecNotificationQuery '%S'\n", (BSTR)query);
                
            WbemClassObjectEnumerator enumerator =  services.execNotificationQuery((BSTR)query);

            printf("4 Display\n");

            display(enumerator);

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

};

}

Last modified: 1 Apr 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.