如何获取 Windows“特殊文件夹"的路径?对于特定用户?

2021-12-17 00:00:00 windows winapi c++

在服务内部,确定特定用户的特殊文件夹路径(例如我的文档")的最佳方法是什么?SHGetFolderPath 允许您传入令牌,因此我假设有某种方法可以模拟您感兴趣的文件夹的用户.

Inside a service, what is the best way to determine a special folder path (e.g., "My Documents") for a specific user? SHGetFolderPath allows you to pass in a token, so I am assuming there is some way to impersonate the user who's folder you are interested in.

有没有办法仅基于用户名来做到这一点?如果没有,您需要为用户帐户提供的最少信息量是多少?我宁愿不需要用户的密码.

Is there a way to do this based just on a username? If not, what is the minimum amount of information you need for the user account? I would rather not have to require the user's password.

(这是一个相关问题.)

推荐答案

我会挂载用户的注册表配置单元并查找路径值.是的,由于提到的所有原因(前向兼容性差等),这是一个次优的解决方案.然而,就像 Windows 中的许多其他东西一样,MS 没有提供 API 方式来做你想做的事情,所以它是最好的选择.

I would mount the user's registry hive and look for the path value. Yes, it's a sub-optimal solution, for all the reasons mentioned (poor forwards compatibility, etc.). However, like many other things in Windows, MS didn't provide an API way to do what you want to do, so it's the best option available.

您可以使用 LookupAccountName 获取用户的 SID(而非 GUID).您可以使用 LoadUserProfile 加载用户的注册表配置单元,但不幸的是这也需要一个用户令牌,这将需要他们的密码.幸运的是,您可以使用 RegLoadKey 到任意位置,读取数据,然后卸载它(我认为).

You can get the SID (not GUID) of the user by using LookupAccountName. You can load the user's registry hive using LoadUserProfile, but unfortunately this also requires a user token, which is going to require their password. Fortunately, you can manually load the hive using RegLoadKey into an arbitrary location, read the data, and unload it (I think).

是的,这很痛苦,是的,它可能会在未来版本的 Windows 中崩溃.也许到那时 MS 会提供一个 API 来做这件事,将它移植到旧版本的 Windows 中,并通过 Windows 更新自动分发它......但我不会屏住呼吸.

Yes, it's a pain, and yes, it's probably going to break in future versions of Windows. Perhaps by that time MS will have provided an API to do it, back-ported it into older versions of Windows, and distributed it automatically through Windows update... but I wouldn't hold my breath.

附言此信息旨在补充您的相关问题中提供的信息,包括免责声明.

P.S. This information intended to augment the information provided in your related question, including disclaimers.

相关文章