Page 1 of 1

Win32 - Get current directory fails

Posted: Fri Jun 07, 2013 8:29 am
by va!n
I have coded an application which loads some data files. The application has no fixed path. The filestructure looks something liks this:

[code
..\MyApplication\App.exe
\MyApplication\Data\file001.dat
[/code

To get the current direcory i used:

Code: Select all

Global currentDir.s = Space(#MAX_PATH)
GetCurrentDirectory_(255,@currentDir.s)
...

The path for loading the files looks something like:   Load( currentDir.s + "\Data\File001.dat")
This works only as long, as i change to the directory of the executeable and run it from its folder. All datafiles will be loaded and currentDir.s is correct.

I added the path and its executeable filename to the AutoRuns entry in the registry, so the application will be started directly at booting / starting the system.
The application is running but it does not find any datafiles and currentDir.s returns an incorrect path, which shows always: "c:\windows\system32\" :?

So i tried another next code to get the correct path to get the application work - even with autorun.

Code: Select all

currentDir.s = Space(#MAX_PATH)
GetModuleFileName_(0,@currentDir.s, 255)
This returns the correct path of the executeable - but always including the filename of the executeable. Is there any other API / way to get just only the full correct path - without filename?

Re: Win32 - Get current directory fails

Posted: Fri Jun 07, 2013 8:48 am
by jpd
Hi Va!n,
ts-soft wrote: SetCurrentDirectory(GetPathPart(ProgramFilename()))

then use getcurrentdirectory()
:-)

Best
jpd

Re: Win32 - Get current directory fails

Posted: Fri Jun 07, 2013 11:29 am
by Little John
Or just forget about Set/GetCurrentDirectory(). IMHO it causes more problems than it's worth.

Simply use

Code: Select all

GetPathPart(ProgramFilename())
:-)

Re: Win32 - Get current directory fails

Posted: Fri Jun 07, 2013 12:22 pm
by jpd
Hi Little John,

I use this commands on similar situation like Va!n describe without problem.
But is interesting to hear that in some case cause problem, have you an example?

Thanks

jpd

Re: Win32 - Get current directory fails

Posted: Fri Jun 07, 2013 12:48 pm
by ts-soft
jpd wrote:But is interesting to hear that in some case cause problem, have you an example?
If you use a filemanager, the will call your application and set the currentdirectory to you're executable. If you use a shell and don't set the currentdirectory, the currentdirectory is in most cases system32.
The third parameter in RunProgram set the currentdirectory to what ever you want, so you see, it is not sure
currentdirectory is you're programdir :wink:

Only if you set the currentdirectory self, it is sure the show to what you wan't!

Greetings - Thomas

Re: Win32 - Get current directory fails

Posted: Fri Jun 07, 2013 2:47 pm
by jpd
Hi ts-soft,
ts-soft wrote: Only if you set the currentdirectory self, it is sure the show to what you wan't!
I agree with you.

Best
jpd