Win32当前目录,锁定& 外壳集成

发布于 2024-07-15 08:30:35 字数 406 浏览 3 评论 0原文

在 Win32 中,主线程的当前工作目录设置为启动可执行文件的位置。 我的问题是,即使在其他地方调用 SetCurrentDirectory() 后,该进程显然仍然有一个引用此初始启动目录的文件系统对象(可以使用 Process Explorer 等工具进行验证) - 这意味着该目录无法被进程删除。

这里有人知道一个不太hacky的解决方案吗? 我特别遇到了与资源管理器集成的程序的问题(向 HKCR\Directory\shell 注册表项添加动词),我需要处理右键单击目录中的文件并删除源目录,这是不可能的因为初始工作目录设置为(您猜对了)右键单击的目录。

编辑:我将采用“使用 helper launch-from-sane-directory”方法。 它可能不是超级优雅,但它会工作并且不需要任何讨厌的黑客。

In Win32, your main thread's current working directory is set to the location the executable was launched from. My problem is that even after a call to SetCurrentDirectory() to somewhere else, the process apparently still has a filesystem object referencing this initial startup directory (verifiable with a tool like Process Explorer) - which means this director cannot be deleted by the process.

Does anybody here know of a not-too-hacky solution? I'm specifically running into the problem with a program that integrates with explorer (adding a verb to HKCR\Directory\shell registry key), I need to process files in a right-clicked directory and the remove the source directory, which is impossible because the initial working directory is set to, you guessed it, the right-clicked directory.

EDIT: I'll go for the "use helper launch-from-sane-directory" approach. It might not be super elegant, but it will work and doesn't require any nasty hacks.

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

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

发布评论

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

评论(1

怀里藏娇 2024-07-22 08:30:35

最简单的解决方案可能是生成一个小帮助程序进程,该进程在您指定的任何目录(c:\,例如)中运行,然后退出并让它执行其操作。 它可能需要与互斥体同步,或者可能只是在计时器上重试两到三次...

我有另一个想法:您也许可以将 CreateFile() 与 FILE_FLAG_DELETE_ON_CLOSE 一起使用。 然后,当每个人都放开它时,它就会消失,但前提是它是使用 FILE_SHARE_DELETE 打开的。

Your easiest solution may be to just spawn a little helper process that runs in whatever directory you specify (c:\, e.g.) and then just exit and let it do its thing. It may need to be synchronized with a mutex, or perhaps just retry two or three times on a timer...

I had another thought: You may be able to use CreateFile() with FILE_FLAG_DELETE_ON_CLOSE. Then it should go away when everyone lets go of it, but only if it was opened with FILE_SHARE_DELETE.

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