1 /* $Id: process.c,v 1.2 2002/08/29 23:57:53 hyperion Exp $
4 * COPYRIGHT: See COPYING in the top level directory
5 * LICENSE: See LGPL.txt in the top level directory
6 * PROJECT: ReactOS system libraries
7 * FILE: reactos/lib/psapi/enum/process.c
8 * PURPOSE: Enumerate processes
9 * PROGRAMMER: KJK::Hyperion <noog@libero.it>
12 * 29/08/2002: Generalized the interface to improve reusability,
13 * more efficient use of memory operations
17 #include <ddk/ntddk.h>
19 #include <internal/psapi.h>
25 IN PPROC_ENUM_ROUTINE Callback
,
26 IN OUT PVOID CallbackContext
,
27 IN OUT PVOID AllocatorContext
30 register NTSTATUS nErrCode
= STATUS_SUCCESS
;
31 PSYSTEM_PROCESS_INFORMATION pInfoBuffer
= NULL
;
32 PSYSTEM_PROCESS_INFORMATION pInfoHead
= NULL
;
35 /* FIXME: if the system has loaded several processes and threads, the buffer
36 could get really big. But if there's several processes and threads, the
37 system is already under stress, and a huge buffer could only make things
38 worse. The function should be profiled to see what's the average minimum
39 buffer size, to succeed on the first shot */
44 /* free the buffer, and reallocate it to the new size. RATIONALE: since we
45 ignore the buffer's contents at this point, there's no point in a realloc()
46 that could end up copying a large chunk of data we'd discard anyway */
47 PsaFree(AllocatorContext
, pInfoBuffer
);
48 pTmp
= PsaMalloc(AllocatorContext
, nSize
);
53 DPRINT(FAILED_WITH_STATUS
, "malloc", STATUS_NO_MEMORY
);
54 nErrCode
= STATUS_NO_MEMORY
;
60 /* query the information */
61 nErrCode
= NtQuerySystemInformation
63 SystemProcessesAndThreadsInformation
,
69 /* double the buffer size */
72 /* repeat until the buffer is big enough */
73 while(nErrCode
== STATUS_INFO_LENGTH_MISMATCH
);
76 if(!NT_SUCCESS(nErrCode
))
78 DPRINT(FAILED_WITH_STATUS
, "NtQuerySystemInformation", nErrCode
);
83 pInfoHead
= pInfoBuffer
;
88 /* notify the callback */
89 nErrCode
= Callback(pInfoHead
, CallbackContext
);
91 /* if the callback returned an error or this is the end of the process list,
93 if(!NT_SUCCESS(nErrCode
) || pInfoHead
->RelativeOffset
== 0)
96 /* move to the next process */
98 (SYSTEM_PROCESS_INFORMATION
*)
99 ((ULONG
)pInfoHead
+ pInfoHead
->RelativeOffset
);
103 /* free the buffer */
104 PsaFree(AllocatorContext
, pInfoBuffer
);
106 /* return the last status */