SOL9 Sample: ProcessWatcher
|
1 Screenshot
2 Source code
/*
* ProcessWatcher.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// 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/ApartmentThreadedModel.h>
#include <sol\com/MultiThreadedModel.h>
#include <sol\wmi\WbemLocator.h>
#include <sol\wmi\WbemServices.h>
#include <sol\wmi\WbemObjectSink.h>
#pragma comment(lib, "wbemuuid.lib")
namespace SOL {
/**
* Thread of ProcessCreationWatcher
*/
class ProcessWatcher: public Thread {
private:
bool looping;
public:
/**
* Constructor
*/
ProcessWatcher()
:looping(true)
{
}
public:
/**
* Thread procedure to watch an instance creation of Win32_Process.
*/
void run()
{
printf("ProcessWatcher#run,1,started\n");
//ApartmentThreadedModel model;
MultiThreadedModel model;
try {
WbemLocator locator;
WbemServices services = locator.connectServer(L"ROOT\\CIMV2");
WbemObjectSink processSink;
HRESULT hr = S_OK;
const BSTR query = L"SELECT * FROM __InstanceCreationEvent WITHIN 1 "
L"WHERE TargetInstance ISA 'Win32_Process'";
if (SUCCEEDED(hr = services.execNotificationQueryAsync(query,
&processSink))) {
while(looping) {
// The services->execNotificationQueryAsync(...) method will call the virtual
// WbemProcessSink::Indicate method when an event occurs
Sleep(500);
}
}
} catch (HRESULT hr) {
printf("ProcessWatcher#run,2,Exception:%08x", hr);
}
printf("ProcessWatcher#run,3,terminated\n");
}
public:
void stop() {
looping = false;
}
};
}
int _tmain(int argc, TCHAR ** argv)
{
try {
Locale locale;
ProcessWatcher processWatcher;
//Start the thread of ProcessWatcher
processWatcher.start();
//Wait for 30 minutes.
//Sleep(1000*60*30);
//Stop the thread
//processWatcher.stop();
processWatcher.wait();
} catch (...) {
}
return 1;
}
Last modified: 2 May 2016
Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.