rainmeter-studio/Plugins/PluginResMon/ResMon.cpp

191 lines
4.5 KiB
C++
Raw Normal View History

2009-02-10 18:37:48 +00:00
/*
Copyright (C) 2004 David Negstad
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.
2009-02-10 18:37:48 +00:00
*/
/*
Requires Win2k or later. Sorry to anyone still using 9x or ME.
All my results seem to agree with Task Manager (it gets tedious adding up
the different process values). Already proved useful in identifying
resource leaks in itself (all fixed). There's irony for you!
*/
#define _WIN32_WINNT 0x0501
#include <windows.h>
2012-01-08 17:35:29 +00:00
#include <stdio.h>
2009-02-10 18:37:48 +00:00
#include <psapi.h>
2012-01-08 17:35:29 +00:00
#include "../../Library/RawString.h"
2011-02-03 18:09:24 +00:00
#include "../../Library/Export.h" // Rainmeter's exported functions
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
2009-02-10 18:37:48 +00:00
// system resources that can be counted
2012-01-08 17:35:29 +00:00
enum MEASURETYPE
2009-02-10 18:37:48 +00:00
{
GDI_COUNT,
USER_COUNT,
HANDLE_COUNT,
WINDOW_COUNT
};
2012-01-08 17:35:29 +00:00
struct MeasureData
{
MEASURETYPE type;
CRawString process;
MeasureData() : type(GDI_COUNT) {}
};
2009-02-10 18:37:48 +00:00
// used to track the number of existing windows
UINT g_WindowCount = 0;
2012-01-08 17:35:29 +00:00
// count the windows
BOOL CALLBACK EnumWindowProc(HWND hWnd, LPARAM lParam)
2009-02-10 18:37:48 +00:00
{
++g_WindowCount;
return TRUE;
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Initialize(void** data)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = new MeasureData;
*data = measure;
2009-02-10 18:37:48 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
LPCWSTR type = RmReadString(rm, L"ResCountType", L"GDI");
if (_wcsicmp(L"GDI", type) == 0)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
measure->type = GDI_COUNT;
2009-02-10 18:37:48 +00:00
}
2012-01-08 17:35:29 +00:00
else if (_wcsicmp(L"USER", type) == 0)
{
measure->type = USER_COUNT;
}
else if (_wcsicmp(L"HANDLE", type) == 0)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
measure->type = HANDLE_COUNT;
}
else if (_wcsicmp(L"WINDOW", type) == 0)
{
measure->type = WINDOW_COUNT;
}
else
{
WCHAR buffer[256];
_snwprintf_s(buffer, _TRUNCATE, L"ResMon.dll: GDICountType=%s is not valid in [%s]", type, RmGetMeasureName(rm));
RmLog(LOG_ERROR, buffer);
2009-02-10 18:37:48 +00:00
}
2012-01-08 17:35:29 +00:00
measure->process = RmReadString(rm, L"ProcessName", L"");
2009-02-10 18:37:48 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT double Update(void* data)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
2009-02-10 18:37:48 +00:00
// count the existing window objects
2012-01-08 17:35:29 +00:00
if (measure->type == WINDOW_COUNT)
2009-02-10 18:37:48 +00:00
{
g_WindowCount = 0;
2012-01-08 17:35:29 +00:00
EnumChildWindows(NULL, EnumWindowProc, 0);
2009-02-10 18:37:48 +00:00
return g_WindowCount;
}
2012-01-08 17:35:29 +00:00
const WCHAR* processName = measure->process.c_str();
bool name = !measure->process.empty();
2009-02-10 18:37:48 +00:00
DWORD aProcesses[1024];
DWORD bytesNeeded;
WCHAR buffer[1024];
HMODULE hMod[1024];
2012-01-08 17:35:29 +00:00
DWORD cbNeeded;
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &bytesNeeded))
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
return 0.0;
2009-02-10 18:37:48 +00:00
}
// step through the running processes
DWORD flags = PROCESS_QUERY_INFORMATION;
2012-01-08 17:35:29 +00:00
if (name)
2009-02-10 18:37:48 +00:00
{
flags |= PROCESS_VM_READ;
}
UINT resourceCount = 0;
2012-01-08 17:35:29 +00:00
for (UINT i = 0, isize = bytesNeeded / sizeof(DWORD); i < isize; ++i)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
HANDLE hProcess = OpenProcess(flags, true, aProcesses[i]);
if (hProcess != NULL)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
if (name)
2009-02-10 18:37:48 +00:00
{
2011-03-29 19:21:57 +00:00
if (EnumProcessModules(hProcess, hMod, sizeof(hMod), &cbNeeded))
2009-02-10 18:37:48 +00:00
{
if (GetModuleBaseName(hProcess, hMod[0], buffer, sizeof(buffer)))
{
2010-09-17 08:47:22 +00:00
if (_wcsicmp(buffer, processName) != 0)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
CloseHandle(hProcess);
2009-02-10 18:37:48 +00:00
continue;
}
}
else
{
2012-01-08 17:35:29 +00:00
CloseHandle(hProcess);
2009-02-10 18:37:48 +00:00
continue;
}
}
else
{
2012-01-08 17:35:29 +00:00
CloseHandle(hProcess);
2009-02-10 18:37:48 +00:00
continue;
}
}
2012-01-08 17:35:29 +00:00
if (measure->type == GDI_COUNT)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
resourceCount += GetGuiResources(hProcess, GR_GDIOBJECTS);
2009-02-10 18:37:48 +00:00
}
2012-01-08 17:35:29 +00:00
else if (measure->type == USER_COUNT)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
resourceCount += GetGuiResources(hProcess, GR_USEROBJECTS);
2009-02-10 18:37:48 +00:00
}
2012-01-08 17:35:29 +00:00
else if (measure->type == HANDLE_COUNT)
2009-02-10 18:37:48 +00:00
{
DWORD tempHandleCount = 0;
2012-01-08 17:35:29 +00:00
GetProcessHandleCount(hProcess, &tempHandleCount);
2009-02-10 18:37:48 +00:00
resourceCount += tempHandleCount;
}
}
2012-01-08 17:35:29 +00:00
CloseHandle(hProcess);
2009-02-10 18:37:48 +00:00
}
return resourceCount;
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Finalize(void* data)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
delete measure;
2009-02-10 18:37:48 +00:00
}