TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkprocess.cpp
Go to the documentation of this file.
1 #include "ttkprocess.h"
2 #include <QStringList>
3 
4 #ifdef Q_OS_WIN
5 #define WIN32_LEAN_AND_MEAN
6 #include <qt_windows.h>
7 #include <psapi.h>
8 #include <tlhelp32.h>
9 
10 static QStringList processList()
11 {
12  QStringList lprocess;
13  unsigned long aProcesses[1024], cbNeeded, cProcesses;
14  if(!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
15  {
16  return lprocess;
17  }
18 
19  cProcesses = cbNeeded / sizeof(unsigned long);
20  for(unsigned int i = 0; i < cProcesses; ++i)
21  {
22  if(aProcesses[i] == 0)
23  {
24  continue;
25  }
26 
27  HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, aProcesses[i]);
28  wchar_t buffer[50];
29  GetModuleBaseNameW(hProcess, 0, buffer, 50);
30  CloseHandle(hProcess);
31  lprocess << QString::fromWCharArray(buffer);
32  }
33 
34  return lprocess;
35 }
36 
37 static bool killProcess(LPCWSTR processName)
38 {
39  PROCESSENTRY32W pe;
40  DWORD id = 0;
41  HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
42  pe.dwSize = sizeof(PROCESSENTRY32W);
43  if(!Process32FirstW(hSnapshot, &pe))
44  {
45  return false;
46  }
47 
48  while(true)
49  {
50  pe.dwSize = sizeof(PROCESSENTRY32W);
51  if(Process32NextW(hSnapshot, &pe) == FALSE)
52  {
53  break;
54  }
55 
56  //find processName
57  if(wcsicmp(pe.szExeFile, processName) == 0)
58  {
59  id = pe.th32ProcessID;
60  break;
61  }
62  }
63 
64  CloseHandle(hSnapshot);
65 
66  //Kill The Process
67  HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, id);
68  if(hProcess)
69  {
70  TerminateProcess(hProcess,0);
71  CloseHandle(hProcess);
72  }
73 
74  return true;
75 }
76 
77 void TTK::killProcessByName(const QString &process, bool more)
78 {
79  const QStringList infos(processList());
80  for(const QString &info : qAsConst(infos))
81  {
82  if(info.contains(process) && killProcess(process.toStdWString().c_str()))
83  {
84  TTK_INFO_STREAM("Windows Kill Process" << process << "Successed");
85  if(!more)
86  {
87  break;
88  }
89  }
90  }
91 }
92 
93 void TTK::killProcessByName(const QStringList &processes, bool more)
94 {
95  const QStringList infos(processList());
96  for(const QString &process : qAsConst(processes))
97  {
98  for(const QString &info : qAsConst(infos))
99  {
100  if(info.contains(process) && killProcess(process.toStdWString().c_str()))
101  {
102  TTK_INFO_STREAM("Windows Kill Process" << process << "Successed");
103  if(!more)
104  {
105  break;
106  }
107  }
108  }
109  }
110 }
111 #elif defined Q_OS_UNIX
112 #include <QProcess>
113 
114 struct PIDInfo
115 {
116  int m_pid;
117  QString m_path;
118 };
119 
120 static QList<PIDInfo> processList()
121 {
122  QList<PIDInfo> lprocess;
123  QProcess process;
124  process.start("/bin/bash", {"-c", "ps -xu | awk '{print $2\";\"$11}'"});
125  if(!process.waitForFinished())
126  {
127  return lprocess;
128  }
129 
130  const QString data(process.readAll());
131  if(data.isEmpty())
132  {
133  return lprocess;
134  }
135 
136  const QStringList &sp = data.split(TTK_LINEFEED);
137  for(const QString &var : qAsConst(sp))
138  {
139  const QStringList &line = var.split(";");
140  if(line.count() != 2)
141  {
142  continue;
143  }
144 
145  PIDInfo info;
146  info.m_pid = line.front().toInt();
147  info.m_path = line.back();
148  lprocess << info;
149  }
150 
151  return lprocess;
152 }
153 
154 static bool killProcess(int pid)
155 {
156  QProcess::execute("kill", {"-s", "9", QString::number(pid)});
157  return true;
158 }
159 
160 void TTK::killProcessByName(const QString &process, bool more)
161 {
162  const QList<PIDInfo> infos(processList());
163  for(const PIDInfo &info : qAsConst(infos))
164  {
165  if(info.m_path.contains(process) && killProcess(info.m_pid))
166  {
167  TTK_INFO_STREAM("Unix Kill Process" << process << "PID" << info.m_pid << "Successed");
168  if(!more)
169  {
170  break;
171  }
172  }
173  }
174 }
175 
176 void TTK::killProcessByName(const QStringList &processes, bool more)
177 {
178  const QList<PIDInfo> infos(processList());
179  for(const QString &process : qAsConst(processes))
180  {
181  for(const PIDInfo &info : qAsConst(infos))
182  {
183  if(info.m_path.contains(process) && killProcess(info.m_pid))
184  {
185  TTK_INFO_STREAM("Unix Kill Process" << process << "PID" << info.m_pid << "Successed");
186  if(!more)
187  {
188  break;
189  }
190  }
191  }
192  }
193 }
194 #endif
TTK_MODULE_EXPORT bool execute(const QString &path)
TTK_MODULE_EXPORT void killProcessByName(const QString &process, bool more=false)
#define qAsConst
Definition: ttkqtglobal.h:57
#define TTK_INFO_STREAM(msg)
Definition: ttklogger.h:74
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
#define TTK_LINEFEED
Definition: ttkglobal.h:271