Win32 - Get current directory fails

Just starting out? Need help? Post your questions and find answers here.
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Win32 - Get current directory fails

Post 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?
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Re: Win32 - Get current directory fails

Post by jpd »

Hi Va!n,
ts-soft wrote: SetCurrentDirectory(GetPathPart(ProgramFilename()))

then use getcurrentdirectory()
:-)

Best
jpd
PB 5.10 Windows 7 x64 SP1
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Win32 - Get current directory fails

Post 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())
:-)
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Re: Win32 - Get current directory fails

Post 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
PB 5.10 Windows 7 x64 SP1
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Win32 - Get current directory fails

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Re: Win32 - Get current directory fails

Post 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
PB 5.10 Windows 7 x64 SP1
Post Reply