Click to See Complete Forum and Search --> : Path of exe
Jewe
January 22nd, 2003, 03:55 AM
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:)
Simon666
January 22nd, 2003, 04:38 AM
Use GetModuleFileName:
char szBuffer[_MAX_PATH];
GetModuleFileName(NULL, szBuffer, _MAX_PATH);
JMS
January 22nd, 2003, 05:31 AM
#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...
Andreas Masur
January 22nd, 2003, 05:42 AM
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...
// With STL string
#include <string>
using std::string;
char szAppPath[MAX_PATH] = "";
string strAppDirectory;
::GetModuleFileName(NULL, szAppPath, sizeof(szAppPath) - 1);
// Extract directory
strAppDirectory = szAppPath;
strAppDirectory = strAppDirectory.substr(0, strAppDirectory.rfind("\\"));
// With CString
char szAppPath[MAX_PATH] = "";
CString strAppDirectory;
::GetModuleFileName(NULL, szAppPath, sizeof(szAppPath) - 1);
// Extract directory
strAppDirectory = szAppPath;
strAppDirectory = strAppDirectory.Left(strAppDirectory.ReverseFind("\\"));
// With standard string
char szAppPath[MAX_PATH] = "";
char szAppDirectory[MAX_PATH] = "";
::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[strlen(szAppDirectory)] = '\0';
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.