Created
August 8, 2023 16:33
-
-
Save kennykerr/ee2754b13fc2040bbb593cf3b1cff3a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use windows::{ | |
core::HSTRING, | |
w, | |
Win32::{ | |
System::Com::CoInitialize, | |
UI::Shell::{ | |
Common::ITEMIDLIST, ILCreateFromPathW, SHCreateShellItemArray, SIGDN_FILESYSPATH, | |
}, | |
}, | |
}; | |
fn main() -> windows::core::Result<()> { | |
unsafe { | |
CoInitialize(None)?; | |
let child_items: Vec<_> = std::fs::read_dir(r#"C:\Windows\Help"#) | |
.unwrap() | |
.filter_map(|entry| entry.ok()) | |
.filter(|entry| entry.path().is_dir()) | |
.map(|entry| { | |
let wide_path = HSTRING::from(entry.path().as_path()); | |
ILCreateFromPathW(&wide_path) as *const ITEMIDLIST | |
}) | |
.collect(); | |
debug_assert!(!child_items.is_empty()); | |
// NOTE: You may want to use SHParseDisplayName instead | |
let parent_item = ILCreateFromPathW(w!(r#"C:\Windows\Help"#)); | |
debug_assert_ne!(parent_item, std::ptr::null_mut()); | |
let item_array = SHCreateShellItemArray(Some(parent_item), None, Some(&child_items))?; | |
debug_assert!(item_array.GetCount()? > 0); | |
println!( | |
"Items[0] => {}", | |
item_array | |
.GetItemAt(0)? | |
.GetDisplayName(SIGDN_FILESYSPATH)? | |
.display() | |
); | |
// ... | |
// Don't forget to free the items in child_items | |
// ... | |
} | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment