如何以编程方式访问 Windows 符号链接的目标路径? [英] How do I programmatically access the target path of a windows symbolic link?

查看:35
本文介绍了如何以编程方式访问 Windows 符号链接的目标路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows 6(Vista 和 Server 2008)支持适当的符号链接,可以通过 CreateSymbolicLink 函数.但是似乎没有相应的函数来查询符号链接以获取链接目标的路径.

Windows 6 (Vista and Server 2008) support proper symbolic links, which can be created via the CreateSymbolicLink function. But there doesn't appear to be a corresponding function for interrogating a symbolic link to obtain the path of the link's target.

我发现符号链接是重解析点的实现,因此可以使用重解析点函数来获取目标路径.但是我需要使用重新分析点的头文件似乎与 Windows 驱动程序工具包一起提供.使用 VS2008 设置这个工具包似乎是一项不平凡的任务.

I have found out that symbolic links are an implementation of reparse points, and so the reparse point functions can be used to obtain the target path. But the header files that I need to use reparse points appear to come with the Windows Driver Kit. Setting up this kit with VS2008 appears to be a non trivial task.

是否有我在获取链接目标时遗漏了一个很好的简单函数,或者我真的必须设置一个 Windows 驱动程序开发环境来编写代码来访问这些信息?

Is there a nice simple function that I've missed for obtaining a link's target, or do I really have to set up a windows driver development environment just to write code to access this information?

Adam Mitz 提出了 GetFinalPathNameByHandle 的建议.此函数非常适用于本地符号链接,但似乎不适用于解析远程链接(通过 UNC 路径).

Adam Mitz came up with the suggestion of GetFinalPathNameByHandle. This function works just great for local symlinks, but doesn't appear to work for resolving remote links (via a UNC path).

编辑 2:应亚当的要求,这里是我尝试过的更多细节:

EDIT 2: At Adam's request, here are more details of what I've tried:

我最初沿着 FSCTL_GET_REPARSE_POINT/DeviceIoControl 路线走下去,但这会产生一个 REPARSE_DATA_BUFFER 结构.定义此结构的标头似乎仅存在于 Windows 驱动程序工具包中.

I initially went down the FSCTL_GET_REPARSE_POINT/ DeviceIoControl route, but that yields a REPARSE_DATA_BUFFER structure. The headers that define this structure seem to exist solely within the Windows Driver Kit.

GetFinalPathNameByHandle() 当链接存在于本地磁盘(C:\...\link 等)时工作正常.奇怪的是,我发现无论是否指定了 FILE_FLAG_OPEN_REPARSE_POINT 标志,我都可以获得链接的句柄 - 从而获得目标 - 使用 CreateFileW()目标文件存在.

GetFinalPathNameByHandle() works fine when the link exists on a local disk (C:\...\link etc). Curiously, I found that I could obtain the handle to the link - and thus get the target - using CreateFileW() whether the FILE_FLAG_OPEN_REPARSE_POINT flag was specified or not, regardless of whether the target file exists.

CreateFileW()GetFinalPathNameByHandle() 用于查询远程链接时,虽然 (\\?\UNC\....),事情开始瓦解.如果指定了 FILE_FLAG_OPEN_REPARSE_POINTGetFinalPathNameByHandle() 总是返回链接路径,而不是目标路径.如果未指定 FILE_FLAG_OPEN_REPARSE_POINT,则返回目标路径,但前提是目标存在且与链接位于同一台机器上.如果链接指向另一台机器,我会收到网络权限错误.如果链接指向一个不存在的本地文件,我会收到文件未找到错误.

When CreateFileW() and GetFinalPathNameByHandle() are used to interrogate a remote link though (\\?\UNC\....), things start to unravel. If FILE_FLAG_OPEN_REPARSE_POINT is specified, GetFinalPathNameByHandle() always returns the link path, not the target path. If FILE_FLAG_OPEN_REPARSE_POINT is not specified, then the target path is returned, but only if the target exists and is on the same machine as the link. If the link points to another machine, I get a network permissions error. If the link points to a local - non-existent - file, I get a file not found error.

推荐答案

GetFinalPathNameByHandle

最终路径是当路径完全时返回解决.例如,对于一个象征性的名为C:\tmp\mydir"的链接指向到D:\yourdir",最终的文件系统路径将是D:\yourdir".

A final path is the path that is returned when a path is fully resolved. For example, for a symbolic link named "C:\tmp\mydir" that points to "D:\yourdir", the final filesystem path would be "D:\yourdir".

这篇关于如何以编程方式访问 Windows 符号链接的目标路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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