From e3ec9371f9f31eeabf8843ff015de2350d5916a9 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Tue, 14 May 2013 16:02:28 +0300 Subject: [PATCH] Process: Improve processes checking This fixes checking for 64-bit processes on 32-bit Rainmeter and checking for elevated processes on unelevated Rainmeter. --- Plugins/PluginProcess/PluginProcess.cpp | 38 ++++++++----------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/Plugins/PluginProcess/PluginProcess.cpp b/Plugins/PluginProcess/PluginProcess.cpp index d8802477..a39f7ab0 100644 --- a/Plugins/PluginProcess/PluginProcess.cpp +++ b/Plugins/PluginProcess/PluginProcess.cpp @@ -17,7 +17,7 @@ */ #include -#include +#include #include #include #include "../../Library/RawString.h" @@ -86,39 +86,25 @@ void CheckProcesses() (*iter)->isRunning = false; } - int bufSize = 256; - DWORD* pids = new DWORD[bufSize]; - DWORD bytesReturned = 0; - while (!EnumProcesses(pids, bufSize * sizeof(DWORD), &bytesReturned) && - bytesReturned == bufSize * sizeof(DWORD)) + HANDLE thSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + if (thSnapshot != INVALID_HANDLE_VALUE) { - 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) + PROCESSENTRY32 processEntry = {sizeof(processEntry)}; + if (Process32First(thSnapshot, &processEntry)) { - WCHAR buffer[MAX_PATH]; - if (GetModuleBaseName(hProcess, NULL, buffer, _countof(buffer))) + do { - iter = g_Measures.begin(); - for ( ; iter != g_Measures.end(); ++iter) + for (MeasureData* data : g_Measures) { - if (_wcsicmp(buffer, (*iter)->processName.c_str()) == 0) + if (_wcsicmp(processEntry.szExeFile, data->processName.c_str()) == 0) { - (*iter)->isRunning = true; + data->isRunning = true; } } } - - CloseHandle(hProcess); + while (Process32Next(thSnapshot, &processEntry)); } - } - delete [] pids; + CloseHandle(thSnapshot); + } }