1, the head file and the implementation part:
1#include <windows.h>2#include <tlhelp32.h>3#include"psapi.h"4 #pragmaComment (lib, "PSAPI.lib")5 6#include <QtCore/QCoreApplication>7#include <QProcess>8 9 intTerminateProcess (QString ProcessName, QString spathname ="")Ten { One intresult =0; A QString ProcessName; - - //Elevate Process Permissions the HANDLE Htoken; - token_privileges TKP; - intn = OpenProcessToken (GetCurrentProcess (), Token_all_access, &htoken); -Lookupprivilegevalue (NULL, Se_debug_name, &TKP. privileges[0]. LUID); +Tkp. Privilegecount =1; -Tkp. privileges[0]. Attributes =se_privilege_enabled; +n = adjusttokenprivileges (Htoken, FALSE, &TKP,0, (ptoken_privileges) NULL,0); A CloseHandle (htoken); at -HANDLE hsnapshot = CreateToolhelp32Snapshot (th32cs_snapprocess,0) ; - PROCESSENTRY32 PInfo; -Pinfo.dwsize =sizeof(pInfo); -Process32First (hSnapShot, &pInfo); - Do in { - //Traverse all processes of a task toProcessName = (QSTRING::FROMUTF16 (reinterpret_cast<ConstUnsigned Short*>(Pinfo.szexefile))); + if(ProcessName = =ProcessName) - { the //find the process to remove the PID * intNpid =Pinfo.th32processid; $ Panax Notoginseng //known PID get path -TCHAR Wcpath[max_path] = {0}; theHANDLE hprocess =0; +hprocess =openprocess (process_all_access, FALSE, npid); A Getmodulefilenameex (hprocess, NULL, Wcpath, MAX_PATH); the CloseHandle (hprocess); +QString path = QSTRING::FROMUTF16 (reinterpret_cast<Const ushort*>(Wcpath)); - $ //If the process is terminated directly based on the process name using the default parameters, it will end according to the program path $ if(Spathname.compare ("") ==0|| Path = =spathname) - { - QString cmd; thecmd = QString ("taskkill/f/pid%1/t"). Arg (npid); -Qprocess P (0);Wuyi P.start (cmd); the p.waitforstarted (); - p.waitforfinished (); WuResult + +; - } About } $} while(Process32Next (hSnapShot, &pInfo)); - CloseHandle (hsnapshot); - returnresult; -}
1. Function:
int Main (intChar *argv[]) { //qcoreapplication A (argc, argv); terminateprocess ("QQ.exe"); // return a.exec ();}
QT implementation to end a process based on the process name