Skip to main content

ib_shell_item/item/
item2.rs

1use windows::{
2    Win32::UI::Shell::{
3        Common::ITEMIDLIST,
4        PropertiesSystem::{GETPROPERTYSTOREFLAGS, IPropertyStore},
5        SHCreateItemFromIDList, SHCreateItemFromParsingName,
6    },
7    core::{PCWSTR, Result},
8};
9
10pub use windows::Win32::UI::Shell::IShellItem2;
11
12pub trait ShellItem2 {
13    /// [SHCreateItemFromParsingName function (shobjidl_core.h)](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-shcreateitemfromparsingname)
14    ///
15    /// Although not documented, this requires `CoInitialize()`.
16    fn from_path_w(path: PCWSTR) -> Result<IShellItem2>;
17
18    /// [SHCreateItemFromIDList function (shobjidl_core.h)](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-shcreateitemfromidlist)
19    #[doc(alias = "from_pidl")]
20    fn from_id_list(id_list: *const ITEMIDLIST) -> Result<IShellItem2>;
21
22    /// [IShellItem2::GetPropertyStore (shobjidl_core.h)](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishellitem2-getpropertystore)
23    fn get_property_store(&self, flags: GETPROPERTYSTOREFLAGS) -> Result<IPropertyStore>;
24}
25
26impl ShellItem2 for IShellItem2 {
27    fn from_path_w(path: PCWSTR) -> Result<IShellItem2> {
28        unsafe { SHCreateItemFromParsingName::<_, _, IShellItem2>(path, None) }
29    }
30
31    fn from_id_list(id_list: *const ITEMIDLIST) -> Result<IShellItem2> {
32        unsafe { SHCreateItemFromIDList::<IShellItem2>(id_list) }
33    }
34
35    fn get_property_store(&self, flags: GETPROPERTYSTOREFLAGS) -> Result<IPropertyStore> {
36        unsafe { self.GetPropertyStore(flags) }
37    }
38}