Click to See Complete Forum and Search --> : Confused over GetCurrentDirectory


ahmd
September 24th, 2009, 10:49 PM
Does each process have its current directory reset to the folder where it was started from at the beginning, or is it some sort of a global value that is not changed like that?

And the second question, what can change that current directory?

hoxsiew
September 25th, 2009, 07:43 AM
It's part of the environment inherited by the application from the OS at execution time, so it's a little of both.

As far as I know, there's no way to change it without directly mucking with the environment table for the application and this would likely screw up many of the standard C/C++ library functions.

MrViggy
September 25th, 2009, 03:45 PM
Changing the working directory (http://msdn.microsoft.com/en-us/library/aa363806%28VS.85%29.aspx).

Viggy

ovidiucucu
September 25th, 2009, 04:12 PM
Does each process have its current directory reset to the folder where it was started from at the beginning [...]
No. The current directory of a process may be or may be not the folder from where it is launched.
See for an example the lpCurrentDirectory parameter of CreateProcess (http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx) function.
Also it may be changed anytime by a call of SetCurrentDirectory, GetOpenFileName, and so on.

ahmd
September 25th, 2009, 07:07 PM
Guys, I'm a bit confused. According to MSDN and the link that Viggy posted above:the current directory; it is the directory in which the active application started, unless explicitly changed., but ovidiucucu says that:No. The current directory of a process may be or may be not the folder from where it is launched. See for an example the lpCurrentDirectory parameter of CreateProcess function.

The reason I need to know this is because I'm tryin to convert a relative path to an absolute one (that path will be used in my app only) and GetCurrentDirectory seems to be the only way to do it.

ovidiucucu
September 26th, 2009, 01:10 AM
I said the same thing in other words.

To get the directory in which the executable file is, you can call GetModuleFileName function. See this FAQ: http://www.codeguru.com/forum/showthread.php?t=312471.

// there are also other Codeguru FAQs (http://www.codeguru.com/forum/showthread.php?t=350759) which can make you less confused. ;)

ahmd
September 26th, 2009, 02:05 AM
Yeah, I thought about it, but you see it's not that easy. Say, if I call CreateFile with "test.txt", where will test.txt be created?

ovidiucucu
September 26th, 2009, 06:09 AM
Say, if I call CreateFile with "test.txt", where will test.txt be created?
In the current directory of the process.

ahmd
September 26th, 2009, 11:43 AM
OK, I apologize for my tenaciousness, but does this mean that I need to use GetCurrentDirectory to convert that relative path to an absolute one (for my program only)?

Ajay Vijay
September 26th, 2009, 11:48 AM
But why do you need that? If you know the current directory, or have set current directory, you need not to use absolute path.

But personally, I always prefer using absolute path (without relying on current directory, since drive change may make matter worse).

ahmd
September 26th, 2009, 12:09 PM
I'm sorry, but the question of why I need it is not what is discussed on this thread. But since you asked, I need it to compare two file paths (that can be provided by a user) and that doesn't seem to be an easy task.

MrViggy
September 28th, 2009, 02:46 PM
Guys, I'm a bit confused. According to MSDN and the link that Viggy posted above:, but ovidiucucu says that:

The reason I need to know this is because I'm tryin to convert a relative path to an absolute one (that path will be used in my app only) and GetCurrentDirectory seems to be the only way to do it.

I believe that ovidiucucu was referring to the directory in which the EXE itself resides.

Viggy

ovidiucucu
September 28th, 2009, 02:59 PM
[...] I need it to compare two file paths (that can be provided by a user) [...]
That's far, far away of both thread title and OP (original post).