Windows API Programming----Enumerating system processes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.