How can I retrieve the full path of running exe

S

somank.sharma

I am running an exe created in C. I need the full path (absolute path)
of this exe. In the first argument of main, I am getting the exe name.
How can I get the full path for this exe.
 
W

Walter Roberson

I am running an exe created in C. I need the full path (absolute path)
of this exe. In the first argument of main, I am getting the exe name.
How can I get the full path for this exe.

There is no portable way to do that.

The C language doesn't even promise that the first argument of main
will have any form of the executable name. (This fact is actively
used in many Unix operating systems.)

Some versions of *some* operating systems provide mechanisms that
do what you want, but they are not necessarily simple to use,
and the path retrieved might be -some- absolute path to reach
the executable rather than the one that was actually used to
invoke it. Details are operating system version dependant, so you
will need to ask in a newsgroup appropriate for your operating system.

(It's not uncommon for it to be pretty messy when it is possible at all.
Messy enough that OS gurus might think it isn't possible when really
it might only take about 3/4 of an hour of investigative work rooting
around in the operating system internals. Or it might be doable on
some operating systems if you devoted a week to it. But it
is completely impossible on some operating systems.)
 
G

ganesh.kundapur

There is no portable way to do that.

The C language doesn't even promise that the first argument of main
will have any form of the executable name. (This fact is actively
used in many Unix operating systems.)

Some versions of *some* operating systems provide mechanisms that
do what you want, but they are not necessarily simple to use,
and the path retrieved might be -some- absolute path to reach
the executable rather than the one that was actually used to
invoke it. Details are operating system version dependant, so you
will need to ask in a newsgroup appropriate for your operating system.

(It's not uncommon for it to be pretty messy when it is possible at all.
Messy enough that OS gurus might think it isn't possible when really
it might only take about 3/4 of an hour of investigative work rooting
around in the operating system internals. Or it might be doable on
some operating systems if you devoted a week to it. But it
is completely impossible on some operating systems.)

Refer getcwd(3) man page.
get_current_dir_name() returns the current working directory of
running exe.
 
C

Chris Dollin

Refer getcwd(3) man page.
get_current_dir_name() returns the current working directory of
running exe.

(a) that's a Linux/Unix/Posix thing: it's OS-dependent.

(b) it's also irrelevant. The OP asked for the full path name of
the running executable, not the full path name of the process
in which it's running.

(c) snip signatures.
 
K

Keith Thompson

There is no portable way to do that.

The C language doesn't even promise that the first argument of main
will have any form of the executable name. (This fact is actively
used in many Unix operating systems.)
[snip]

Refer getcwd(3) man page.
get_current_dir_name() returns the current working directory of
running exe.

Walter's answer, that there is no portable way to do that, is correct.
The getcwd function is system-specific -- and it doesn't do what the
original poster was asking about anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top