40

I run a Qt application, what I want to know is this running binary file name.

tro
  • 6,473
  • 6
  • 46
  • 65
Frank
  • 781
  • 2
  • 8
  • 16

3 Answers3

99

I must (partially) disagree with the other comments that it is not a Qt question: There is a Qt method QCoreApplication::applicationFilePath() which gives the directory+filename of the executable.

On Linux this will try to use /proc, and on Windows perhaps GetModuleFileName(). According to the docs it will fall back to argv[0].

You could then use QFileInfo to split it into an executable name and a directory.

QFileInfo(QCoreApplication::applicationFilePath()).fileName()
Cameron
  • 91,868
  • 20
  • 192
  • 220
André
  • 17,162
  • 6
  • 55
  • 69
3

The Qapplication parses the commandline arguemnts, the first entry is the name of the executable - this is roughly the same as argv[0] in standard C but has a few extra complexities on windows if you have a Unicode build or if the application is started as a service

See http://doc.qt.io/qt-5/qcoreapplication.html#arguments

Christophe Weis
  • 2,350
  • 4
  • 27
  • 30
Martin Beckett
  • 92,791
  • 27
  • 183
  • 258
-2

Again not really a Qt question. To find the name of the binary file executed it would be something like.

#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
  cout << argv[0] << endl;
  return 0;
}
tro
  • 6,473
  • 6
  • 46
  • 65
St0ner
  • 291
  • 1
  • 3
  • 5
  • 2
    I'm unsure as to why your concern about it being a Qt question is relevant. The OP is simply stating the environment they're using, no different to if they had said "Linux" or Windows" or, for that matter, "C++". It's extra information which can help target the answers. In this particular case, it's useful because Qt provides a *much* better way to get this info - as per the ISO standard, `argv[0]` is not required to actually hold *any* useful information about the executable. see http://stackoverflow.com/questions/2794150/when-can-argv0-have-null/2794171#2794171 for details. – paxdiablo Nov 08 '16 at 01:04