使用 Win32/Shell PathCombine() 从 Vista 中的相对路径获取绝对路径似乎失败

发布于 2024-07-10 03:58:24 字数 508 浏览 12 评论 0原文

不确定这是否是预期的行为或错误或我正在使用的错误函数,但问题是 PathCombine() 在 Vista 盒子上返回错误的路径。

相对路径为(由 WMP 导出到播放列表):

..\..\..\Public\Music\Sample Music\Amanda.wma

它的相对路径为:

C:\Users\userX\Music\Playlists\playlist.wpl

和 PathCombine() 返回:

C:\Users\userX\Public\Music\Sample Music\Amanda.wma

但是,该文件实际上位于此处(根据资源管理器判断以及我无法从代码中打开它的事实):

C:\Users\Public\Music\Sample Music\Amanda.wma

是这是一个已知问题? 我还应该使用其他功能吗?

Not sure if this is intended behavior or a bug or a wrong function that I'm using, but the problem is that PathCombine() returns a wrong path on a Vista box.

The relative path is (as exported by the WMP to a playlist):

..\..\..\Public\Music\Sample Music\Amanda.wma

The path it's relative to is:

C:\Users\userX\Music\Playlists\playlist.wpl

and PathCombine() returns:

C:\Users\userX\Public\Music\Sample Music\Amanda.wma

however, the file is actually located here (judging by the Explorer and the fact that I can't open it from the code):

C:\Users\Public\Music\Sample Music\Amanda.wma

Is this a known issue? Is there some other function I should be using?

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

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

发布评论

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

评论(2

冰雪之触 2024-07-17 03:58:24

PathCombine 文档 指定第二个参数, lpszDir,是“指向包含目录路径的最大长度 MAX_PATH 的以 null 结尾的字符串的指针。” 您似乎正在传递目录内文件的完全限定名称,而不是目录的完全限定名称。 因此,它删除三个组件:playlist.wpl、播放列表和音乐,然后附加其余部分。

您应该能够使用 PathRemoveFileSpec 删除目录路径中的文件部分。

The documentation for PathCombine specifies that the second parameter, lpszDir, is "A pointer to a null-terminated string of maximum length MAX_PATH that contains the directory path." You appear to be passing the fully qualified name of a file inside the directory, instead of the fully qualified name of the directory. So, it removes three components: playlist.wpl, Playlists, and Music, and then appends the remainder.

You should be able to use PathRemoveFileSpec to remove the file part from your directory path.

月依秋水 2024-07-17 03:58:24

当您组合两个字符串时,您会得到以下结果。

C:\Users\userX\Music\Playlists\playlist.wpl\..\..\..\Public\Music\Sample Music\Amanda.wma

由于每个“..”都会清除前面的部分,因此您可以使用以下序列结束 u[p:

C:\Users\userX\Music\Playlists\playlist.wpl\..\..\..\Public\Music\Sample Music\Amanda.wma
C:\Users\userX\Music\Playlists\..\..\Public\Music\Sample Music\Amanda.wma
C:\Users\userX\Music\..\Public\Music\Sample Music\Amanda.wma
C:\Users\userX\Public\Music\Sample Music\Amanda.wma

这是因为“PathCombine()”不会被是否有任何路径的各个部分是文件或目录。 这只是一种相对愚蠢的方式,将特殊导航字符(“..”和“.”)与真实导航段进行匹配,以形成没有这些特殊导航字符的路径。

它只是假设“playlist.wpl”是您的情况下的目录名称。 删除它(或在相对路径的开头添加另一个“..”,这是避免删除文件名部分的无关代码的技巧),它应该可以正常工作。

When you combine your two strings, you get the following.

C:\Users\userX\Music\Playlists\playlist.wpl\..\..\..\Public\Music\Sample Music\Amanda.wma

Since each ".." will wipe out the preceding section, you end u[p with the following sequence:

C:\Users\userX\Music\Playlists\playlist.wpl\..\..\..\Public\Music\Sample Music\Amanda.wma
C:\Users\userX\Music\Playlists\..\..\Public\Music\Sample Music\Amanda.wma
C:\Users\userX\Music\..\Public\Music\Sample Music\Amanda.wma
C:\Users\userX\Public\Music\Sample Music\Amanda.wma

That's because "PathCombine()" is not bothered by whether any segments of your path are files or directories. It's just a relatively dumb way of matching special navigation characters (".." and ".") against real navigation segments to form a path without those special navigation characters.

It's just assuming that "playlist.wpl" is a directory name in your case. Strip that off (or add another ".." at the start of your relative path, a trick to avoid extraneous code for stripping of the filename section) and it should work okay.

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