@@ -38,23 +38,34 @@ fn wrap_in_quotes<T: AsRef<OsStr>>(path: T) -> OsString {
38
38
39
39
#[ cfg( feature = "shellexecute-on-windows" ) ]
40
40
pub fn that_detached < T : AsRef < OsStr > > ( path : T ) -> std:: io:: Result < ( ) > {
41
+ use std:: path:: Path ;
42
+
41
43
let path = path. as_ref ( ) ;
42
44
let is_dir = std:: fs:: metadata ( path) . map ( |f| f. is_dir ( ) ) . unwrap_or ( false ) ;
43
45
44
- let path = wide ( path) ;
45
-
46
46
if is_dir {
47
+ let path = dunce:: simplified ( Path :: new ( path) ) ;
48
+ let path = wide ( path) ;
47
49
unsafe { ffi:: CoInitialize ( std:: ptr:: null ( ) ) } ;
48
50
let folder = unsafe { ffi:: ILCreateFromPathW ( path. as_ptr ( ) ) } ;
49
- unsafe { SHOpenFolderAndSelectItems ( folder, Some ( & [ folder] ) , 0 ) ? } ;
50
- return Ok ( ( ) ) ;
51
+ if unsafe { SHOpenFolderAndSelectItems ( folder, Some ( & [ folder] ) , 0 ) } . is_ok ( ) {
52
+ return Ok ( ( ) ) ;
53
+ }
54
+ } ;
55
+
56
+ let path = wide ( path) ;
57
+
58
+ let ( verb, class) = if is_dir {
59
+ ( ffi:: EXPLORE , ffi:: FOLDER )
60
+ } else {
61
+ ( std:: ptr:: null ( ) , std:: ptr:: null ( ) )
51
62
} ;
52
63
53
64
let mut info = ffi:: SHELLEXECUTEINFOW {
54
65
cbSize : std:: mem:: size_of :: < ffi:: SHELLEXECUTEINFOW > ( ) as _ ,
55
66
nShow : ffi:: SW_SHOWNORMAL ,
56
- lpVerb : std :: ptr :: null ( ) ,
57
- lpClass : std :: ptr :: null ( ) ,
67
+ lpVerb : verb ,
68
+ lpClass : class ,
58
69
lpFile : path. as_ptr ( ) ,
59
70
..unsafe { std:: mem:: zeroed ( ) }
60
71
} ;
@@ -145,6 +156,12 @@ mod ffi {
145
156
/// <https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow>
146
157
pub const SW_SHOWNORMAL : i32 = 1 ;
147
158
159
+ /// Null-terminated UTF-16 encoding of `explore`.
160
+ pub const EXPLORE : * const u16 = [ 101 , 120 , 112 , 108 , 111 , 114 , 101 , 0 ] . as_ptr ( ) ;
161
+
162
+ /// Null-terminated UTF-16 encoding of `folder`.
163
+ pub const FOLDER : * const u16 = [ 102 , 111 , 108 , 100 , 101 , 114 , 0 ] . as_ptr ( ) ;
164
+
148
165
// Taken from https://docs.rs/windows-sys/latest/windows_sys/
149
166
#[ cfg_attr( not( target_arch = "x86" ) , repr( C ) ) ]
150
167
#[ cfg_attr( target_arch = "x86" , repr( C , packed( 1 ) ) ) ]
0 commit comments