How can I retrieve the full path of running exe

Discussion in 'C Programming' started by somank.sharma@gmail.com, Jun 21, 2007.

  1. Guest

    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.
     
    , Jun 21, 2007
    #1
    1. Advertising

  2. In article <>,
    <> wrote:
    >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.)
    --
    Is there any thing whereof it may be said, See, this is new? It hath
    been already of old time, which was before us. -- Ecclesiastes
     
    Walter Roberson, Jun 21, 2007
    #2
    1. Advertising

  3. Guest

    On Jun 21, 12:39 pm, -cnrc.gc.ca (Walter Roberson)
    wrote:
    > In article <>,
    >
    > <> wrote:
    > >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.)
    > --
    > Is there any thing whereof it may be said, See, this is new? It hath
    > been already of old time, which was before us. -- Ecclesiastes


    Refer getcwd(3) man page.
    get_current_dir_name() returns the current working directory of
    running exe.
     
    , Jun 21, 2007
    #3
  4. Chris Dollin Guest

    wrote:

    > On Jun 21, 12:39 pm, -cnrc.gc.ca (Walter Roberson)
    > wrote:
    >> In article <>,
    >>
    >> <> wrote:
    >> >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.)
    >> --
    >> Is there any thing whereof it may be said, See, this is new? It hath
    >> been already of old time, which was before us. -- Ecclesiastes

    >
    > 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.

    --
    Chris "didn't get to D" Dollin

    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England
     
    Chris Dollin, Jun 21, 2007
    #4
  5. "" <> writes:
    > On Jun 21, 12:39 pm, -cnrc.gc.ca (Walter Roberson)
    > wrote:
    >> In article <>,
    >> <> wrote:
    >> >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.)

    [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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 21, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. csdev
    Replies:
    5
    Views:
    12,086
    Tor Iver Wilhelmsen
    Jul 14, 2004
  2. Replies:
    1
    Views:
    324
    John Harrison
    Jun 21, 2007
  3. Replies:
    7
    Views:
    469
  4. dude
    Replies:
    5
    Views:
    1,749
    Jussi Piitulainen
    Mar 17, 2011
  5. sm
    Replies:
    3
    Views:
    123
    Tad McClellan
    Jan 26, 2006
Loading...

Share This Page