mirror of
https://github.com/chibicitiberiu/rainmeter-studio.git
synced 2024-02-24 04:33:31 +00:00
PerfMon.dll: Updated to new API
This commit is contained in:
parent
f51f2aac24
commit
51cddb00f8
@ -17,179 +17,108 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include "PerfData.h"
|
#include <vector>
|
||||||
#include <string>
|
#include "Titledb.h"
|
||||||
#include <map>
|
#include "PerfSnap.h"
|
||||||
#include <crtdbg.h>
|
#include "PerfObj.h"
|
||||||
#include "../../Library/Export.h" // Rainmeter's exported functions
|
#include "PerfCntr.h"
|
||||||
|
#include "ObjList.h"
|
||||||
|
#include "ObjInst.h"
|
||||||
|
#include "../API/RainmeterAPI.h"
|
||||||
|
#include "../../Library/RawString.h"
|
||||||
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
|
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
|
||||||
|
|
||||||
|
struct MeasureData
|
||||||
|
{
|
||||||
|
CRawString objectName;
|
||||||
|
CRawString counterName;
|
||||||
|
CRawString instanceName;
|
||||||
|
ULONGLONG oldValue;
|
||||||
|
bool difference;
|
||||||
|
bool firstTime;
|
||||||
|
};
|
||||||
|
|
||||||
ULONGLONG GetPerfData(PCTSTR ObjectName, PCTSTR InstanceName, PCTSTR CounterName);
|
ULONGLONG GetPerfData(PCTSTR ObjectName, PCTSTR InstanceName, PCTSTR CounterName);
|
||||||
|
|
||||||
struct PerfMeasure
|
PLUGIN_EXPORT void Initialize(void** data, void* rm)
|
||||||
{
|
{
|
||||||
std::wstring ObjectName;
|
MeasureData* measure = new MeasureData;
|
||||||
std::wstring CounterName;
|
*data = measure;
|
||||||
std::wstring InstanceName;
|
|
||||||
bool Difference;
|
|
||||||
ULONGLONG OldValue;
|
|
||||||
bool FirstTime;
|
|
||||||
};
|
|
||||||
|
|
||||||
static CPerfTitleDatabase g_CounterTitles( PERF_TITLE_COUNTER );
|
|
||||||
static std::map<UINT, PerfMeasure*> g_Measures;
|
|
||||||
|
|
||||||
/*
|
|
||||||
This function is called when the measure is initialized.
|
|
||||||
The function must return the maximum value that can be measured.
|
|
||||||
The return value can also be 0, which means that Rainmeter will
|
|
||||||
track the maximum value automatically. The parameters for this
|
|
||||||
function are:
|
|
||||||
|
|
||||||
instance The instance of this DLL
|
|
||||||
iniFile The name of the ini-file (usually Rainmeter.ini)
|
|
||||||
section The name of the section in the ini-file for this measure
|
|
||||||
id The identifier for the measure. This is used to identify the measures that use the same plugin.
|
|
||||||
*/
|
|
||||||
UINT Initialize(HMODULE instance, LPCTSTR iniFile, LPCTSTR section, UINT id)
|
|
||||||
{
|
|
||||||
PerfMeasure* measure = new PerfMeasure;
|
|
||||||
measure->Difference = false;
|
|
||||||
measure->FirstTime = true;
|
|
||||||
measure->OldValue = 0;
|
|
||||||
|
|
||||||
LPCTSTR buffer = ReadConfigString(section, L"PerfMonObject", L"");
|
|
||||||
if (buffer)
|
|
||||||
{
|
|
||||||
measure->ObjectName = buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer = ReadConfigString(section, L"PerfMonCounter", L"");
|
|
||||||
if (buffer)
|
|
||||||
{
|
|
||||||
measure->CounterName = buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer = ReadConfigString(section, L"PerfMonInstance", L"");
|
|
||||||
if (buffer)
|
|
||||||
{
|
|
||||||
measure->InstanceName = buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer = ReadConfigString(section, L"PerfMonDifference", L"1");
|
|
||||||
if (buffer)
|
|
||||||
{
|
|
||||||
measure->Difference = 1==_wtoi(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store the measure
|
|
||||||
g_Measures[id] = measure;
|
|
||||||
|
|
||||||
int maxVal = 0;
|
|
||||||
buffer = ReadConfigString(section, L"PerfMonMaxValue", L"0");
|
|
||||||
if (buffer)
|
|
||||||
{
|
|
||||||
maxVal = _wtoi(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
return maxVal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
|
||||||
This function is called when new value should be measured.
|
|
||||||
The function returns the new value.
|
|
||||||
*/
|
|
||||||
double Update2(UINT id)
|
|
||||||
{
|
{
|
||||||
|
MeasureData* measure = (MeasureData*)data;
|
||||||
|
|
||||||
|
measure->objectName = RmReadString(rm, L"PerfMonObject", L"");
|
||||||
|
measure->counterName = RmReadString(rm, L"PerfMonCounter", L"");
|
||||||
|
measure->instanceName = RmReadString(rm, L"PerfMonInstance", L"");
|
||||||
|
measure->difference = RmReadInt(rm, L"PerfMonDifference", 1) == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
PLUGIN_EXPORT double Update(void* data)
|
||||||
|
{
|
||||||
|
MeasureData* measure = (MeasureData*)data;
|
||||||
double value = 0;
|
double value = 0;
|
||||||
|
|
||||||
std::map<UINT, PerfMeasure*>::iterator i = g_Measures.find(id);
|
ULONGLONG longValue = GetPerfData(
|
||||||
if (i != g_Measures.end())
|
measure->objectName.c_str(),
|
||||||
|
measure->instanceName.c_str(),
|
||||||
|
measure->counterName.c_str());
|
||||||
|
|
||||||
|
if (measure->difference)
|
||||||
{
|
{
|
||||||
PerfMeasure* measure = (*i).second;
|
// Compare with the old value
|
||||||
|
if (!measure->firstTime)
|
||||||
if (measure)
|
|
||||||
{
|
{
|
||||||
ULONGLONG longvalue;
|
value = (double)(longValue - measure->oldValue);
|
||||||
longvalue = GetPerfData(measure->ObjectName.c_str(),
|
|
||||||
measure->InstanceName.c_str(),
|
|
||||||
measure->CounterName.c_str());
|
|
||||||
|
|
||||||
if (measure->Difference)
|
|
||||||
{
|
|
||||||
// Compare with the old value
|
|
||||||
if (!measure->FirstTime)
|
|
||||||
{
|
|
||||||
value = (double)(longvalue - measure->OldValue);
|
|
||||||
}
|
|
||||||
measure->OldValue = longvalue;
|
|
||||||
measure->FirstTime = false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
value = (double)longvalue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
measure->oldValue = longValue;
|
||||||
|
measure->firstTime = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value = (double)longValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
PLUGIN_EXPORT void Finalize(void* data)
|
||||||
If the measure needs to free resources before quitting.
|
|
||||||
The plugin can export Finalize function, which is called
|
|
||||||
when Rainmeter quits (or refreshes).
|
|
||||||
*/
|
|
||||||
void Finalize(HMODULE instance, UINT id)
|
|
||||||
{
|
{
|
||||||
// delete the measure
|
MeasureData* measure = (MeasureData*)data;
|
||||||
std::map<UINT, PerfMeasure*>::iterator i = g_Measures.find(id);
|
delete measure;
|
||||||
if (i != g_Measures.end())
|
|
||||||
{
|
|
||||||
delete (*i).second;
|
|
||||||
g_Measures.erase(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
CPerfSnapshot::CleanUp();
|
CPerfSnapshot::CleanUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
ULONGLONG GetPerfData(LPCWSTR objectName, LPCWSTR instanceName, LPCWSTR counterName)
|
||||||
This method gets value of the given perfmon counter.
|
|
||||||
*/
|
|
||||||
ULONGLONG GetPerfData(PCTSTR ObjectName, PCTSTR InstanceName, PCTSTR CounterName)
|
|
||||||
{
|
{
|
||||||
CPerfObject* pPerfObj;
|
static CPerfTitleDatabase s_CounterTitles(PERF_TITLE_COUNTER);
|
||||||
CPerfObjectInstance* pObjInst;
|
|
||||||
CPerfCounter* pPerfCntr;
|
|
||||||
BYTE data[256];
|
BYTE data[256];
|
||||||
WCHAR name[256];
|
WCHAR name[256];
|
||||||
ULONGLONG value = 0;
|
ULONGLONG value = 0;
|
||||||
|
|
||||||
if (ObjectName == NULL || CounterName == NULL || wcslen(ObjectName) == 0 || wcslen(CounterName) == 0)
|
CPerfSnapshot snapshot(&s_CounterTitles);
|
||||||
{
|
CPerfObjectList objList(&snapshot, &s_CounterTitles);
|
||||||
// Unable to continue
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
CPerfSnapshot snapshot(&g_CounterTitles);
|
if (snapshot.TakeSnapshot(objectName))
|
||||||
CPerfObjectList objList(&snapshot, &g_CounterTitles);
|
|
||||||
|
|
||||||
if (snapshot.TakeSnapshot(ObjectName))
|
|
||||||
{
|
{
|
||||||
pPerfObj = objList.GetPerfObject(ObjectName);
|
CPerfObject* pPerfObj = objList.GetPerfObject(objectName);
|
||||||
|
|
||||||
if (pPerfObj)
|
if (pPerfObj)
|
||||||
{
|
{
|
||||||
for (pObjInst = pPerfObj->GetFirstObjectInstance();
|
for (CPerfObjectInstance* pObjInst = pPerfObj->GetFirstObjectInstance();
|
||||||
pObjInst != NULL;
|
pObjInst != NULL;
|
||||||
pObjInst = pPerfObj->GetNextObjectInstance())
|
pObjInst = pPerfObj->GetNextObjectInstance())
|
||||||
{
|
{
|
||||||
if (InstanceName != NULL && wcslen(InstanceName) > 0)
|
if (*instanceName)
|
||||||
{
|
{
|
||||||
if (pObjInst->GetObjectInstanceName(name, 256))
|
if (pObjInst->GetObjectInstanceName(name, 256))
|
||||||
{
|
{
|
||||||
if (_wcsicmp(InstanceName, name) != 0)
|
if (_wcsicmp(instanceName, name) != 0)
|
||||||
{
|
{
|
||||||
delete pObjInst;
|
delete pObjInst;
|
||||||
continue;
|
continue;
|
||||||
@ -202,7 +131,7 @@ ULONGLONG GetPerfData(PCTSTR ObjectName, PCTSTR InstanceName, PCTSTR CounterName
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pPerfCntr = pObjInst->GetCounterByName(CounterName);
|
CPerfCounter* pPerfCntr = pObjInst->GetCounterByName(counterName);
|
||||||
if (pPerfCntr != NULL)
|
if (pPerfCntr != NULL)
|
||||||
{
|
{
|
||||||
pPerfCntr->GetData(data, 256, NULL);
|
pPerfCntr->GetData(data, 256, NULL);
|
||||||
@ -228,23 +157,13 @@ ULONGLONG GetPerfData(PCTSTR ObjectName, PCTSTR InstanceName, PCTSTR CounterName
|
|||||||
delete pObjInst;
|
delete pObjInst;
|
||||||
break; // No need to continue
|
break; // No need to continue
|
||||||
}
|
}
|
||||||
|
|
||||||
delete pObjInst;
|
delete pObjInst;
|
||||||
}
|
}
|
||||||
|
|
||||||
delete pPerfObj;
|
delete pPerfObj;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
UINT GetPluginVersion()
|
|
||||||
{
|
|
||||||
return 1002;
|
|
||||||
}
|
|
||||||
|
|
||||||
LPCTSTR GetPluginAuthor()
|
|
||||||
{
|
|
||||||
return L"Rainy (rainy@iki.fi)";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user