-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
196 lines (166 loc) · 6.55 KB
/
main.rs
File metadata and controls
196 lines (166 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#![windows_subsystem = "windows"]
use log::{info, warn, error};
use log4rs;
use log4rs::{append::file::FileAppender, config::{Appender, Root}, encode::pattern::PatternEncoder, Config};
use windows::core::{Interface, Result, w};
use windows::Win32::{
System::{
Com::{
CoCreateInstance, CoInitializeEx, CLSCTX_LOCAL_SERVER,
COINIT_APARTMENTTHREADED, IServiceProvider,
},
Variant::VARIANT,
SystemServices::SFGAO_FILESYSTEM,
},
UI::Shell::{
IShellBrowser, IShellWindows, IShellView, IShellItem, IFolderView,
ShellWindows, SIGDN_FILESYSPATH, SIGDN_DESKTOPABSOLUTEPARSING, SVGIO_SELECTION,
IShellItemArray
},
UI::WindowsAndMessaging::{GetForegroundWindow, FindWindowExW, MessageBoxW, MB_ICONINFORMATION, MB_OK},
};
use windows::core::PCWSTR;
use windows::Win32::System::Com::IDispatch;
unsafe fn get_selected_file_from_explorer() -> Result<String> {
let _ = CoInitializeEx(None, COINIT_APARTMENTTHREADED);
let hwnd_gfw = GetForegroundWindow();
let shell_windows: IShellWindows =
CoCreateInstance(&ShellWindows, None, CLSCTX_LOCAL_SERVER)?;
let result_hwnd = FindWindowExW(Some(hwnd_gfw), None, w!("ShellTabWindowClass"), None)?;
let mut target_path = String::new();
let count = shell_windows.Count().unwrap_or_default();
for i in 0..count {
let variant = VARIANT::from(i);
let dispatch: IDispatch = shell_windows.Item(&variant)?;
let shell_browser = dispath2browser(dispatch);
if shell_browser.is_none() {
continue;
}
let shell_browser = shell_browser.unwrap();
// 调用 GetWindow 可能会阻塞 GUI 消息
let phwnd = shell_browser.GetWindow()?;
if hwnd_gfw.0 != phwnd.0 && result_hwnd.0 != phwnd.0 {
continue;
}
let shell_view = shell_browser.QueryActiveShellView().unwrap();
target_path = get_base_location_from_shellview(shell_view); // get_selected_file_path_from_shellview(shell_view);
}
info!("get_selected_file_from_explorer: {}",target_path);
Ok(target_path)
}
unsafe fn dispath2browser(dispatch: IDispatch) -> Option<IShellBrowser> {
let mut service_provider: Option<IServiceProvider> = None;
dispatch
.query(
&IServiceProvider::IID,
&mut service_provider as *mut _ as *mut _,
)
.ok()
.unwrap();
if service_provider.is_none() {
return None;
}
let shell_browser = service_provider
.unwrap()
.QueryService::<IShellBrowser>(&IShellBrowser::IID)
.ok();
shell_browser
}
unsafe fn get_selected_file_path_from_shellview(shell_view: IShellView) -> String {
let mut target_path = String::new();
let shell_items = shell_view.GetItemObject::<IShellItemArray>(SVGIO_SELECTION);
if shell_items.is_err() {
return target_path;
}
info!("shell_items: {:?}", shell_items);
let shell_items = shell_items.unwrap();
let count = shell_items.GetCount().unwrap_or_default();
for i in 0..count {
let shell_item = shell_items.GetItemAt(i).unwrap();
// 如果不是文件对象则继续循环
if let Ok(attrs) = shell_item.GetAttributes(SFGAO_FILESYSTEM) {
log::info!("attrs: {:?}", attrs);
if attrs.0 == 0 {
continue;
}
}
if let Ok(display_name) = shell_item.GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING)
{
let tmp = display_name.to_string();
if tmp.is_err() {
continue;
}
target_path = tmp.unwrap();
break;
}
if let Ok(display_name) = shell_item.GetDisplayName(SIGDN_FILESYSPATH) {
println!("display_name: {:?}", display_name);
let tmp = display_name.to_string();
if tmp.is_err() {
println!("display_name error: {:?}", tmp.err());
continue;
}
target_path = tmp.unwrap();
break;
}
}
target_path
}
unsafe fn get_base_location_from_shellview(shell_view: IShellView) -> String {
let mut base_path = String::new();
// Try to get the current folder from the shell view
// We need to query for IFolderView interface to get folder information
if let Ok(folder_view) = shell_view.cast::<windows::Win32::UI::Shell::IFolderView>() {
if let Ok(folder) = folder_view.GetFolder::<IShellItem>() {
// Try to get the file system path first
if let Ok(display_name) = folder.GetDisplayName(SIGDN_FILESYSPATH) {
if let Ok(path_str) = display_name.to_string() {
base_path = path_str;
}
}
// Fallback to desktop absolute parsing name
else if let Ok(display_name) = folder.GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING) {
if let Ok(path_str) = display_name.to_string() {
base_path = path_str;
}
}
}
}
base_path
}
fn main() -> Result<()> {
// Initialize logging from the configuration file
// log4rs::init_file("d:\\myproject\\win-dir-forwarder\\log4rs.yml", Default::default()).unwrap();
// Create a custom JSON encoder
let json_encoder = Box::new(PatternEncoder::new("{d} [{l}] - {m}{n}"));
// Create a file appender with the custom encoder
let file_appender = FileAppender::builder()
.encoder(json_encoder)
.build("d:\\myproject\\win-dir-forwarder\\logs\\log.json")
.unwrap();
// Create a log configuration with the file appender
let config = Config::builder()
.appender(Appender::builder().build("file", Box::new(file_appender)))
.build(Root::builder().appender("file").build(log::LevelFilter::Info))
.unwrap();
// Initialize the logger
log4rs::init_config(config).unwrap();
// Log some messages
info!("This is an info message.-------------->");
warn!("This is a warning message.");
error!("This is an error message.");
let result = unsafe { get_selected_file_from_explorer() };
match result {
Ok(path) => {
info!("result is {:?} <-------------------", path);
let wide_path: Vec<u16> = path.encode_utf16().chain(std::iter::once(0)).collect();
unsafe {
MessageBoxW(None, PCWSTR(wide_path.as_ptr()), w!("Selected File"), MB_ICONINFORMATION | MB_OK);
}
}
Err(e) => {
error!("Error getting selected file: {:?}", e);
}
}
Ok(())
}