簡介
檢索進程中的每一個進程標識符.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 返回數組所用的記憶體大小。pProcessId
接收進程標識符的數組.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
#include
#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);
}