以编程方式 chdir

发布于 2024-08-24 22:08:16 字数 134 浏览 10 评论 0 原文

在 Windows 中(也可能是 Unix),在(32 位)程序中使用 chdir() 函数不会在程序退出时更改目录。 (在 16 位 Windows 程序中是这样。)
有谁知道如何在 Windows 32 位程序中做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

铁憨憨 2024-08-31 22:08:16

嗯...恕我直言,这正是操作系统必须保证不会发生的事情之一。当前目录是每个进程的属性,子进程通常从父进程继承它,但相反的情况不应该发生(而且也没有发生)。

为了获得你想要的,父进程可以主动监视子进程存储新目录的一些信息(消息、文件、共享内存...),然后使用新值调用 chdir() 。

据我所知,Windows的cmd.exe没有任何类似的机制。
实际上,通过在父进程上使用代码注入技术(例如CreateRemoteThread),可以强迫它做一些意想不到的事情,但这是一个非常肮脏的伎俩,一点都不好,也不一般。
Win16 不同:所有程序都有一个“msdos”状态,但这是一个限制,而不是一个功能。

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.

一抹微笑 2024-08-31 22:08:16

听起来您正在要求一个进程(您的 Win32 程序)更改另一进程(您的 shell)的 CWD。据我所知,如果没有后一个进程为此目的提供 API,这是不可能的。然而,我能找到的最接近此断言的任何参考文献是以下引用 来自 MSDN

父进程可以在进程创建过程中直接更改子进程的环境变量。这是一个进程可以直接更改另一个进程的环境设置的唯一情况。

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.

仙女 2024-08-31 22:08:16

是的,确实流行的 API 调用更改目录会更改进程的目录。 ...但是...

(1.) 16 位 Windows 程序可以更改全局目录;可能是因为它们与 command.com 运行在同一进程中。这就是我多年来一直愉快使用的东西;我想 XP 会以某种方式模拟这个吗? ...但现在 Windows 7 64 位将不再运行 16 位程序! (?)

(2.) Windows 和 Unix 的“cd”命令当然可以更改调用进程的目录——大概是因为它们是命令 shell 的内置命令。但后续的 Windows shell 设法实现了这一点,或者至少我希望 PowerShell 能够做到这一点。都是内置的吗?

(3.) 我完成此操作的方法是修改我的程序,该程序用于调用 API,以简单地将“cd \dst\directory”发送到 stdout,然后在过程中执行

chdirprogram >t~.bat

调用T~.bat

效果很好。当然,更改目录程序的通常目的是在批处理过程中提供具有计算目的地的功能。当然,您可以在 Unix 中使用 Bash 等变量执行此操作,但不能在 Windows 批处理文件中执行此操作,尽管也许(?)在众多后续 Windows 程序中,但我不想使用它们。 ...由于此功能显然很有用,我希望有人知道偷偷摸摸的Windows调用会做什么。进程更改调用进程的目录在某种程度上错误的解释是那些虚假的借口之一,“你不应该这样做,我不会告诉你为什么”。 ...但我想我只会坚持我可怜的小批处理文件。

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.

对你的占有欲 2024-08-31 22:08:16

你说的是Windows的SetCurrentDirectory函数API?文章称该函数“更改当前进程的当前目录”。例如在Delphi中,有一个函数ChDir实际上调用了这个API函数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文