Charlie Gibbs
2022-11-29 23:29:14 UTC
When calling CreateProcess() with lpCurrentDirectory set to NULL, the
child process is supposed to inherit the parent's current directory -
but I've learned the hard way that occasionally it doesn't. I've
had several instances where the child's current directory is set to
something random. The child program then fails because it can't find
files it's expecting to see in the current directory. Before it shuts
down it opens a log file in the current directory (wherever that now
may be), and writes an error message to it; log files have shown up
all over the disk.
This doesn't happen very often - but if you have 1000 customers running
your programs daily, it doesn't take long before anguished phone calls
start coming in.
So far the only completely reliable solution I've found is to include
code in my initialization routines to set the current directory to a
known value; I call GetModuleFileName() to determine where the program
loaded from, and set the current directory there. This works, but has
the disadvantage that my programs cannot run in a directory that is
different from the one in which their .EXE files live.
This might be an old bug that has long since been fixed - we had
a lot of trouble with it back in 2001 and 2002, and my belt-and-
suspenders solution has been in place ever since. It might be
safe to remove my fixes now, but we were burned so badly that
I'm reluctant to take the risk.
Has anyone else encountered this problem?
<rant>
Windows seems to take a cavalier attitude toward the concept of
"current directory". The GetOpenFileName() and GetSaveFileName()
APIs will change the current directory if the user goes into a
different directory while searching for files. The OPENFILENAME
structure passed to these APIs contains the flag OFN_NOCHANGEDIR,
whose purpose is to restore the current directory in such cases,
although I prefer to first call getcwd() and save the current
directory, then use chdir() to restore it afterwards.
Since I wrote this code myself, I trust it to be bombproof -
and should it turn out not to be, I can change it until it is.
</rant>
child process is supposed to inherit the parent's current directory -
but I've learned the hard way that occasionally it doesn't. I've
had several instances where the child's current directory is set to
something random. The child program then fails because it can't find
files it's expecting to see in the current directory. Before it shuts
down it opens a log file in the current directory (wherever that now
may be), and writes an error message to it; log files have shown up
all over the disk.
This doesn't happen very often - but if you have 1000 customers running
your programs daily, it doesn't take long before anguished phone calls
start coming in.
So far the only completely reliable solution I've found is to include
code in my initialization routines to set the current directory to a
known value; I call GetModuleFileName() to determine where the program
loaded from, and set the current directory there. This works, but has
the disadvantage that my programs cannot run in a directory that is
different from the one in which their .EXE files live.
This might be an old bug that has long since been fixed - we had
a lot of trouble with it back in 2001 and 2002, and my belt-and-
suspenders solution has been in place ever since. It might be
safe to remove my fixes now, but we were burned so badly that
I'm reluctant to take the risk.
Has anyone else encountered this problem?
<rant>
Windows seems to take a cavalier attitude toward the concept of
"current directory". The GetOpenFileName() and GetSaveFileName()
APIs will change the current directory if the user goes into a
different directory while searching for files. The OPENFILENAME
structure passed to these APIs contains the flag OFN_NOCHANGEDIR,
whose purpose is to restore the current directory in such cases,
although I prefer to first call getcwd() and save the current
directory, then use chdir() to restore it afterwards.
Since I wrote this code myself, I trust it to be bombproof -
and should it turn out not to be, I can change it until it is.
</rant>
--
/~\ Charlie Gibbs | Life is perverse.
\ / <***@kltpzyxm.invalid> | It can be beautiful -
X I'm really at ac.dekanfrus | but it won't.
/ \ if you read it the right way. | -- Lily Tomlin
/~\ Charlie Gibbs | Life is perverse.
\ / <***@kltpzyxm.invalid> | It can be beautiful -
X I'm really at ac.dekanfrus | but it won't.
/ \ if you read it the right way. | -- Lily Tomlin