Get system processes list and termination process

xiaoxiao2021-03-06  181

1. The definition of the structure and initializes its members PROCESSENTRY32 dwsizePROCESSENTRY32 proc; proc.dwsize = sizeof (PROCESSENTRY32); 2 with GreateToolhelp32Snapshot (DWORD dwFlags, // TH32CS_SNAPPROCESS or TH32CS_SNAPTHREADDWORD th32ProcessID); // return a handle to the process as a function of 0:. HANDLE hSysSnapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);. 3 pick-Process32First (HANDLE hSnapshot, PROCESSENTRY32 * proc) function value PROCESSENTRY32 member structural body; szExeFile its members return path to the executable file, its members th32ProcessID return process ID. 4. Call the same process as the function process32next (). 5. Close the handle CloseHandle (HSYSSNAPSHOT);

Termination process:

1. Use the process ID to open the process with the openprocess () function and return the process handle. 2. Terminate the process with the function TERMINATEPROCESS (). 3. Close the handle.

// Code: / / Define two arrays to save process ID and corresponding file name #include cstring procFileName [30]; long proCID [30]; // list all processes Void ListProcess (CSTRING PROCFILENAME ], LONG {HANDLE hSysSnapshot = NULL procID []); PROCESSENTRY32 proc; proc.dwSize = sizeof (proc); hSysSnapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0); if (hSysSnapshot == (HANDLE) -1) return; if (Process32First (hSysSnapshot, & proc)) {proc.dwSize = sizeof (proc); int nProcCount = 0; do {ProcFileName [nProcCount] = CString (proc.szExeFile); ProcessID [nProcCount] = proc.th32ProcessID; nProcCount ;} while (Process32Next (hsyssnapshot, ");} closehandle (hsyssnapshot);

// terminate the process specified ID number void Killproc (int nProcID) {if (nProcID) {HANDLE hProc = OpenProcess (PROCESS_ALL_ACCESS, FALSE, nProcID); if (hProc) {TerminateProcess (hProc, 99); CloseHandle (hProc);} }

转载请注明原文地址:https://www.9cbs.com/read-128310.html

New Post(0)