SOL9 2.0 Sample: SWbemQueryAsAdminApplet

SOL9 2.0 Samples

1 Screenshot




2 Source code

/*
 * SWbemQueryAsAdminApplet.cpp 
 * Copyright (c) 2009 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2009/12/23 Modified to save query results to a text file ".\SWbemQueryAsAdminResult.txt"
// 

// This application need the following Query.Properties file:
// If needed, please change WQL Query property to  

/* 
; SWbemQueryAsAdminApplet.Query.properties

Server    = .
Namespace = root\cimv2

; If LogFile were 'Application', OK.
;Query    = SELECT * FROM Win32_NTLogEvent Where LogFile='Application' AND TimeWritten > '20091214163612.000000-000'

; But, how about LogFile were 'Security'? Probably you need a right of the administrator, namely a manifest file to allow it.
Query    = SELECT * FROM Win32_NTLogEvent Where LogFile='Security' AND TimeWritten > '20091201000000.000000-000'
*/

#include <sol/wmi/SWbemQueryApplet.h>

namespace SOL {

class SWbemQueryAsAdminApplet: public SWbemQueryApplet {

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

public:
    virtual void display(SWbemObjectSet& objectSet)
    {
        SWbemQueryProperties& properties = getQueryProperties();

        BString query = properties.getQuery();

        printf("ExecQuery %S\n", (const wchar_t*)query); 
        
        const wchar_t* reportFile = L".\\SWbemQueryAsAdminResult.txt";

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

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

        long count = objectSet.getCount();

        writer.write(L"<ResultSet Count=\"%d\" />\n", count);
                    
                
        for (long i = 0; i<count; i++) {
            try {
                SWbemObject object = objectSet.itemIndex(i);
                BString text = object.getObjectText(0);
                //printf("\n\nNo:%d ObjectText=%S\n", i, (const wchar_t*)text);

                writer.write(L"<ObjectText No=\"%d\">\n%s\n</ObjectText>\n\n", 
                    i, (const wchar_t*)text);
            

            } catch (HRESULT hr) {
                WbemErrors errors;
                printf("Exception: HRESULT=%0x %s\n", hr, errors.getName(hr));
            } catch (...) {
                printf("Unknown Exception\n");
            }
        }
        printf("Saved qurey reult to a file %S\n", reportFile);
    }

};

}


void _tmain(int argc, const TCHAR ** argv)
{
    printf("SWbemQueryAsAdminApplet started\n");

    try {
        Locale locale;

        SWbemQueryAsAdminApplet applet;

        
        //Start the thread of ServiceQuery
        applet.start();

        applet.wait();

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

    printf("\nOK. Please hit return key\n");
    getchar();
}


Last modified: 24 Dec 2009

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