1. The function can retrieve the identifier (process ID) of each process in the system
BOOL WINAPI enumprocesses (
_out_ DWORD *pprocessids, _in_ DWORD CB, _out_ DWORD *pbytesreturned);
CB is the number of bytes of memory space that pprocessids points to, (*pbytesreturned)/sizeof (DWORD) is the number of process IDs returned in Pprocessids.
2. Retrieving the handle of each module in the development process
BOOL WINAPI EnumProcessModules (
_in_ HANDLE hprocess,
_out_ hmodule *lphmodule,
_in_ DWORD CB,
_out_ Lpdword lpcbneeded
);
Hprocess: Process Handle
Lphmodule: Array that receives a module handle
Number of bytes in the Cb:lphmodule array
Lpcbneeded: The number of bytes required to store all the module handles in the Lphmodule array (you can call the function by setting the Lphmodule to NULL,CB to 0 first. Then use the number of bytes stored in the lpcbneeded to allocate lphmodule space, and then call EnumProcessModules again)
3. Retrieving the base name of the developed module
DWORD WINAPI GetModuleBaseName (
_in_ HANDLE hprocess,
_in_opt_ hmodule hmodule,
_out_ LPTSTR Lpbasename,
_in_ DWORD NSize
);
Hprocess: Handle to the process that contains the Module
Hmodule: The handle of the module to retrieve, and if the parameter is NULL, the function returns the name of the process that called the function
Lpbasename: The base name is truncated if the Buffer;buffer space used to receive the module name is not enough
Space size of Nsize:lpbasename buffer
Return value: The length of the string in copy to Lpbasename is returned if the call succeeds; 0 is returned if the call fails
Sample program:
#include <iostream>#include<Windows.h>#include<Psapi.h>using namespacestd;voidPrintprocessnameandid (DWORD processID);intMainintargcChar*argv[]) { //get a list of process identifiersDWORD aprocesses[1024x768], cbneeded, cprocesses; unsignedinti; if(! EnumProcesses (Aprocesses,sizeof(aprocesses), &cbneeded)) return 1; //calculates the number of returned process identifiersCprocesses = cbneeded/sizeof(DWORD); for(i =0; i < cprocesses; i++) {Printprocessnameandid (aprocesses[i]); } System ("Pause"); return 0;}voidPrintprocessnameandid (DWORD processID) {CharSzprocessname[max_path] ="Unknown"; //Get process handleHANDLE hprocess = openprocess (process_query_information |Process_vm_read, FALSE, ProcessID); if(NULL! =hprocess) {hmodule hmod; DWORD cbneeded; if(EnumProcessModules (hprocess, &hmod,sizeof(Hmod), &cbneeded)) {GetModuleBaseName (hprocess, Hmod, szProcessName,sizeof(szProcessName)); } Else return; } Else return; printf ("%s (Process id:%u) \ n", szProcessName, ProcessID); CloseHandle (hprocess);}
Windows API Programming----Enumerating system processes