Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chdir programmatically

Tags:

windows

chdir

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?


1 Answers

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.

like image 143
Giuseppe Guerrini Avatar answered Jan 29 '26 11:01

Giuseppe Guerrini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!