Use createmedilhelp32snapshot/process32first/process32next API to enumerate System Processes

Source: Internet
Author: User

Use the createconlhelp32snapshot/process32first/process32next API to enumerate system processes. In many cases, you need to operate system processes, there are many methods, but the most common method is createconlhelp32snapshot/process32first/process32next. A series of Apis enable the end process to use terminateprocess to enable the following functions to enumerate processes: int processlist () {processentry32 pe32; pe32.dwsize = sizeof (pe32); int COUNT = 0; handle hprocesssnap =: equals (th32cs_snapprocess, 0); If (hprocesssnap = equals) {printf ("creat Etoolhelp32snapshot call failed! "); Return-1;} bool bmore =: process32first (hprocesssnap, & pe32); printf (" % 20 s/t % 10 s/n "," process name ", "PID "); printf ("===========================================/ N "); while (bmore) {count ++; printf ("% 20 s/t % 10d/N", pe32.szexefile, pe32.th32processid); bmore =: process32next (hprocesssnap, & pe32 );}:: closehandle (hprocesssnap ); printf ("===========================================/ N "); printf ("/n current system process count: % d/N", count); Return 0 ;} To terminate a process, you can use the following function: (you can obtain the dwid as the ID of the process you want to terminate through the function of the process column) int terminateprocessfromid (DWORD dwid) {bool Bret = false; handle hprocess =: OpenProcess (process_all_access, false, dwid); If (hprocess! = NULL) {Bret =: terminateprocess (hprocess, 0); }:: closehandle (hprocess); If (BRET) printf ("% d process ended successfully ...... /n ", dwid); else printf (" % d process ended failed ...... /n ", dwid); Return 0;} process-related operations usually require elevation of permissions, you can refer to another note, "process permission escalation code. because the code is relatively simple, there is no more text description. The key functions are annotated. In fact, it is okay to call them directly.

 

Related Article

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.