summaryrefslogtreecommitdiff
path: root/plugins/Variables/src/enumprocs.cpp
blob: ebe665c8174ff3a6db2986bafb0a6ff8f6f5f89b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
	Variables Plugin for Miranda-IM (www.miranda-im.org)
	Copyright 2003-2006 P. Boon

	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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "stdafx.h"

struct EnumInfoStruct
{
	DWORD          dwPID;
	PROCENUMPROC   lpProc;
	DWORD          lParam;
	BOOL           bEnd;
};

// The EnumProcs function takes a pointer to a callback function
// that will be called once per process with the process filename
// and process ID.
//
// lpProc -- Address of callback routine.
//
// lParam -- A user-defined LPARAM value to be passed to
//           the callback routine.
//
// Callback function definition:
// BOOL CALLBACK Proc(DWORD dw, WORD w, LPCSTR lpstr, LPARAM lParam);

BOOL WINAPI EnumProcs(PROCENUMPROC lpProc, LPARAM lParam)
{
	// Retrieve the OS version
	OSVERSIONINFO  osver;
	osver.dwOSVersionInfoSize = sizeof(osver);
	if (!GetVersionEx(&osver))
		return FALSE;

	if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS || (osver.dwPlatformId == VER_PLATFORM_WIN32_NT && osver.dwMajorVersion > 4)) {
		// Get a handle to a Toolhelp snapshot of all processes.
		HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
		if (hSnapShot == INVALID_HANDLE_VALUE)
			return FALSE;

		// Get the first process' information.
		PROCESSENTRY32 procentry;
		procentry.dwSize = sizeof(PROCESSENTRY32);
		BOOL bFlag = Process32First(hSnapShot, &procentry);

		// While there are processes, keep looping.
		while (bFlag) {
			// Call the enum func with the filename and ProcID.
			if (lpProc(procentry.th32ProcessID, 0, (char *)procentry.szExeFile, lParam)) {
				procentry.dwSize = sizeof(PROCESSENTRY32);
				bFlag = Process32Next(hSnapShot, &procentry);

			}
			else bFlag = FALSE;
		}
	}
	else return FALSE;

	return TRUE;
}