1

My intention is to get the name of the running executable file,in every platform (Windows,Mac Os x,Linux) with the same code.

There are separate code for each platform to find the name of the executable file.

In Windows there is GetModuleFileName() function to get the name of executable,but it works on Visual Studio.

I also used argv[0] to get the path of the current executable file.

I am using gcc compiler,I need to generate code which works on all platform (Windows,Mac Os x,Linux) to get the name of the running executable file.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    what is `arg[0]`? I've heard of `argv[0] `, though. (Just kiddin') :P – Sourav Ghosh Aug 17 '15 at 15:02
  • 4
    Well, you know that `argv[0]` gives you the information, and it does indeed. What's the problem ? – Quentin Aug 17 '15 at 15:02
  • i want the running executable name,which works in all platform – Anand Sukumar Aug 17 '15 at 15:06
  • @AnandSukumar `argv` is standard, it works on all platforms. – Quentin Aug 17 '15 at 15:07
  • 4
    The pathname of the executable is only found by (different) devious means on different systems. It is not necessarily in `argv[0]`. I believe it is always doable. I'm also tolerably sure there's another question on the topic somewhere in SO — I found it (favourites have their uses); I've closed this as a duplicate of that. – Jonathan Leffler Aug 17 '15 at 15:11
  • I used argv for try,but i need to get the running executable name without using the argv because i am going to use it in dynamic library to get the name of executable. – Anand Sukumar Aug 17 '15 at 15:11
  • There is no guaranteed way to get the actual executable name that's platform-independent, especially if you're trying to get it before `main()` is called. – Andrew Henle Aug 17 '15 at 15:11
  • My intention is to get the name of the executable, in which my dynamic library(dll,so,dylib) is loaded – Anand Sukumar Aug 17 '15 at 15:18
  • @AnandSukumar - See the question this was marked as a dupe of: http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe Note that some of those methods may not work if called via dynamic library prior to `main()` being called. I know from experience that `getexecname()` on Solaris doesn't reliably return a file name when called like that. – Andrew Henle Aug 17 '15 at 15:23
  • @AndrewHenle: have you, or will you please, add that information to the other question? – Jonathan Leffler Aug 17 '15 at 16:11
  • actually i want to get the name of the executable by using the method called from the dynamic library(which is loaded in the application) – Anand Sukumar Aug 18 '15 at 04:29
  • @JonathanLeffler - Still working on verifying the behavior. I only have access to Solaris 11.2 at the moment. – Andrew Henle Aug 19 '15 at 13:49
  • @AndrewHenle: fair enough. I have access to more archaic versions of Solaris (and not to Solaris 11). If you need testing on old Solaris (9 &10), contact me by email; see my profile. – Jonathan Leffler Aug 19 '15 at 13:54

1 Answers1

5

Quoting from the C11 standard, chapter 5.1.2.2.1,

If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment.[...]

So, after a null-check on argv[0], you can use the same as the program name.

And yes, it is platform independent.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    And note that `execlp("sleep", "anonymous", "20", (char *)0);` tells `sleep` that it is called `anonymous`. – Jonathan Leffler Aug 17 '15 at 15:10
  • 2
    That is not always correct. For example, a login shell such as bash will have `-bash` as `argv[0]` instead of `/usr/bin/bash`. – Andrew Henle Aug 17 '15 at 15:10
  • argv[0] isn't necessarily the actual name of the executable. it might be the name of a link that points to the executable, or it could be an alias set up by the user – FredK Aug 17 '15 at 18:51
  • From the path of the executable,we can split the executable name. – Anand Sukumar Aug 19 '15 at 12:15