EnumProcesses

EnumProcesses函式主要功能得到一系列過程採用EnumProcesses功能。為每個過程、主要功能調用PrintModules功能,通過工藝標識符。PrintModules反過來呼叫OpenProcess功能得到過程處理。 如果OpenProcess失敗,只有過程輸出顯示標識符。OpenProcess閒置,或者因為他們的準入限制CSRSS過程防止用戶級代碼從打開它們。其次,PrintModules稱EnumProcessModules功能模組處理獲得的功能。最後,PrintModules稱GetModuleFileNameEx功能,對每一個模組進行一次,取得模組的名字。

基本介紹

  • 外文名:EnumProcesses
  • 類別:函式
  • 功能:得到一系列EnumProcesses功能
  • 作用:為每個過程通過工藝標識符
介紹,c++語法,參數,返回值,備註,需求,示例代碼,代碼解釋,

介紹

檢索進程中的每一個進程標識符.
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.
pBytesReturned
數組返回的位元組數.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[i]);}

代碼解釋

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.

相關詞條

熱門詞條

聯絡我們