标题：EnumProcesses
简介
检索进程中的每一个进程标识符.
[1]The EnumProcessesfunction retrieves the process identifier for each process object in the system.
语法 c++
BOOL WINAPI EnumProcesses（
_Out_ DWORD * pProcessIds，
_In_ DWORD CB，
_Out_ DWORD * pBytesReturned
）;
参数
EnumProcesses()带三个参数，DWORD 类型的数组指针 lpidProcess；该数组的大小尺寸 cb；以及一个指向 DWORD 的指针 pBytesRrturned，它接收返回数据的长度。DWORD 数组用于保存当前运行的进程 IDs。pBytesRrturned 返回数组所用的内存大小。pProcessIds接收进程标识符的数组.Pointer to an array that receives the list of process identifiers. cb数组的大小.Size of the pProcessIds array, in bytes. pBytesRrturned数组返回的字节数.Number of bytes returned in the pProcessIds array.
返回值
成功返回非零数,失败返回零,可以使用函数 GetLastError获取错误信息.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
备注
下面算式可以得出返回了多少进程：
nReturned = cbNeeded / sizeof(DWORD)。
定义个比较大的数组来接收进程IDs,是一个比较好的选择.虽然文档将返回的 DWORD 命名为“pBytesRrturned”，实际上是没有办法知道到底要传多大的数组的。EnumProcesses()根本不会在 pBytesRrturned 中返回一个大于 cb 参数传递的数组值。结果，唯一确保 EnumProcesses()函数成功的方法是分配一个 DWORD 数组，并且，如果返回的 cbNeeded 等于 cb，分配一个较大的数组，并不停地尝试直到 cbNeeded 小于 cb
It is a good idea to use a large array, because it is hard to predict how many processes there will be at the time you call EnumProcesses. 
To determine how many processes were enumerated, divide the pBytesReturned value by sizeof(DWORD). There is no indication given when the buffer is too small to store all process identifiers. Therefore, if pBytesReturned equals cb, consider retrying the call with a larger array.
To obtain process handles for the processes whose identifiers you have just obtained, call the OpenProcess function.
需求
客户端需求(Client Requires):Windows XP, Windows 2000 Professional, or Windows NT Workstation 4.0. 
服务器需求(Server Requires)Windows Server 2003, Windows 2000 Server, or Windows NT Server 4.0. 
头文件声明在Psapi.h (HeaderDeclared in Psapi.h.)
库中链接到 Psapi.lib (LibraryLink to Psapi.lib.)
DLL名: Psapi.dll (DLLRequires Psapi.dll.)
示例代码
For an example, see Enumerating All Processes or Enumerating All Modules for a Process.
Enumerating All Modules For a Process
To determine which processes have loaded a particular DLL, you must enumerate the modules for each process. The following sample code uses the EnumProcessModules function to enumerate the modules of current processes in the system.
#include <windows.h>
#include <stdio.h>
#include "psapi.h"
void PrintModules( DWORD processID )
{
HMODULE hMods[1024];
HANDLE hProcess;
DWORD cbNeeded;
unsigned int i;
// Print the process identifier.
printf( "\nProcess ID: %u\n", processID );
// Get a list of all the modules in this process.
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, processID );
if (NULL == hProcess)
return;
if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
{
for ( i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
{
char szModName[MAX_PATH];
// Get the full path to the module's file.
if ( GetModuleFileNameEx( hProcess, hMods, szModName, sizeof(szModName)))
{
// Print the module name and handle value.
printf("\t%s (0x%08X)\n", szModName, hMods ) ;
}
}
}
CloseHandle( hProcess );
}
void main( )
{
// Get the list of process identifiers.
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return;
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the name of the modules for each process.
for ( i = 0; i < cProcesses; i++ )
PrintModules( aProcesses);
}
代码解释
The main function obtains a list of processes by using the EnumProcesses function. For each process, the main function calls the PrintModules function, passing it the process identifier. PrintModules in turn calls the OpenProcess function to obtain the process handle. If OpenProcess fails, the output shows only the process identifier. For example, OpenProcess fails for the Idle and CSRSS processes because their access restrictions prevent user-level code from opening them. Next, PrintModules calls the EnumProcessModules function to obtain the module handles function. Finally, PrintModules calls the GetModuleFileNameEx function, once for each module, to obtain the module names.