File: selflocator.cpp

package info (click to toggle)
gammaray 3.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,612 kB
  • sloc: cpp: 94,643; ansic: 2,227; sh: 336; python: 164; yacc: 90; lex: 82; xml: 61; makefile: 26
file content (59 lines) | stat: -rw-r--r-- 1,424 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
  selflocator.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Volker Krause <volker.krause@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include "selflocator.h"

#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QString>

using namespace GammaRay;

#ifndef Q_OS_WIN

#include <dlfcn.h>

static QString findMeInternal()
{
    Dl_info info;
    if (dladdr(reinterpret_cast<void *>(&SelfLocator::findMe), &info) == 0)
        return QString();
    if (!info.dli_fname)
        return QString();
    return QString::fromLocal8Bit(info.dli_fname);
}

#else

#include <qt_windows.h>

static QString findMeInternal()
{
    WCHAR path[MAX_PATH];
    HMODULE handle;
    if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
                           reinterpret_cast<LPWSTR>(&SelfLocator::findMe), &handle)) {
        GetModuleFileNameW(handle, path, sizeof(path));
        return QDir::fromNativeSeparators(QString::fromUtf16(reinterpret_cast<const char16_t *>(path)));
    }
    return QString();
}

#endif

QString SelfLocator::findMe()
{
    const QFileInfo fi(findMeInternal());
    return fi.canonicalFilePath();
}