Home Forums Programming retrieve ull path to c++ executable

This topic contains 3 replies, has 3 voices, and was last updated by  Anonymous 12 years, 10 months ago.

  • Author
  • #5187


    Hi there,

    I have a C++ program which I need to get the full path to as I need to store game assets in a datbase. I am connecting via odbc so need ot make a DSN string. Now, I have it working but I need to put a literel path into the database string, which is in the same directory as the executable.
    If I change the place the .exe is , which i routinely do, I need to change the conenction string.
    So I was looking for a C++ windows API call which returns the path of the directory of the executable.
    Can anyone shed any light on this , any google search I make returns lots of USELESS results ?
    Cheers in advance guys and gals,

  • #30653


    Well , time is a great heler, unless you have a bad rash, then get a cream.
    And lo and behold the problem solution was found

    #include <direct.h> // for getcwd

    char FilePath[_MAX_PATH]; // _MAX_PATH represents the longest possible path on this OS

    getcwd(FilePath, _MAX_PATH); // reads the current working directory into the array FilePath

    works well for C++ in both VS6 and Bloodshed Dev c++.
    post closed.

  • #30877


    In C and C++ applications argv[0] is set to the executables path. If you’re using C++, I highly recomend boost (boost.org) and its filesystem libraries.

  • #32959


    It can be extracted from the string returned by the Win32 function GetCommandLine () also.

The forum ‘Programming’ is closed to new topics and replies.