summaryrefslogtreecommitdiff
path: root/sal/systools/win32/uwinapi/EnumProcesses.cpp
blob: a8b5fabef45f4f679f2852fe92578a182963818f (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include "macros.h"
#include <tlhelp32.h>

IMPLEMENT_THUNK( psapi, WINDOWS, BOOL, WINAPI, EnumProcesses, ( LPDWORD lpProcesses, DWORD cbSize, LPDWORD lpcbCopied ) )
{
    BOOL	fSuccess = FALSE;
    HANDLE	hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

    if ( IsValidHandle( hSnapshot ) )
    {
        PROCESSENTRY32	pe;

        if ( lpcbCopied )
            *lpcbCopied = 0;

        pe.dwSize = sizeof(pe);
        if ( Process32First( hSnapshot, &pe ) )
        {
            fSuccess = TRUE;

            while ( cbSize >= sizeof(*lpProcesses) )
            {
                *(lpProcesses++) = pe.th32ProcessID;
                if ( lpcbCopied )
                    *lpcbCopied += sizeof(*lpProcesses);
                cbSize -= sizeof(*lpProcesses);

                if ( !Process32Next( hSnapshot, &pe ) )
                    break;
            }
        }

        CloseHandle( hSnapshot );
    }
    else
        SetLastError( ERROR_INVALID_HANDLE );
    
    return fSuccess;
}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */