swift
processinfo.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2016 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/processinfo.h"
5 
6 #include <type_traits>
7 
8 #include <QFile>
9 #include <QFileInfo>
10 
11 #if defined(Q_OS_MACOS)
12 # include <libproc.h>
13 #elif defined(Q_OS_WIN)
14 # ifndef NOMINMAX
15 # define NOMINMAX
16 # endif
17 // clang-format off
18 # include <windows.h>
19 # include <psapi.h>
20 // clang-format on
21 #endif
22 
24 
25 namespace swift::misc
26 {
27 
28  QString CProcessInfo::convertToQString(bool) const
29  {
30  return QStringLiteral("{ %1, %2 }").arg(QString::number(m_pid), m_name);
31  }
32 
33 #if defined(Q_OS_LINUX)
34  QString CProcessInfo::processNameFromId(qint64 pid)
35  {
36  QString path = QFileInfo(QStringLiteral("/proc/%1/exe").arg(pid)).symLinkTarget();
37  return QFileInfo(path).fileName();
38  }
39 #elif defined(Q_OS_MACOS)
40  QString CProcessInfo::processNameFromId(qint64 pid)
41  {
42  char name[1024];
43  proc_name(pid, name, std::extent_v<decltype(name)>);
44  return name;
45  }
46 #elif defined(Q_OS_WIN)
47  QString CProcessInfo::processNameFromId(qint64 pid)
48  {
49  HANDLE proc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, static_cast<DWORD>(pid));
50  if (!proc) { return {}; }
51  wchar_t path[1024];
52  auto len = GetModuleFileNameEx(proc, nullptr, path, std::extent_v<decltype(path)>);
53  CloseHandle(proc);
54  if (len <= 0) { return {}; }
55  return QFileInfo(QString::fromWCharArray(path)).completeBaseName();
56  }
57 #else
58  QString CProcessInfo::processNameFromId(qint64)
59  {
60  qFatal("Not implemented");
61  return {};
62  }
63 #endif
64 
65 } // namespace swift::misc
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition: processinfo.cpp:28
Free functions in swift::misc.
unsigned long DWORD
Fake Windows DWORD.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67