TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
miniprocess.cpp
Go to the documentation of this file.
1 #include "miniprocess.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)
78 {
79  const QStringList list(processList());
80  if(list.contains(process) && killProcess(process.toStdWString().c_str()))
81  {
82  TTK_INFO_STREAM("Windows Kill Process " << process << " Successed");
83  }
84 }
85 
86 void TTK::killProcessByName(const QStringList &processes)
87 {
88  const QStringList list(processList());
89  for(const QString &process : qAsConst(processes))
90  {
91  if(list.contains(process) && killProcess(process.toStdWString().c_str()))
92  {
93  TTK_INFO_STREAM("Windows Kill Process " << process << " Successed");
94  }
95  }
96 }
97 #elif defined Q_OS_UNIX
98 #include <QProcess>
99 
100 struct PIDInfo
101 {
102  int m_pid;
103  QString m_path;
104 };
105 
106 static QList<PIDInfo> processList()
107 {
108  QList<PIDInfo> lprocess;
109  QProcess process;
110  process.start("/bin/bash", {"-c", "ps -xu | awk '{print $2\";\"$11}'"});
111  if(!process.waitForFinished())
112  {
113  return lprocess;
114  }
115 
116  const QString data(process.readAll());
117  if(data.isEmpty())
118  {
119  return lprocess;
120  }
121 
122  const QStringList &sp = data.split("\n");
123  for(const QString &var : qAsConst(sp))
124  {
125  const QStringList &line = var.split(";");
126  if(line.count() != 2)
127  {
128  continue;
129  }
130 
131  PIDInfo info;
132  info.m_pid = line.front().toInt();
133  info.m_path = line.back();
134  lprocess << info;
135  }
136 
137  return lprocess;
138 }
139 
140 static bool killProcess(int pid)
141 {
142  QProcess::execute("kill", {"-s", "9", QString::number(pid)});
143  return true;
144 }
145 
146 void TTK::killProcessByName(const QString &process)
147 {
148  const QList<PIDInfo> list(processList());
149  for(const PIDInfo &info : qAsConst(list))
150  {
151  if(info.m_path.contains(process) && killProcess(info.m_pid))
152  {
153  TTK_INFO_STREAM("Unix Kill Process " << process << " PID" << info.m_pid << " Successed");
154  break;
155  }
156  }
157 }
158 
159 void TTK::killProcessByName(const QStringList &processes)
160 {
161  const QList<PIDInfo> list(processList());
162  for(const QString &process : qAsConst(processes))
163  {
164  for(const PIDInfo &info : qAsConst(list))
165  {
166  if(info.m_path.contains(process) && killProcess(info.m_pid))
167  {
168  TTK_INFO_STREAM("Unix Kill Process " << process << " PID" << info.m_pid << " Successed");
169  break;
170  }
171  }
172  }
173 }
174 #endif
TTK_MODULE_EXPORT bool execute(const QString &path)
#define qAsConst
Definition: ttkqtglobal.h:53
#define TTK_INFO_STREAM(msg)
Definition: ttklogger.h:67
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
TTK_MODULE_EXPORT void killProcessByName(const QString &process)