Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > get full path of application

Reply
Thread Tools

get full path of application

 
 
rh00667
Guest
Posts: n/a
 
      03-06-2007
hi all,

i'm confused now. how i can get the full path of an application?

if myapp is in a directory which belongs to PATH, argv[0] gives me the
first token of cmd line, and not the real path of the executed
program.

so if i write anywhere:

% myapp

it runs ok from the file (for example) /usr/local/bin/myapp

but argv[0] returns me

../myapp

but i need

/usr/local/bin/myapp

thanks in advance

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      03-06-2007
rh00667 wrote:

> hi all,
>
> i'm confused now. how i can get the full path of an application?


You can't.


 
Reply With Quote
 
 
 
 
rh00667
Guest
Posts: n/a
 
      03-06-2007
On Mar 6, 2:41 pm, Rolf Magnus <(E-Mail Removed)> wrote:

> You can't.


wow! any hint?

or must i navegate PATH finding the first pathdir/myapp ??????



 
Reply With Quote
 
rh00667
Guest
Posts: n/a
 
      03-06-2007
On Mar 6, 2:41 pm, Rolf Magnus <(E-Mail Removed)> wrote:

> You can't.


wow! any hint?

or must i navegate PATH finding the first pathdir/myapp ??????



 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      03-06-2007
rh00667 wrote:

> On Mar 6, 2:41 pm, Rolf Magnus <(E-Mail Removed)> wrote:
>
>> You can't.

>
> wow! any hint?


It's compiler/OS specific. Standard C++ has no means of getting the path of
the executable. You will need to use some platform specific code. What that
code looks like, is a question you will have to ask in a forum for your
particular compiler/OS.


Best

Kai-Uwe Bux

 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      03-06-2007
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> On Mar 6, 2:41 pm, Rolf Magnus <(E-Mail Removed)> wrote:
>
> > You can't.

>
> wow! any hint?


Sure: any real answer is platform specific, so you need to ask where
your platform is topical.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      03-06-2007
On Mar 6, 8:16 am, "rh00667" <(E-Mail Removed)> wrote:
> hi all,
>
> i'm confused now. how i can get the full path of an application?
>
> if myapp is in a directory which belongs to PATH, argv[0] gives me the
> first token of cmd line, and not the real path of the executed
> program.
>
> so if i write anywhere:
>
> % myapp
>
> it runs ok from the file (for example) /usr/local/bin/myapp
>
> but argv[0] returns me
>
> ./myapp
>
> but i need
>
> /usr/local/bin/myapp
>
> thanks in advance

Of course this is platform specific but the issue is common across
platforms and any solutions would be useful across platforms. Here's
what we do (which has nothing to do with C++ even though the apps
are written in C++). Note that most of this stuff can be done in C++
but it just seems easier to use a script since one is needed for
launching the apps for other reasons.

1. Start your applications via a script and have it know specifically
where your application lives relative to where the script lives.

2. Detect the full pathname of where your script lives. Here's a
simple bash shell function which sets a variable INSDIR to the
full pathname of where the script lives. The function allows
the script to be started from any directory.

install_directory ()
{
if [ `printf '%c' $0` == "/" ]; then
INSDIR=`dirname $0`;
else
INSDIR=`pwd`"/"`dirname $0`;
fi
}

3. Launch your application with a full pathname by manipulating the
full path name of your script and the relative path of your
executable
to get the full patha name of your excutable.

Hope that helps.

 
Reply With Quote
 
adrian.hawryluk@gmail.com
Guest
Posts: n/a
 
      03-06-2007
On Mar 6, 11:10 am, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> On Mar 6, 8:16 am, "rh00667" <(E-Mail Removed)> wrote:
>
> > hi all,

>
> > i'm confused now. how i can get the full path of an application?

>
> > if myapp is in a directory which belongs to PATH, argv[0] gives me the
> > first token of cmd line, and not the real path of the executed
> > program.

>
> > so if i write anywhere:

>
> > % myapp

>
> > it runs ok from the file (for example) /usr/local/bin/myapp

>
> > but argv[0] returns me

>
> > ./myapp

>
> > but i need

>
> > /usr/local/bin/myapp

>
> > thanks in advance

>
> Of course this is platform specific but the issue is common across
> platforms and any solutions would be useful across platforms. Here's
> what we do (which has nothing to do with C++ even though the apps
> are written in C++). Note that most of this stuff can be done in C++
> but it just seems easier to use a script since one is needed for
> launching the apps for other reasons.
>
> 1. Start your applications via a script and have it know specifically
> where your application lives relative to where the script lives.
>
> 2. Detect the full pathname of where your script lives. Here's a
> simple bash shell function which sets a variable INSDIR to the
> full pathname of where the script lives. The function allows
> the script to be started from any directory.
>
> install_directory ()
> {
> if [ `printf '%c' $0` == "/" ]; then
> INSDIR=`dirname $0`;
> else
> INSDIR=`pwd`"/"`dirname $0`;
> fi
>
> }
>
> 3. Launch your application with a full pathname by manipulating the
> full path name of your script and the relative path of your
> executable
> to get the full patha name of your excutable.
>
> Hope that helps.


You could also traverse the PATH environment variable, but you will
have to know something about the OS you are using.

If you are using UNIX, or Windoze with MiNGW, or Cygwin, you could
call the command which the argv[0] as an argument. But then you would
have to fork with a pipe to set its stdout to which can be a pain, or
you can use system() and redirect it to a named pipe which you can
read from more easliy. i.e. system("which prog > namedPipe").


Adrian

 
Reply With Quote
 
rh00667
Guest
Posts: n/a
 
      03-06-2007
thank to all for comments and ideas!

i tested this for redhat4:

pid = getpid();
sprintf(proc_exe, "/proc/%d/exe", pid);
nr = readlink(proc_exe, buff, BUFFSIZE);
buff[nr]=0;
return buff;

(defs, checks, cosmetics are not shown)

tks!

 
Reply With Quote
 
adrian.hawryluk@gmail.com
Guest
Posts: n/a
 
      03-06-2007
On Mar 6, 2:20 pm, "rh00667" <(E-Mail Removed)> wrote:
> thank to all for comments and ideas!
>
> i tested this for redhat4:
>
> pid = getpid();
> sprintf(proc_exe, "/proc/%d/exe", pid);
> nr = readlink(proc_exe, buff, BUFFSIZE);
> buff[nr]=0;
> return buff;
>
> (defs, checks, cosmetics are not shown)
>
> tks!


Very proprietary, will work on only LINUX and I don't even know if it
will work on all of them.


Adrian

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get Full Path of current directory ? (ASP.net) PHaroZ ASP .Net 5 08-17-2012 08:39 AM
os.path.walk() to get full path of all files dude Python 5 03-17-2011 09:09 AM
how to get application path? Is that my method in getting path wrong? James Java 2 12-07-2004 11:59 AM
how to get application path when the application start? haiwen ASP .Net Web Controls 1 01-28-2004 03:21 PM
Simple way to get the full path of a running script? Benjamin Han Python 7 01-03-2004 07:15 PM



Advertisments