/*
* PerformanceCounter.h
* Copyright (c) 2009 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// SOL9
// 2009/09/25
#pragma once
#include <sol/pdh/PdhObject.h>
#include <sol/pdh/PerformanceCounterInfo.h>
namespace SOL {
class PerformanceCounter :public PdhObject {
private:
HCOUNTER hCounter;
public:
PerformanceCounter()
:hCounter(NULL)
{
}
public:
~PerformanceCounter()
{
if (hCounter) {
PdhRemoveCounter(hCounter);
hCounter = NULL;
}
}
public:
void setCounter(HCOUNTER hc) {
hCounter = hc;
}
public:
HCOUNTER getCounter() {
return hCounter;
}
public:
operator HCOUNTER() const{
return hCounter;
}
public:
PDH_STATUS getFormattedValue(__out String& string)
{
/*
typedef struct _PDH_FMT_COUNTERVALUE {
DWORD CStatus;
union {
LONG longValue;
double doubleValue;
LONGLONG largeValue;
LPCSTR AnsiStringValue; //not supported (From MSDN)
LPCWSTR WideStringValue; //not supported (From MSDN)
} ;
}PDH_FMT_COUNTERVALUE, *PPDH_FMT_COUNTERVALUE;
*/
string = "";
PDH_FMT_COUNTERVALUE value;
memset(&value, 0, sizeof(value));
PerformanceCounterInfo info(getCounter());
String sizeName;
DWORD size;
info.getDataSize(size, sizeName);
DWORD sizeType = PDH_FMT_LONG;
if (size == PERF_SIZE_DWORD){
sizeType = PDH_FMT_LONG;
}
if (size == PERF_SIZE_LARGE){
sizeType = PDH_FMT_LARGE;
}
char buffer[MAX_PATH];
memset(buffer, 0, sizeof(buffer));
PDH_STATUS status = PdhGetFormattedCounterValue(hCounter, sizeType,
//PDH_FMT_LONG,
//PDH_FMT_LARGE,
//|PDH_FMT_NOSCALE,
NULL, &value);
if (status == ERROR_SUCCESS) {
if (sizeType == PDH_FMT_LONG) {
sprintf(buffer, "%lu", value.longValue);
}
if (sizeType == PDH_FMT_LARGE) {
sprintf(buffer, "%I64d", value.largeValue);
}
string = buffer;
} else {
throw (int)status;
}
return status;
}
public:
PDH_STATUS getFormattedValue(__out PDH_FMT_COUNTERVALUE& value,
__in DWORD type=PDH_FMT_LONG)
{
memset(&value, 0, sizeof(value));
PDH_STATUS status = PdhGetFormattedCounterValue(hCounter, type,
NULL, &value);
if (status != ERROR_SUCCESS) {
throw (int)status;
}
return status;
}
};
}
|