0

There are many similar questions to this but I could not find one which exactly matches to what I need to know.

It's a C++ application in Linux. I am in a deep nested function where I need to know the name of the this application instance. How can I do that?

Following are not the possible options:

  1. Getcwd function return the directory name, and not the application name.
  2. I cannot use argv[0] since I am not in the main function. Also, my main function does not access argv[0] and save it somewhere. Also, I am not the author of main function and I cannot change it.
Mercurial
  • 3,615
  • 5
  • 27
  • 52
Rakesh Agarwal
  • 3,009
  • 9
  • 33
  • 40
  • Re, "I cannot use `argv`." Is that because you are not allowed to change the `main()` function? 'Cause if you're allowed to change the `main()` function, then it's simple for `main()` to stash the program name in a global variable that can be accessed by any other function. – Solomon Slow Nov 14 '19 at 16:10
  • @SolomonSlow, yes that's right. I am not allowed to change the main function. – Rakesh Agarwal Nov 14 '19 at 16:11
  • 2
  • 1
    `/proc/self/exe` is a symbolic link to the executable that is running in the process. http://man7.org/linux/man-pages/man5/proc.5.html – Solomon Slow Nov 14 '19 at 16:28
  • There are multiple reasons due to which argv[0] solution is not feasible. 1. The use of global variable would not be recommended by others. 2. It's a huge application and the call is too nested to add a new parameter at each level. 3. The call is inside the dll where I need to know the application name. So, it would be quite some work (a new function in dll to be called from app to stash the global value etc.) – Rakesh Agarwal Nov 14 '19 at 16:38

0 Answers0