显示对话框时如何防止 GetOpenFileName 更改当前目录? [英] How to prevent GetOpenFileName from changing the current directory while the dialog is shown?

查看:118
本文介绍了显示对话框时如何防止 GetOpenFileName 更改当前目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GetOpenFileName(对于有问题的原因)在显示对话框时更改应用程序的当前目录.这可以在对话框关闭时通过指定 OFN_NOCHANGEDIR 作为对话框初始化标志来重置:

GetOpenFileName (for questionable reasons) changes the current directory of an application while the dialog is shown. This can be reset on dialog closure by specifying OFN_NOCHANGEDIR as dialog initialization flag:

OFN_NOCHANGEDIR 如果用户在搜索文件时更改了目录,则将当前目录恢复为其原始值.

OFN_NOCHANGEDIR Restores the current directory to its original value if the user changed the directory while searching for files.

但是,设置此标志不会阻止函数在显示资源管理器对话框时更改当前目录.

Setting this flag, however, doesn't prevent the function from changing the current directory while the explorer dialog is shown.

这是多线程环境中的一个问题,其中其他线程依赖当前目录来保持可执行文件的路径.

This is an issue in multithreaded environments where other threads rely on the current directory to remain being the executable's path.

有没有办法防止GetOpenFileName 在显示资源管理器对话框和用户浏览文件夹时更改应用程序的当前目录?

Is there a way to prevent GetOpenFileName from changing the current directory of an application while the explorer dialog is shown and the user browses through folders?

推荐答案

这里有几个选项:

  1. 使用 detours 或 MinHook 来挂钩 SetCurrentDirectory 并使其对您的进程没有任何作用(丑陋)
  2. 使用 自定义文件选择器 不会更改当前目录.(更好)
  3. 在您的代码中删除对当前目录的依赖,因为您可能会遇到与其相关的其他错误,尤其是在多线程环境中.(最好)
  1. Use detours or MinHook to hook SetCurrentDirectory and make it do nothing for your process (ugly)
  2. Use a custom file chooser that does not change current directory. (better)
  3. Remove dependencies on current directory in your code, as you're likely to run into other bugs related to it, especially in multi-threaded environment. (best)

这篇关于显示对话框时如何防止 GetOpenFileName 更改当前目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆