Detailed introduction to a small program that deletes a process

Source: Internet
Author: User
When reading Windows API, as an exercise, I wrote a program for deleting processes. I think there are a lot of knowledge points in it, so I can post it as a note. Of course there will inevitably be errors. please advise ^ I also used the function for permission adjustment mentioned in the article, so I copied it directly from the shutdown program. The following is a program, and the key points are annotated. When reading windows APIs, I wrote a program for deleting processes as an exercise. I think there are a lot of knowledge points in it, so I can post it as a note. Of course there will inevitably be errors. please advise ^
I also used the permission adjustment function mentioned in the article, so I copied it directly from the shutdown program. The following is a program, and the key points are annotated.

When reading windows APIs, I wrote a program for deleting processes as an exercise. I think there are a lot of knowledge points in it, so I can post it as a note. Of course there will inevitably be errors. please advise ^ I also used the function for permission adjustment mentioned in the previous article, so I copied it directly from the shutdown program. The following is a program, and the key points are annotated. # Include
 
  
# Include
  
   
# Include
   
    
Void ListProcess () // list the process name and ID {HANDLE hProcessSnap = NULL; PROCESSENTRY32 pe32 = {0}; // structure for storing process information hProcessSnap = creatw.lhelp32snapshot (TH32CS_SNAPPROCESS, 0 ); // create a snapshot of all processes in the system if (hProcessSnap = INVALID_HANDLE_VALUE) {printf ("createconlhelp32snapshot failed: % d \ n", GetLastError (); return ;} pe32.dwSize = sizeof (PROCESSENTRY32); printf ("ProcessName ProcessID \ n"); if (Process32First (hProcessSnap, & pe32) // point A process and put it into the PROCESSENTRY32 struct {char c [5]; do {itoa (pe32.th32ProcessID, c, 10 ); printf ("%-30 s % d \ n", pe32.szExeFile, pe32.th32ProcessID); // szExeFile is the name of the executable file of the process} while (Process32Next (hProcessSnap, & pe32);} else {printf ("Process32First () failed: % d \ n", GetLastError ();} CloseHandle (hProcessSnap); return;} BOOL EnablePrivilege () // Obtain system permissions {HANDLE hProcess = NULL; HANDLE hToken = NULL; LUID uID = {0}; TOKE N_PRIVILEGES stToken_Privileges = {0}; hProcess = GetCurrentProcess (); // Obtain the current application process handle if (! OpenProcessToken (hProcess, TOKEN_ADJUST_PRIVILEGES, & hToken) // open the access token handle of the current process // (when the OpenProcessToken function fails to call, the returned value is zero) return FALSE; if (! LookupPrivilegeValue (NULL, SE_SHUTDOWN_NAME, & uID) // Obtain the LUID of the permission name "SeShutdownPrivilege" // The return value of the LookupPrivilegeValue function call failure is zero return FALSE; priority = 1; // number of permissions to be adjusted stToken_Privileges.Privileges [0]. luid = uID; // permission LUID stToken_Privileges.Privileges [0]. attributes = SE_PRIVILEGE_ENABLED; // permission attribute, // SE_PRIVILEGE_ENABLED: enable this permission // Adjust the specified permission in the access token (AdjustTokenPrivileges function call failure returns 0) if (! AdjustTokenPrivileges (hToken, FALSE, & stToken_Privileges, sizeof stToken_Privileges, NULL, NULL) return FALSE; if (GetLastError ()! = ERROR_SUCCESS) // check whether the permission is successfully adjusted return FALSE; CloseHandle (hToken); return TRUE;} bool KillProcess (DWORD id) {HANDLE hProcess = NULL, hProcessToken = NULL; bool isKilled = false, bRet = false; EnablePrivilege (); // Adjust the permission printf ("Enable Privilege OK! \ N "); if (hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, id) = NULL) // Open the process to obtain the handle {printf (" Open process % d failed: % d \ n ", id, GetLastError (); return false;} if (! TerminateProcess (hProcess, 1) // terminate the process {printf ("TerminateProcess failed: % d \ n", GetLastError (); return false;} isKilled = true; if (hProcessToken! = NULL) CloseHandle (hProcessToken); if (hProcess! = NULL) CloseHandle (hProcess); return isKilled;} void main () {int id = 0; ListProcess (); while (1) {printf ("Select the process ID to be deleted:"); scanf ("% d", & id); if (KillProcess (id) = true) {system ("cls"); ListProcess ();} else printf ("Failed !! ");}}
   
  
 

The above is a detailed introduction to the mini-program for process deletion. For more information, see other related articles in the first PHP community!

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.