xref: /AOO41X/main/sal/systools/win32/uwinapi/EnumProcesses.cpp (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir #include "macros.h"
2*cdf0e10cSrcweir #include <tlhelp32.h>
3*cdf0e10cSrcweir 
4*cdf0e10cSrcweir IMPLEMENT_THUNK( psapi, WINDOWS, BOOL, WINAPI, EnumProcesses, ( LPDWORD lpProcesses, DWORD cbSize, LPDWORD lpcbCopied ) )
5*cdf0e10cSrcweir {
6*cdf0e10cSrcweir 	BOOL	fSuccess = FALSE;
7*cdf0e10cSrcweir 	HANDLE	hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
8*cdf0e10cSrcweir 
9*cdf0e10cSrcweir 	if ( IsValidHandle( hSnapshot ) )
10*cdf0e10cSrcweir 	{
11*cdf0e10cSrcweir 		PROCESSENTRY32	pe;
12*cdf0e10cSrcweir 
13*cdf0e10cSrcweir 		if ( lpcbCopied )
14*cdf0e10cSrcweir 			*lpcbCopied = 0;
15*cdf0e10cSrcweir 
16*cdf0e10cSrcweir 		pe.dwSize = sizeof(pe);
17*cdf0e10cSrcweir 		if ( Process32First( hSnapshot, &pe ) )
18*cdf0e10cSrcweir 		{
19*cdf0e10cSrcweir 			fSuccess = TRUE;
20*cdf0e10cSrcweir 
21*cdf0e10cSrcweir 			while ( cbSize >= sizeof(*lpProcesses) )
22*cdf0e10cSrcweir 			{
23*cdf0e10cSrcweir 				*(lpProcesses++) = pe.th32ProcessID;
24*cdf0e10cSrcweir 				if ( lpcbCopied )
25*cdf0e10cSrcweir 					*lpcbCopied += sizeof(*lpProcesses);
26*cdf0e10cSrcweir 				cbSize -= sizeof(*lpProcesses);
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir 				if ( !Process32Next( hSnapshot, &pe ) )
29*cdf0e10cSrcweir 					break;
30*cdf0e10cSrcweir 			}
31*cdf0e10cSrcweir 		}
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir 		CloseHandle( hSnapshot );
34*cdf0e10cSrcweir 	}
35*cdf0e10cSrcweir 	else
36*cdf0e10cSrcweir 		SetLastError( ERROR_INVALID_HANDLE );
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir 	return fSuccess;
39*cdf0e10cSrcweir }
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 
42