use ShellExecuteExW

feat/shell-show-item-in-dir
amrbashir 7 months ago
parent 539d41ace7
commit bc53cf11db
No known key found for this signature in database
GPG Key ID: BBD7A47A2003FF33

@ -46,6 +46,7 @@ features = [
"Win32_UI_Shell_Common",
"Win32_UI_WindowsAndMessaging",
"Win32_System_Com",
"Win32_System_Registry",
]
[target.'cfg(target_os = "ios")'.dependencies]

@ -0,0 +1,3 @@
use std::path::Path;
pub fn show_item_in_directory(file: &Path) -> crate::Result<()> {}

@ -3,11 +3,14 @@ use std::path::Path;
use windows::{
core::{w, HSTRING, PCWSTR},
Win32::{
Foundation::{ERROR_FILE_NOT_FOUND, HWND},
Foundation::ERROR_FILE_NOT_FOUND,
System::Com::CoInitialize,
UI::{
Shell::{ILCreateFromPathW, ILFree, SHOpenFolderAndSelectItems, ShellExecuteW},
WindowsAndMessaging::SW_SHOW,
Shell::{
ILCreateFromPathW, ILFree, SHOpenFolderAndSelectItems, ShellExecuteExW,
SHELLEXECUTEINFOW,
},
WindowsAndMessaging::SW_SHOWNORMAL,
},
},
};
@ -22,20 +25,27 @@ pub fn show_item_in_directory(file: &Path) -> crate::Result<()> {
let dir = HSTRING::from(dir);
let dir_item = unsafe { ILCreateFromPathW(PCWSTR::from_raw(dir.as_ptr())) };
let file = HSTRING::from(file);
let file_item = unsafe { ILCreateFromPathW(PCWSTR::from_raw(file.as_ptr())) };
let file_h = HSTRING::from(file);
let file_item = unsafe { ILCreateFromPathW(PCWSTR::from_raw(file_h.as_ptr())) };
unsafe {
if let Err(e) = SHOpenFolderAndSelectItems(dir_item, Some(&[file_item]), 0) {
if e.code().0 == ERROR_FILE_NOT_FOUND.0 as i32 {
ShellExecuteW(
HWND::default(),
w!("open"),
PCWSTR::from_raw(dir.as_ptr()),
PCWSTR::null(),
PCWSTR::null(),
SW_SHOW,
);
let is_dir = std::fs::metadata(file).map(|f| f.is_dir()).unwrap_or(false);
let mut info = SHELLEXECUTEINFOW {
cbSize: std::mem::size_of::<SHELLEXECUTEINFOW>() as _,
nShow: SW_SHOWNORMAL.0,
lpVerb: if is_dir {
w!("explore")
} else {
PCWSTR::null()
},
lpClass: if is_dir { w!("folder") } else { PCWSTR::null() },
lpFile: PCWSTR(file_h.as_ptr()),
..std::mem::zeroed()
};
ShellExecuteExW(&mut info)?;
}
}
}

Loading…
Cancel
Save