swift
Loading...
Searching...
No Matches
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#ifdef 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
25namespace swift::misc
26{
27
29 {
30 return QStringLiteral("{ %1, %2 }").arg(QString::number(m_pid), m_name);
31 }
32
33#ifdef 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)
51 {
52 return {};
53 }
54 wchar_t path[1024];
55 auto len = GetModuleFileNameEx(proc, nullptr, path, std::extent_v<decltype(path)>);
56 CloseHandle(proc);
57 if (len <= 0)
58 {
59 return {};
60 }
61 return QFileInfo(QString::fromWCharArray(path)).completeBaseName();
62 }
63#else
64 QString CProcessInfo::processNameFromId(qint64)
65 {
66 qFatal("Not implemented");
67 return {};
68 }
69#endif
70
71} // namespace swift::misc
QString convertToQString(bool i18n=false) const
Cast as QString.
Free functions in swift::misc.
QString fileName() const const
QString symLinkTarget() const const
QString arg(Args &&... args) const const
QString fromWCharArray(const wchar_t *string, qsizetype size)
QString number(double n, char format, int precision)
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