Scan system processes and get PID of a process

Source: Internet
Author: User

Scan all processes of the system

1#include <stdio.h>2#include <windows.h>3#include <tlhelp32.h>4 5 intScan ()6 {7HANDLE Hprocessshap =NULL;8PROCESSENTRY32 pe32 = {0};9Hprocessshap = CreateToolhelp32Snapshot (Th32cs_snapall,0);Ten  One     if(Hprocessshap = =Invalid_handle_value) A     { -printf"\ncreatetoolhelp32snapshot () failed:%d", GetLastError ()); -         return 1; the     } -  -Pe32.dwsize =sizeof(PROCESSENTRY32); -  +     if(Process32First (Hprocessshap, &pe32)) -     { +          Do A         { at wprintf (pe32.szexefile); -printf"\ n"); -         } -          while(Process32Next (Hprocessshap, &pe32)); -     } -     Else in     { -printf"\nprocess32first () failed:%d", GetLastError ()); to     } + CloseHandle (HPROCESSSHAP); -     return 0; the } *  $ intMainintargcChar*argv)Panax Notoginseng { - Scan (); the     return 0; +}

gets the PID of a process

Sometimes when working on a process in a system, we need to get the PID of the program as a parameter. One of these methods is, of course, in the details of the task Manager, and the other is to get the PID of the process directly using the code.

1 intGetpid (wchar_t *name)2 {3HANDLE Hprocessshap =NULL;4     //The structure that holds the snapshot process information5PROCESSENTRY32 pe32 = {0}; 6     //Create a snapshot of the system's processes to scan7Hprocessshap = CreateToolhelp32Snapshot (Th32cs_snapall,0);8     if(Hprocessshap = =Invalid_handle_value)9     {Tenprintf"\ncreatetoolhelp32snapshot () failed:%d", GetLastError ()); One         return 1; A     } -     //set the size of the structure body -Pe32.dwsize =sizeof(PROCESSENTRY32); the     //Process32First get the handle to the first process -     if(Process32First (Hprocessshap, &pe32)) -     { -         //the process name that matches the input +          Do -         { +             if(!wcscmp (name, pe32.szexefile)) A             { at                 return(int) Pe32.th32processid; -             } -         } -          while(Process32Next (Hprocessshap, &pe32)); -     } -     Else in     { -printf"\nprocess32first () failed:%d", GetLastError ()); to     } + CloseHandle (HPROCESSSHAP); -     return 0; the}

Scan system processes and get PID of a process

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.