CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Path of exe

  1. #1
    Join Date
    Mar 2002
    Location
    Holland
    Posts
    279

    Question Path of exe

    Hi,

    I need to know the path the exe is running from.
    Like d:\C\projectabc\debug

    I think there is a API call or something for it, but i'm unable to find it.

    I need it for buffering images in my program.

    ThX,

    Jewe
    A VB programmer trying to stay alive in a Real C World

    If the hardware is so great.. why use software to correct it..?? It will only slow it down..
    Al is de hardware nog zo snel de software achterhaalt het wel

  2. #2
    Join Date
    Oct 2001
    Location
    lake of fire and brimstone
    Posts
    1,628
    Use GetModuleFileName:

    char szBuffer[_MAX_PATH];
    GetModuleFileName(NULL, szBuffer, _MAX_PATH);
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞

  3. #3
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    #1) one easy way is to find the path to the currently running program
    is.......

    main( int argv, char *argc[] )
    {
    printf( "The current program running is [%s]\n",
    argc[0] );

    }

    then you can use _splitpath() to get the directory, drive etc

    #2) If you're not in the main and don't want to bother with the navigation to main or you want the working directory which isn't always the same as the executeable directory then you can use the stdio.h function.... _getcwd or "Get Current Working Directory".....


    void main( void )
    {
    char buffer[_MAX_PATH];

    /* Get the current working directory: */
    if( _getcwd( buffer, _MAX_PATH ) == NULL )
    perror( "_getcwd error" );
    else
    printf( "%s\n", buffer );
    }


    #3) I believe GetModuleFileName will return the path of the .c or .h file..

    #4) The macro "__FILE__" is also available which returns the actual .C or .h file name including path..

    have fun and good luck...

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...it is depending which platform you are working on...there is no platform-independent way except the passed command-line arguments where the path of the application is passed as the first argument...

    On a windows system you can use the following API call...
    Code:
    // With STL string
    #include <string>
    
    using std::string;
    
    char szAppPath&#091;MAX_PATH&#093; = "";
    string strAppDirectory;
    
    ::GetModuleFileName(NULL, szAppPath, sizeof(szAppPath) - 1);
    
    // Extract directory
    strAppDirectory = szAppPath;
    strAppDirectory = strAppDirectory.substr(0, strAppDirectory.rfind("\\"));
    
    
    
    // With CString
    char szAppPath&#091;MAX_PATH&#093; = "";
    CString strAppDirectory;
    
    ::GetModuleFileName(NULL, szAppPath, sizeof(szAppPath) - 1);
    
    // Extract directory
    strAppDirectory = szAppPath;
    strAppDirectory = strAppDirectory.Left(strAppDirectory.ReverseFind("\\"));
    
    
    
    // With standard string
    char szAppPath&#091;MAX_PATH&#093;      = "";
    char szAppDirectory&#091;MAX_PATH&#093; = "";
    
    ::GetModuleFileName(NULL, szAppPath, sizeof(szAppPath) - 1);
    
    // Extract directory
    // Note, that the extra spaces around \\ are only for display reasons here...
    strncpy(szAppDirectory, szAppPath, strrchr(szAppPath, ' \\ ') - szAppPath);
    szAppDirectory&#091;strlen(szAppDirectory)&#093; = '\0';

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured