/*
* WbemQueryApplet.h
* Copyright (c) 2012 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// SOL9
// 2010/02/24
// 2012/03/09 Updated.
#pragma once
#include <sol/Locale.h>
#include <sol/Exception.h>
#include <sol/Thread.h>
#include <sol/com/ApartmentThreadedModel.h>
#include <sol/com/MultiThreadedModel.h>
#include <sol/wmi/WbemQueryProperties.h>
#include <sol/wmi/WbemQueryPropertiesLoader.h>
//2010/05/08
#include <sol/wmi/WbemClassObject.h>
#include <sol/wmi/WbemLocator.h>
#include <sol/wmi/WbemServices.h>
#include <sol/wmi/WbemClassObjectEnumerator.h>
#include <sol/LocalDateTime.h>
#include <sol/wmi/WbemErrors.h>
namespace SOL {
/**
* Thread for WbemServices::ExecQuery method call
*/
class WbemQueryApplet: public Thread {
private:
WbemQueryProperties properties;
public:
/**
* Constructor
*/
WbemQueryApplet()
{
WbemQueryPropertiesLoader loader;
loader.load(properties);
}
public:
//Redefine this method in your own sublcass to inherit this class.
virtual void display(WbemClassObjectEnumerator& enumerator)
{
_bstr_t reportFile = properties.getReportFile();
FileWriter writer(reportFile);
_bstr_t query = properties.getQuery();
writer.writeln(L"[WbemQuery result for \"%s\"]", (const wchar_t*)query);
LocalDateTime ldt;
StringT<wchar_t> now;
writer.writeln(L"(Generated on %s)", ldt.nowToSeconds(now));
int i = 0;
while (true) {
ULONG iret = 0;
try {
WbemClassObject object = enumerator.next(0, 1, &iret);
if (iret == 0) {
break;
}
_bstr_t text = object.getObjectText(0);
printf("\n\nObjectText No:%d %S\n", i, (const wchar_t*)text);
writer.writeln(L"\r\n\r\nObjectText No:%d %s", 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:
WbemQueryProperties& getQueryProperties()
{
return properties;
}
public:
/**
* Thread procedure to watch an instance creation of Win32_Process.
*/
virtual void run()
{
//ApartmentThreadedModel apartmentThreaded;
MultiThreadedModel multiThreaded;
try {
WbemLocator locator;
_bstr_t server = properties.getServer();
_bstr_t nspace = properties.getNamespace();
_bstr_t query = properties.getQuery();
_bstr_t networkPath = L"";
if (server.length()>0) {
networkPath = L"\\\\";
networkPath = networkPath + server;
networkPath = networkPath + _bstr_t("\\");
networkPath = networkPath + nspace;
} else {
networkPath = (BSTR)nspace;
}
printf("NetworkPath='%S'\n", (const wchar_t*)networkPath);
WbemServices services = locator.connectServer((BSTR)networkPath);
printf("Query='%S'\n", (const wchar_t*)query);
WbemClassObjectEnumerator enumerator = services.execQuery((BSTR)query);
display(enumerator);
} catch (HRESULT hr) {
WbemErrors errors;
printf("WbemQueryApplet,Exception:HRESULT=%08x %s", hr, errors.getName(hr));
} catch (Exception ex) {
ex.printf();
} catch (...) {
printf("WbemQueryApplet,Unknown Exception\n");
}
}
};
}
|