Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ program locate itself

Options
  • 17-08-2001 4:03am
    #1
    Registered Users Posts: 7,314 ✭✭✭


    i'm writing a C++ program atm and one thing i've never managed to find is some sort of function that will get the programs own location? msdn seems no help(maybe searching wrong thing) but i need to store an ini file beside my program...it sounds very simple but if i have to hard code some way of finding the program it will take ages....anyone know the function/method?


Comments

  • Subscribers Posts: 1,911 ✭✭✭Draco


    If you're using VC++ then this is the function your looking for: char *_getcwd( char *buffer, int maxlen ); I think the function is slightly different in non Mircosoft compliers (probably loses the _ at the very least)


  • Closed Accounts Posts: 1,193 ✭✭✭Kix


    Draco: getcwd only gets the current working directory, not necessarly the one you want.

    If you have access to main() then argv[0] should be the full path to the program.

    If you are using MFC you can use something like:
      char buffer[_MAX_PATH];
      ::GetModuleFileName(AfxGetApp()->m_hInstance, buffer, _MAX_PATH);
    

    or if it's straight Win32 with a WinMain() function you just need:
      char buffer[_MAX_PATH];
      GetModuleFileName(hInstance, buffer, _MAX_PATH);
    

    Which is practically the same thing anyway.

    In all cases you'll get something like C:\TEST\TEST.EXE and you'll have to trim off the application name to get just the path.

    I'll leave that as an exercise for the reader. smile.gif

    K

    <edit> Tidied up a bit </edit>



    [This message has been edited by Kix (edited 17-08-2001).]


  • Subscribers Posts: 1,911 ✭✭✭Draco


    <font face="Verdana, Arial" size="2">Originally posted by Kix:
    Draco: getcwd only gets the current working directory, not necessarly the one you want.
    </font>
    Point taken...but it should still work if it is called before you change directories... wink.gif



  • Closed Accounts Posts: 1,193 ✭✭✭Kix


    <font face="Verdana, Arial" size="2">Originally posted by Draco:
    Point taken...but it should still work if it is called before you change directories... wink.gif</font>

    Draco,

    I've seen people get burned with this approach loads of times here at work. If you run the program from the command line and you're not in its directory then you won't get the right answer whether you specify the path to the executable explictly or whether it's on the PATH.

    If you run it from inside Windows and it's not on the C: drive or even if somone's used a common dialog box to load or save a file before you retrieve the path you probably won't get the right answer either.

    Almost good enough isn't usually any good at all for professional code.

    No offence intended. It's good to know the right way to do it.

    K


  • Registered Users Posts: 7,314 ✭✭✭Nietzschean


    Well code worked perfect so thanks both of ye's program all done now (saved me hours of **** 'n around on da net looking for function) smile.gif


  • Advertisement
  • Subscribers Posts: 1,911 ✭✭✭Draco


    <font face="Verdana, Arial" size="2">Originally posted by Kix:
    I've seen people get burned with this approach loads of times here at work</font>
    *cough* hence the smilie?



  • Closed Accounts Posts: 1,193 ✭✭✭Kix


    *cough* OK *cough* smile.gif


Advertisement