2

In Windows -- and probably Unix for that matter -- using the chdir() function in a (32-bit) program doesn't change the directory when the program exits. (It does in a 16-bit Windows program.)
Does anybody know how to do that in a Windows 32-bit program?

Michael Myers
  • 178,094
  • 41
  • 278
  • 290
  • 1
    Things needed to make this a better question: What language are you talking about? Perl? C? Something else? Secondly, if you have some code to demonstrate your problem, that will greatly increase the chances at getting a good response to your question. – JasCav Mar 12 '10 at 18:29
  • May I ask *why* you want to do this? Perhaps there are other ways of achiving your goal without needing to change the working directory of the Windows shell? – Andreas Rejbrand Mar 12 '10 at 19:07

4 Answers4

7

Uhm... IMHO it's exactly one of the things that the OS must guarantee not to happen. The current dir is a per-process property, a child process usually inherits it from the parent process, but the reverse should not happen (and it doesn't).

To obtain what you want, the parent could actively watch some information (message, file, shared memory...) in which the child process stores the new directory, and then call chdir() with the new value.

As far as I know, Windows' cmd.exe doesn't have any mechanism like that. Actually, by using code injection techniques (e.g. CreateRemoteThread) on the parent process it could be possible to force it to do something unexpected, but it's a very dirty trick, not at all good neither general. Win16 was different: there was a single "msdos" state for all the programs, but it was a limitation, not a feature.

Avindra Goolcharan
  • 3,288
  • 36
  • 37
Giuseppe Guerrini
  • 3,824
  • 15
  • 28
  • To add: the `chdir` syscall changes the dir of the _calling_ process. This means you cannot change the directories of other processes, by design. – Avindra Goolcharan Feb 23 '21 at 21:13
3

It sounds like you're asking one process (your Win32 program) to change the CWD of another process (your shell). As far as I know, this is impossible without the latter process providing an API for such a purpose. The nearest I can come to any sort of reference for this assertion, however, is the following quote from MSDN:

A parent process can directly alter the environment variables of a child process during process creation. This is the only situation when a process can directly change the environment settings of another process.

ladenedge
  • 12,165
  • 10
  • 56
  • 106
2

Well yeah it's true the popular API calls to change directory change it for the process. ... BUT ...

(1.) 16-bit windows programs can change the global directory; probably because they run in the same process as the command.com thing. That's what I've been happily using for years; I assume XP somehow emulates this? ... But now Windows 7 64-bit won't run 16-bit programs anymore! (?)

(2.) Both Windows and Unix "cd" commands can of course change directories for the calling process -- presumably because they are built-in commands of the command shell. But successor Windows shells manage to accomplish this, or at least I hope PowerShell can do that. All built-ins?

(3.) The way I've wound-up doing it is modifying my programs that used to call the API to simply emit "cd \dst\directory" to stdout, then in a procedure do

chdirprogram >t~.bat

call T~.bat

Which works great. And of course the usual point of a change-directory program is to provide the functionality in a batch procedure with a computed destination. Which of course you can do in Unix with Bash etc. variables, but not in Windows batch files, although maybe (?) in the numerous successor Windows procedure things, which I don't want to use. ... Since this functionality is obviously useful, I was hoping someone knew of a sneaky Windows call what'd do it. The explanation that it's somehow wrong for a process to change the directory for a calling process is one of those bogus, "you're not supposed to do that and I won't tell you why" excuses. ... But I guess I'll just stick to my pitiful little batch files.

0

Are you talking about the SetCurrentDirectory function of Windows API? The article says that the function "changes the current directory for the current process". In for instance Delphi, there is a function ChDir that actually calls this API function.

Andreas Rejbrand
  • 95,177
  • 8
  • 253
  • 351