如何将 SHCreateItemFromParsingName 与来自 shell 命名空间的名称一起使用? [英] How to use SHCreateItemFromParsingName with names from the shell namespace?
问题描述
我正在使用
如何将用户可能键入的各种特殊 shell 名称位置(Windows 资源管理器和 IFileOpen 对话框可以解析)解析为该文件夹的 IShellItem
?
真正的问题是我希望用户能够拥有一个最近的 MRU 列表,其中包含以下内容:
- C:\Windows
- 回收站
- 这台电脑
并且能够在以后解析它们.
调试 Explorer 并看看它是如何工作的会很有趣.
我的建议是;如果初始解析失败,将 shell:
添加到路径字符串并尝试使用 SHParseDisplayName
再次解析它.如果您设置 STR_PARSE_SHELL_PROTOCOL_TO_FILE_OBJECTS
在绑定上下文中,您还可以绑定到特殊文件.shell: 协议能够解析特殊/已知文件夹的内部/规范名称,但我不知道它是否还会检查显示名称.
我现在有机会玩一下,shell: prefix 并不是一个巨大的改进,因为它只检查已知的文件夹规范名称:
PCWSTR 路径[] = {TEXT("C:\\"),TEXT("C:\\Windows"),文本(""),TEXT("这台电脑"),TEXT("MyComputerFolder"),//规范的 KF 名称TEXT("回收站"),TEXT("RecycleBinFolder"),//规范的 KF 名称TEXT("图书馆"),文本(OneDrive"),TEXT("图书馆\\文件"),TEXT("网络"),TEXT("NetworkPlacesFolder"),//规范的 KF 名称TEXT("启动"),};OleInitialize(0);INT pad = 0, 填充, i;for (i = 0; i GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &p1), pSI->Release();wsprintf(buf, L"shell:%s", paths[i]);HRESULT hr2 = SHCreateItemFromParsingName(buf, NULL, IID_IShellItem, (void**) &pSI);if (SUCCEEDED(hr2)) pSI->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &p2), pSI->Release();wprintf(L"%-*s | %.8x | %.8x | %s\n", pad, paths[i], hr, hr2, p2 && *p2 ? p2 : p1 ? p1 : L"");CoTaskMemFree(p1), CoTaskMemFree(p2);}
给我这个输出:
<代码> |原创 |外壳: |-------------------------------------C:\ |00000000 |80070003 |C:\C:\Windows |00000000 |80070003 |C:\Windows|00000000 |80070003 |::{20D04FE0-3AEA-1069-A2D8-08002B30309D}这台电脑 |80070002 |80070003 |我的电脑文件夹 |80070002 |00000000 |::{20D04FE0-3AEA-1069-A2D8-08002B30309D}回收站 |80070002 |80070003 |回收站文件夹 |80070002 |00000000 |::{645FF040-5081-101B-9F08-00AA002F954E}图书馆 |80070002 |00000000 |::{031E4825-7B94-4DC3-B131-E946B44C8DD5}OneDrive |80070002 |80070003 |图书馆\文件 |80070002 |80070002 |网络|80070002 |80070003 |网络位置文件夹 |80070002 |00000000 |::{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}启动 |80070002 |00000000 |C:\Users\Anders\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
在 Windows 8 上 SHCreateItemFromParsingName
调用 SHParseDisplayName
(使用 STR_PARSE_AND_CREATE_ITEM
和 STR_PARSE_TRANSLATE_ALIASES
),所以即使是微软也很难分离解析并在其 API 中显示名称.
如果您想远离未记录的界面,则必须添加第三遍,以检查已知文件夹的显示名称.或者,正如 Raymond Chen 在评论中所建议的那样;根据该 IShellFolder
中的项目显示名称手动解析每个路径组件.
I am using SHCreateItemFromParsingName
to turn a path into a IShellItem
:
IShellItem ParseName(String path)
{
IShellItem shellItem;
HRESULT hr = SHCreateItemFromParsingName(path, null, IShellItem, out shellItem);
if (Failed(hr))
throw new ECOMException(hr);
return shellItem;
}
Note: A
IShellItem
was introduced around 2006 to provide a handy wrapper around the Windows 95-eraIShellFolder
+pidl
constructs. You can even ask aIShellItem
to cough up it's underlyingIShellFolder
andpidl
with theIParentAndItem.GetParentAndItem
interface and method.
Different things have different display names
I can get ahold of some well-known locations in the shell namespace, and see their absolute parsing (SIGDN_DESKTOPABSOLUTEPARSING
) and editing (SIGDN_DESKTOPABSOLUTEEDITING
) display names:
Path | Editing | Parsing |
---|---|---|
C:\ | "C:" | "C:" |
C:\Windows | "C:\Windows" | "C:\Windows" |
Desktop | "Desktop" | "C:\Users\Ian\Desktop" |
Computer | "This PC" | "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" |
Recycle Bin | "Recycle Bin" | "::{645FF040-5081-101B-9F08-00AA002F954E}" |
Documents Library | "Libraries\Documents" | "::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Documents.library-ms" " |
Startup | "C:\Users\Ian\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" | "C:\Users\Ian\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" |
How to parse them when the user types in them in?
I can use IFileOpenDialog
to let the user select one of these folders. But i'd really like the user to be able to type
- "C:\Users"
- "C:\Windows\Fonts"
- "This PC"
- "Recycle Bin"
- "Libraries"
- "Startup"
- "Fonts"
and be able to parse it into an IShellItem
.
The problem is that some of the paths are not parsed by SHCreateItemFromParsingName
:
SHCreateItemFromParsingName("C:\")
: ParsesSHCreateItemFromParsingName("C:\Windows")
: ParsesSHCreateItemFromParsingName("")
: Parses (but becomes "This PC")SHCreateItemFromParsingName("This PC")
: FailsSHCreateItemFromParsingName("Recycle Bin")
: FailsSHCreateItemFromParsingName("Libraries")
: FailsSHCreateItemFromParsingName("OneDrive")
: FailsSHCreateItemFromParsingName("Libraries\Documents")
: FailsSHCreateItemFromParsingName("Network")
: FailsSHCreateItemFromParsingName("Startup")
: Fails
Meanwhile, the IFileOpenDialog control that my program uses can parse them fine:
How can i parse the various special shell name places that a user might type in (that Windows Explorer and the IFileOpen dialog can parse) into an IShellItem
for that folder?
The real question is that i want the user to be able to have a recent MRU list that contains things like:
- C:\Windows
- Recycle Bin
- This PC
and be able to parse them later.
It would be interesting to debug Explorer and see how it does it.
My suggestion is; if the initial parse fails, prepend shell:
to the path string and try parsing it again with SHParseDisplayName
. If you set STR_PARSE_SHELL_PROTOCOL_TO_FILE_OBJECTS
in the bind context you can also bind to special files. The shell: protocol is able to parse the internal/canonical name of special/known folders but I don't know if it also checks the display name.
Edit:
I had a chance to play around a bit now and the shell: prefix is not a huge improvement because it only checks the known folder canonical names:
PCWSTR paths[] = {
TEXT("C:\\"),
TEXT("C:\\Windows"),
TEXT(""),
TEXT("This PC"),
TEXT("MyComputerFolder"), // Canonical KF name
TEXT("Recycle Bin"),
TEXT("RecycleBinFolder"), // Canonical KF name
TEXT("Libraries"),
TEXT("OneDrive"),
TEXT("Libraries\\Documents"),
TEXT("Network"),
TEXT("NetworkPlacesFolder"), // Canonical KF name
TEXT("Startup"),
};
OleInitialize(0);
INT pad = 0, fill, i;
for (i = 0; i < ARRAYSIZE(paths); ++i) pad = max(pad, lstrlen(paths[i]));
for (i = 1, fill = printf("%-*s | Original | shell: |\n", pad, ""); i < fill; ++i) printf("-"); printf("\n");
for (i = 0; i < ARRAYSIZE(paths); ++i)
{
WCHAR buf[MAX_PATH], *p1 = NULL, *p2 = NULL;
IShellItem*pSI;
HRESULT hr = SHCreateItemFromParsingName(paths[i], NULL, IID_IShellItem, (void**) &pSI);
if (SUCCEEDED(hr)) pSI->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &p1), pSI->Release();
wsprintf(buf, L"shell:%s", paths[i]);
HRESULT hr2 = SHCreateItemFromParsingName(buf, NULL, IID_IShellItem, (void**) &pSI);
if (SUCCEEDED(hr2)) pSI->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &p2), pSI->Release();
wprintf(L"%-*s | %.8x | %.8x | %s\n", pad, paths[i], hr, hr2, p2 && *p2 ? p2 : p1 ? p1 : L"");
CoTaskMemFree(p1), CoTaskMemFree(p2);
}
gives me this output:
| Original | shell: |
-------------------------------------------
C:\ | 00000000 | 80070003 | C:\
C:\Windows | 00000000 | 80070003 | C:\Windows
| 00000000 | 80070003 | ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
This PC | 80070002 | 80070003 |
MyComputerFolder | 80070002 | 00000000 | ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
Recycle Bin | 80070002 | 80070003 |
RecycleBinFolder | 80070002 | 00000000 | ::{645FF040-5081-101B-9F08-00AA002F954E}
Libraries | 80070002 | 00000000 | ::{031E4825-7B94-4DC3-B131-E946B44C8DD5}
OneDrive | 80070002 | 80070003 |
Libraries\Documents | 80070002 | 80070002 |
Network | 80070002 | 80070003 |
NetworkPlacesFolder | 80070002 | 00000000 | ::{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}
Startup | 80070002 | 00000000 | C:\Users\Anders\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
On Windows 8 SHCreateItemFromParsingName
calls SHParseDisplayName
(with STR_PARSE_AND_CREATE_ITEM
and STR_PARSE_TRANSLATE_ALIASES
) so even Microsoft have trouble separating parsing and display names in their API.
If you want to stay away from undocumented interfaces then you would have to add a third pass where you check the known folder display names. Or alternatively as Raymond Chen suggests in the comments; parse every path component manually against item display names in that IShellFolder
.
这篇关于如何将 SHCreateItemFromParsingName 与来自 shell 命名空间的名称一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!