QT implementation to end a process based on the process name

Source: Internet
Author: User

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, &AMP;TKP. privileges[0]. LUID); +Tkp. Privilegecount =1; -Tkp. privileges[0]. Attributes =se_privilege_enabled; +n = adjusttokenprivileges (Htoken, FALSE, &AMP;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

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.