rainmeter-studio/Plugins/PluginProcess/PluginProcess.cpp

125 lines
3.2 KiB
C++
Raw Normal View History

2011-12-16 19:53:12 +00:00
/*
Copyright (C) 2011 Birunthan Mohanathas
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2011-12-16 19:53:12 +00:00
*/
#include <windows.h>
#include <Psapi.h>
2012-01-08 17:35:29 +00:00
#include <algorithm>
#include <vector>
#include "../../Library/RawString.h"
2011-12-16 19:53:12 +00:00
#include "../../Library/Export.h" // Rainmeter's exported functions
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
struct MeasureData
{
2012-01-08 17:35:29 +00:00
CRawString processName;
2011-12-16 19:53:12 +00:00
bool isRunning;
MeasureData() : isRunning(false) {}
};
static UINT g_UpdateCount = 0;
2012-01-08 17:35:29 +00:00
static std::vector<MeasureData*> g_Measures;
2011-12-16 19:53:12 +00:00
void CheckProcesses();
PLUGIN_EXPORT void Initialize(void** data, void* rm)
2011-12-16 19:53:12 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = new MeasureData;
g_Measures.push_back(measure);
2011-12-16 19:53:12 +00:00
2012-01-08 17:35:29 +00:00
*data = measure;
2011-12-16 19:53:12 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
2011-12-16 19:53:12 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
2011-12-16 19:53:12 +00:00
2012-01-08 17:35:29 +00:00
LPCWSTR value = RmReadString(rm, L"ProcessName", L"");
measure->processName = value;
2011-12-16 19:53:12 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT double Update(void* data)
2011-12-16 19:53:12 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
// Updates the measure only once per combined updates of all measures
++g_UpdateCount;
if (g_UpdateCount >= g_Measures.size())
2011-12-16 19:53:12 +00:00
{
2012-01-08 17:35:29 +00:00
CheckProcesses();
g_UpdateCount = 0;
2011-12-16 19:53:12 +00:00
}
2012-01-08 17:35:29 +00:00
return measure->isRunning ? 1.0 : -1.0;
2011-12-30 17:18:34 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Finalize(void* data)
2011-12-30 17:18:34 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
std::vector<MeasureData*>::iterator iter = std::find(g_Measures.begin(), g_Measures.end(), measure);
g_Measures.erase(iter);
delete measure;
2011-12-16 19:53:12 +00:00
}
void CheckProcesses()
{
// Set everything to false
2012-01-08 17:35:29 +00:00
std::vector<MeasureData*>::iterator iter = g_Measures.begin();
for ( ; iter != g_Measures.end(); ++iter)
2011-12-16 19:53:12 +00:00
{
2012-01-08 17:35:29 +00:00
(*iter)->isRunning = false;
2011-12-16 19:53:12 +00:00
}
int bufSize = 256;
DWORD* pids = new DWORD[bufSize];
DWORD bytesReturned = 0;
while (!EnumProcesses(pids, bufSize * sizeof(DWORD), &bytesReturned) &&
bytesReturned == bufSize * sizeof(DWORD))
{
delete [] pids;
bufSize *= 2;
pids = new DWORD[bufSize];
}
for (UINT i = 0, isize = bytesReturned / sizeof(DWORD); i < isize; ++i)
{
const DWORD flags = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
HANDLE hProcess = OpenProcess(flags, FALSE, pids[i]);
if (hProcess)
{
WCHAR buffer[MAX_PATH];
if (GetModuleBaseName(hProcess, NULL, buffer, _countof(buffer)))
{
2012-01-08 17:35:29 +00:00
iter = g_Measures.begin();
for ( ; iter != g_Measures.end(); ++iter)
2011-12-16 19:53:12 +00:00
{
2012-01-08 17:35:29 +00:00
if (_wcsicmp(buffer, (*iter)->processName.c_str()) == 0)
2011-12-16 19:53:12 +00:00
{
2012-01-08 17:35:29 +00:00
(*iter)->isRunning = true;
2011-12-16 19:53:12 +00:00
}
}
}
CloseHandle(hProcess);
}
}
delete [] pids;
}