Originally Posted by
scwizzo
Here's something interesting.
When I use getcwd() I get the the correct path, but it still won't open my files. When I use GetCurrentDirectory() I get the C:\Windows\system\ path. So both return different things, yet neither will open my file despite one of them being correct.
edit: nevermind, I just ran it through another test. It is returning the C:\Windows\system path every time it is launched from the link. Does anyone know of a way to make it be what I want, without hard coding it, and without sticking an .ini file in the start menu folder? I prefer to keep only links in the start menu folder, and keep all program files in the folder under Program Files.
The reason I don't want to hard code it is cause some systems are x86 with only Program Files folder, and other are x64 with both Program Files and Program Files (x86) folders. Also, the user may change the path on install.
Extract the full path from argv[ 0 ]. If it isn't empty, use it to set the current directory. Something like this should work:
Code:
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
char* extract_program_path( const char* nam, char* buf, size_t max )
{
char* end = strrchr( nam, '\\' );
if( end == NULL )
return getcwd( buf, max );
size_t len = end - nam;
if( max <= len )
return NULL;
strncpy( buf, nam, len );
buf[ len ] = 0;
return buf;
}
// Example:
int main( int argc, char** argv )
{
char dir[ MAX_PATH ];
if( extract_program_path( argv[ 0 ], dir, MAX_PATH ) )
{
chdir( dir );
}
}