SHGetDesktopFolder的使用

本文深入解析 Windows Shell 扩展的概念、注册过程及实际应用,包括如何通过 COM 对象增强 Windows Explorer 的功能。重点讨论了 Shell 扩展的两大部分:Shell 和 Extension,以及如何通过注册 DLL 来实现特定事件触发的自定义功能。此外,文章还详细解释了关联文件与文件夹的方法,并介绍了如何利用 PIDL 路径在 Windows Shell 中定位文件和文件夹。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一部分 SHELL基本概念

    Windows外壳扩展(Windows Shell Extension),是一类特殊的COM对象,在这类COM对象中用户可以加入自己的特殊功能,而Windows外壳扩展最终都会被Windows Explorer所引用[1]

    A shell extension is a COM object that adds some kind of functionality to the Windows shell (Explorer).

    There are two parts in the term "shell extension." Shell refers to Explorer, and extension refers to code you write that gets run by Explorer when a predetermined event happens (e.g., a right-click on a .DOC file). So a shell extension is a COM object that adds features to Explorer.[7]

    A shell extension is an in-process server that implements some interfaces that handle the communication with Explorer. ATL is the easiest way to get an extension up and running quickly, since without it you'd be stuck writing QueryInterface() and AddRef() code over and over. It is also much easier to debug extensions on Windows NT-based OSes, as I will explain later.

    “Shell 扩展从字面上分两个部分:Shell 与 ExtensionShell 指 Windows Explorer,而Extension 则指由你编写的当某一预先约定好的事件(如在以. doc为后缀的文件图标上单击右键)发生时由 Explorer调用执行的代码。因此一个“Shell 扩展就是一个为 Explorer 添加功能的 COM 对象。

    动态库必须注册才能使用。除了使用 regasm 来注册 DLL 以外,还应该在代码中增加 RegisterServer 和 UnregisterServer 方法,以指导 DLL 注册时,在 Windows 注册表中增加什么键。关于具体键以下做简单说明:

    1) 注册DLLShell Extensions。具体位置是 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved,增加以GUID 为名称的键,值则是动态库说明。(此位置里面全是 Shell 扩展的动态库注册,许多相关软件就是从里面获取信息,例如 ShexView

    2) 关联文件。Shell扩展一般是针对文件或者文件夹的,因此必须关联;许多人都熟知“HKEY_CLASSES_ROOT\*”的作用,就是用来关联所有文件。而文件夹则是“HKEY_CLASSES_ROOT\Folder”[3]

    我们所看到的资源管理器以及整个桌面,都是一个 Shell。在win32中是以外壳名字空间的形式来组织文件系统的,在外壳名字空间里的每一个对象()都实现了一个IShellFolder的接口,通过这个接口我们可以直接查询或间接得到其他相关的接口。

    (注:这里的对象指的是外壳名字空间中的一个节点,对象有可能是一个文件夹,有可能是一个文件,也有可能是一个虚拟文件夹,例如:我的电脑,网上邻居,控制面板等)[4]

    A shell extension is an in-process server that implements some interfaces that handle the communication with Explorer. ATL is the easiest way to get an extension up and running quickly, since without it you'd be stuck writing QueryInterface() and AddRef() code over and over. It is also much easier to debug extensions on Windows NT-based OSes[7]

    在外壳编程中,要使用 PIDL 路径代替普通路径桌面是最顶级的文件夹,外壳名字空间中其他各项都可以用从桌面开始的 PIDL 加以表示。

通过API SHGetDesktopFolder获取桌面的 PIDL 和其 IShellFolder 接口。

通过桌面,来获取“C:\”这个路径的 PIDL 和 IShellFolder 接口,可以通过 IShellFolder 的 ParseDisplayName 和 BindToObject 函数。

### 回答1: 可以使用Python的win32com API来调用Windows API,实现在Windows文件资源管理器中给文件夹添加备注的具体代码如下:import win32com.clientshell = win32com.client.Dispatch("Shell.Application") folder = shell.NameSpace("c:\\myfolder") for file in folder.Items(): if folder.GetDetailsOf(file, 0).endswith("txt"): folder.SetDetailsOf(file, 21, "This is a text file.") ### 回答2: 要使用Python调用Windows API,在文件资源管理器中给文件夹添加备注,可以使用ctypes库来实现。下面是详细的代码实现: ```python import ctypes import comtypes.client # 获取Shell32库 shell32 = ctypes.windll.shell32 # 定义文件夹路径和备注内容 folder_path = r"C:\Path\To\Folder" comment = "这是一个文件夹备注" # 创建文件夹属性对象 folder_property = comtypes.client.CreateObject("{D6886603-9D2D-4E71-9E7F-217B9207ADA1}") # 设置备注内容 folder_property.SetValue("Comments", comment) # 将备注内容写入文件夹 shell_folder = shell32.SHGetDesktopFolder() folder_item = shell_folder.ParseDisplayName(0, None, folder_path, None)[0] folder_property_storage = folder_item.QueryInterface(comtypes.client.IID_IPropertySetStorage) property_storage = folder_property_storage.Open(comtypes.client.FMTID_Storage, comtypes.client.STGM_READWRITE) property_storage.WriteMultiple([2], [folder_property]) # 关闭文件夹属性对象 property_storage.Commit() property_storage = None folder_property_storage = None folder_item = None shell_folder = None shell32 = None ``` 请注意,以上代码中的`folder_path`变量应该是一个有效的文件夹路径,而`comment`变量是要添加的文件夹备注内容。 这段代码的实现步骤如下: 1. 导入必要的库(ctypes和comtypes.client)。 2. 使用ctypes加载shell32库。 3. 定义文件夹路径和备注内容。 4. 创建文件夹属性对象。 5. 设置备注内容。 6. 打开文件夹并获取文件夹属性存储对象。 7. 将备注内容写入文件夹。 8. 关闭文件夹属性存储对象。 请注意,以上代码块仅适用于Python 3.x版本,并且需要安装comtypes库。 ### 回答3: 要在Windows的文件资源管理器中给文件夹添加备注,可以使用Python调用Windows API来实现。下面是一个实现的示例代码: ```python import ctypes def add_folder_comment(folder_path, comment): # 定义文件资源管理器的 GUID CLSID_ShellLink = '{00021401-0000-0000-C000-000000000046}' IID_IShellLinkW = '{000214F9-0000-0000-C000-000000000046}' # 加载 shell32.dll shell32 = ctypes.windll.shell32 # 创建 IShellLink 对象 shell_link = ctypes.POINTER(ctypes.c_void_p)() shell32.CoCreateInstance(ctypes.byref(ctypes.wintypes.GUID(CLSID_ShellLink)), None, ctypes.c_ulong(1), ctypes.byref(ctypes.wintypes.GUID(IID_IShellLinkW)), ctypes.byref(shell_link)) # 获取 IPersistFile 接口 persist_file = ctypes.cast(shell_link, ctypes.POINTER(ctypes.c_void_p)) # 转换为原始类型以获取指针 persist_file = ctypes.cast(persist_file.contents.value, ctypes.POINTER(ctypes.c_void_p)) # 设置要备注的文件夹路径和备注信息 shell_link.SetPath(folder_path) shell_link.SetDescription(comment) # 保存备注 persist_file.Save(None, True) # 使用示例 folder_path = r'C:\path\to\folder' # 要给备注的文件夹路径 comment = '这是一个文件夹备注' # 文件夹备注信息 add_folder_comment(folder_path, comment) ``` 这段代码使用了`ctypes`模块来调用Windows API。首先,它加载了`shell32.dll`并创建了`IShellLink`接口的对象。然后,通过`SetPath`和`SetDescription`方法设置了文件夹路径和备注信息。最后,通过`Save`方法保存备注。 你可以将文件夹路径和备注信息替换为实际的值,然后运行这段代码,它将在Windows的文件资源管理器中为指定的文件夹添加备注。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值