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_Shell_Common",
"Win32_UI_WindowsAndMessaging", "Win32_UI_WindowsAndMessaging",
"Win32_System_Com", "Win32_System_Com",
"Win32_System_Registry",
] ]
[target.'cfg(target_os = "ios")'.dependencies] [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::{ use windows::{
core::{w, HSTRING, PCWSTR}, core::{w, HSTRING, PCWSTR},
Win32::{ Win32::{
Foundation::{ERROR_FILE_NOT_FOUND, HWND}, Foundation::ERROR_FILE_NOT_FOUND,
System::Com::CoInitialize, System::Com::CoInitialize,
UI::{ UI::{
Shell::{ILCreateFromPathW, ILFree, SHOpenFolderAndSelectItems, ShellExecuteW}, Shell::{
WindowsAndMessaging::SW_SHOW, 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 = HSTRING::from(dir);
let dir_item = unsafe { ILCreateFromPathW(PCWSTR::from_raw(dir.as_ptr())) }; let dir_item = unsafe { ILCreateFromPathW(PCWSTR::from_raw(dir.as_ptr())) };
let file = HSTRING::from(file); let file_h = HSTRING::from(file);
let file_item = unsafe { ILCreateFromPathW(PCWSTR::from_raw(file.as_ptr())) }; let file_item = unsafe { ILCreateFromPathW(PCWSTR::from_raw(file_h.as_ptr())) };
unsafe { unsafe {
if let Err(e) = SHOpenFolderAndSelectItems(dir_item, Some(&[file_item]), 0) { if let Err(e) = SHOpenFolderAndSelectItems(dir_item, Some(&[file_item]), 0) {
if e.code().0 == ERROR_FILE_NOT_FOUND.0 as i32 { if e.code().0 == ERROR_FILE_NOT_FOUND.0 as i32 {
ShellExecuteW( let is_dir = std::fs::metadata(file).map(|f| f.is_dir()).unwrap_or(false);
HWND::default(), let mut info = SHELLEXECUTEINFOW {
w!("open"), cbSize: std::mem::size_of::<SHELLEXECUTEINFOW>() as _,
PCWSTR::from_raw(dir.as_ptr()), nShow: SW_SHOWNORMAL.0,
PCWSTR::null(), lpVerb: if is_dir {
PCWSTR::null(), w!("explore")
SW_SHOW, } 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