Process: Improve processes checking

This fixes checking for 64-bit processes on 32-bit Rainmeter and checking for elevated processes on unelevated Rainmeter.
This commit is contained in:
Birunthan Mohanathas 2013-05-14 16:02:28 +03:00
parent c91f095aef
commit e3ec9371f9

View File

@ -17,7 +17,7 @@
*/ */
#include <windows.h> #include <windows.h>
#include <Psapi.h> #include <TlHelp32.h>
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
#include "../../Library/RawString.h" #include "../../Library/RawString.h"
@ -86,39 +86,25 @@ void CheckProcesses()
(*iter)->isRunning = false; (*iter)->isRunning = false;
} }
int bufSize = 256; HANDLE thSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
DWORD* pids = new DWORD[bufSize]; if (thSnapshot != INVALID_HANDLE_VALUE)
DWORD bytesReturned = 0;
while (!EnumProcesses(pids, bufSize * sizeof(DWORD), &bytesReturned) &&
bytesReturned == bufSize * sizeof(DWORD))
{ {
delete [] pids; PROCESSENTRY32 processEntry = {sizeof(processEntry)};
bufSize *= 2; if (Process32First(thSnapshot, &processEntry))
pids = new DWORD[bufSize]; {
do
{
for (MeasureData* data : g_Measures)
{
if (_wcsicmp(processEntry.szExeFile, data->processName.c_str()) == 0)
{
data->isRunning = true;
}
}
}
while (Process32Next(thSnapshot, &processEntry));
} }
for (UINT i = 0, isize = bytesReturned / sizeof(DWORD); i < isize; ++i) CloseHandle(thSnapshot);
{
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)))
{
iter = g_Measures.begin();
for ( ; iter != g_Measures.end(); ++iter)
{
if (_wcsicmp(buffer, (*iter)->processName.c_str()) == 0)
{
(*iter)->isRunning = true;
} }
} }
}
CloseHandle(hProcess);
}
}
delete [] pids;
}