SOL9 2.0 Sample: SWbemClassEnumerator

SOL9 2.0 Samples

1 Screenshot




2 Source code

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


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

// 2009/12/23 Modified to accept a namespace from command line
// For example: SWbemClassEnumerator.exe root\Default

#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>
#include <sol\wmi\SWbemObjectPath.h>


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

namespace SOL {

/**
 * Thread of ClassEnumerator 
 */
class SWbemClassEnumerator: public Thread {
private:
    BString nameSpace;

public:    
    /**
     * Constructor
     */
    SWbemClassEnumerator(TCHAR* tnameSpace)
    {
        nameSpace = tnameSpace;
    }


private:
    void enumClasses(SWbemLocator& locator, BSTR nameSpace)
    {
        try {
            SWbemServices services = locator.connectServer(L".", nameSpace);

            SWbemObjectSet objectSet = services.subclassesOf();
            wchar_t reportFile[1024];
            StringT<wchar_t> rnameSpace = nameSpace;
            rnameSpace.replace('\\', '_');
            swprintf(reportFile, L".\\SWbemClassEnumerator_%s_Result.xml", (const wchar_t*)rnameSpace);
            
            FileWriter writer(reportFile);

            writer.write(L"<?xml version=\"1.0\" ?>\n");
            writer.write(L"<SWbemClassEnumeratorResult>\n");

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

            writer.write(L"<Namespace>%s</Namespace>\n", nameSpace);

            long count = objectSet.getCount();

            writer.write(L"<Count>%d</Count>\n", count);
            HTMLEncoder encoder;
            for (long i = 0; i<count; i++) {
                try {
                    SWbemObject object = objectSet.itemIndex(i);
                
                    SWbemObjectPath objectPath = object.getPath(); 
                    BString path = objectPath.getPath();
                    BString className = objectPath.getClass();


                    printf("Path: No=%d  %S Class=%S\n", i, 
                        (const wchar_t*)path, (const wchar_t*)className);
                    writer.write(L"<Class>\n");                        
        
    
                    writer.write(L"<ClassName>%s</ClassName>\n", 
                        (const wchar_t*)className);

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

                    BString displayName = objectPath.getDisplayName();
                    StringT<wchar_t> wname;
                    encoder.encode((const wchar_t*)displayName, wname);
                    writer.write(L"<DisplayName>%s</DisplayName>\n", (const wchar_t*)wname);

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


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

        } catch (HRESULT hr) {
            WbemErrors errors;
            printf("Exception HRESUL=%0x %s\n", hr, errors.getName(hr));
        }
    }

public:
    /**
     * Thread procedure to enumerate all WMI classes under namespace root\cimv2.
     */
    void run()
    {
        ApartmentThreadedModel apartmentThreadedModel;

        try {

            SWbemLocator locator;

            enumClasses(locator, (BSTR)nameSpace);

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

};

}


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

        Locale locale;

        //Default values
        TCHAR* nameSpace = _T("root\\cimv2");

        if (argc== 2) {
            //If specified on command line arguments, we use them
            nameSpace = argv[1];
        }        

        SWbemClassEnumerator enumerator(nameSpace);

        //Start the thread of ClassEnumerator
        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: 24 Dec 2009

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