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!