1 registered members (AndrewAMD),
1,534
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
long WINAPI SetCurrentDirectory(long lpPathN)
#189140
03/18/08 09:59
03/18/08 09:59
|
Joined: Apr 2005
Posts: 4,506 Germany
fogman
OP
Expert
|
OP
Expert
Joined: Apr 2005
Posts: 4,506
Germany
|
I want to set the current directory to the work directory (where the executable is located) but I can´t get it to work so far. Even if I give the path name directly in the call, like eg: SetCurrentDirectory("C:\bla\blabla\blaaaa");
This doesn´t work, also: SetCurrentDirectory(work_dir);
I´ve tried to use a long as pointer to the string, but that doesn´t work either.
Please don´t laugh, I´m an absolute beginner when it comes to C. I´m a littlebit confused by all the datatypes. Can you help me out?
no science involved
|
|
|
Re: long WINAPI SetCurrentDirectory(long lpPathN)
[Re: fogman]
#189141
03/18/08 12:13
03/18/08 12:13
|
Joined: Apr 2006
Posts: 66 Sweden
rstralberg
Junior Member
|
Junior Member
Joined: Apr 2006
Posts: 66
Sweden
|
Code:
#include <direct.h>
void SetPathToExe(void) { TCHAR exePath[MAX_PATH+1] ;
::GetModuleFileName( ::GetModuleHandle(0), exePath, sizeof( exePath )-1 ) ; _chdir( exePath ) ; }
Then just call 'SetPathToExe() ;' Kindly ~Roland
Last edited by rstralberg; 03/18/08 12:17.
|
|
|
Re: long WINAPI SetCurrentDirectory(long lpPathN)
[Re: rstralberg]
#189142
03/18/08 14:06
03/18/08 14:06
|
Joined: Apr 2005
Posts: 4,506 Germany
fogman
OP
Expert
|
OP
Expert
Joined: Apr 2005
Posts: 4,506
Germany
|
"TCHAR" undeclared identifier - of course, it´s Lite-C.
When I use a char or a string insteat of a TCHAR, it shows me an syntax error at "::GetModuleFileName".
no science involved
|
|
|
Re: long WINAPI SetCurrentDirectory(long lpPathN)
[Re: fogman]
#189143
03/18/08 14:38
03/18/08 14:38
|
Joined: Apr 2006
Posts: 66 Sweden
rstralberg
Junior Member
|
Junior Member
Joined: Apr 2006
Posts: 66
Sweden
|
Ok. I forgot that we was in lite-C that code was from C. Sorry for that. Here is the lite-C version. Code:
#include <windows.h> #include <stdio.h> void SetPathToExe(void) { char exePath[260+1] ; // MAX_PATH = 260 GetModuleFileName( GetModuleHandle(0), exePath, sizeof( exePath )-1 ) ; _chdir( exePath ) ; }
|
|
|
Re: long WINAPI SetCurrentDirectory(long lpPathN)
[Re: rstralberg]
#189144
03/18/08 15:11
03/18/08 15:11
|
Joined: Apr 2005
Posts: 4,506 Germany
fogman
OP
Expert
|
OP
Expert
Joined: Apr 2005
Posts: 4,506
Germany
|
I´ve used "char exePath = "#261";" because I´ve got an error when I use "char exePath[260+1] ;" Result: No errors but it doesn´t seem to work. Thanks for your help btw.
no science involved
|
|
|
Re: long WINAPI SetCurrentDirectory(long lpPathN)
[Re: fogman]
#189145
03/18/08 15:25
03/18/08 15:25
|
Joined: Apr 2006
Posts: 66 Sweden
rstralberg
Junior Member
|
Junior Member
Joined: Apr 2006
Posts: 66
Sweden
|
Ok.... next one to try then ... I have added a verify ... Code:
#include <windows.h> #include <stdio.h> #define MAX_PATH 260 void SetPathToExe(void) { char exePath[MAX_PATH] ; GetModuleFileName( GetModuleHandle(0), exePath, (MAX_PATH-1) ) ; _chdir( exePath ) ; // verify _getcwd(exePath, (MAX_PATH-1) ) ; printf( "%s\n", exePath ) ; }
|
|
|
Re: long WINAPI SetCurrentDirectory(long lpPathN)
[Re: rstralberg]
#189146
03/18/08 16:00
03/18/08 16:00
|
Joined: Apr 2005
Posts: 4,506 Germany
fogman
OP
Expert
|
OP
Expert
Joined: Apr 2005
Posts: 4,506
Germany
|
It shows me the directory where the latest file comes from - in my case a bmap, loaded through a file open dialog. I need the actual work directory, the directory where the programm (my game or application) is located, regardless if it´s an exe or the development version. I hope that I don´t have confused you by writing "engine path".
no science involved
|
|
|
Re: long WINAPI SetCurrentDirectory(long lpPathN)
[Re: fogman]
#189147
03/18/08 16:57
03/18/08 16:57
|
Joined: Apr 2006
Posts: 66 Sweden
rstralberg
Junior Member
|
Junior Member
Joined: Apr 2006
Posts: 66
Sweden
|
Ok. A slight missunderstanding from my side. If I comes up with a solution I will write it here. Haven't the time for it until later this evening, but hopefully something could be done then ( I hope Cheers
|
|
|
Re: long WINAPI SetCurrentDirectory(long lpPathN)
[Re: rstralberg]
#189149
03/18/08 22:31
03/18/08 22:31
|
Joined: Apr 2006
Posts: 66 Sweden
rstralberg
Junior Member
|
Junior Member
Joined: Apr 2006
Posts: 66
Sweden
|
So... after some struggling here we are Code:
////////////////////////////// // Set working directory // lite-C 7.06.1 // by rstralberg //////////////////////////////
// GameStudio #include <acknex.h>
// Windows and StdLib stuff #include <windows.h> #include <stdio.h> #define MAX_PATH 260
////////////////////////////// // Get executable directory ////////////////////////////// char* GetExeDir(void) { char exeDir[MAX_PATH] ; GetModuleFileName( GetModuleHandle(0), exeDir, (MAX_PATH-1) ) ; int index = strlen(exeDir)-1 ; while( index-- > 0 ) { if( exeDir[index] == '\\' ) { exeDir[index] = '\0' ; break ; } } return exeDir ; }
////////////////////////////// // Get current directory ////////////////////////////// char* GetCurDir(void) { char curDir[MAX_PATH] ;
_getcwd( curDir, (MAX_PATH-1) ) ; return curDir ; }
////////////////////////////// // Set current directory ////////////////////////////// void SetCurDir( const char* pstrDir ) { _chdir( pstrDir ) ; }
////////////////////////////// // MAIN PROGRAM ENTRY ////////////////////////////// function main() { char dir[MAX_PATH] ;
// switch dir to executable directory strcpy( dir, GetExeDir() ) ; SetCurDir( dir ) ; // write a file there just to verify where we are strcpy( dir, GetCurDir() ) ; printf( "We are now in directory %s. Writing 'Hello' to text.txt there\n", dir ) ; FILE* fp = fopen( "test.txt", "wt" ) ; if( fp ) { fputs( "Hello", fp ) ; fclose(fp) ; }
}
This testcode switches to the executable directory, then writes a file test.txt there.
|
|
|
|