Skip to content

Commit d76cedb

Browse files
author
deeleeramone
committed
do it this way
1 parent fcc9b88 commit d76cedb

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

desktop/src-tauri/src/utils/autostart/windows_autostart.rs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,27 @@ pub fn enable_autostart(app_handle: &AppHandle) -> Result<(), String> {
6767

6868
if SUCCEEDED(hr) && !shell_link.is_null() {
6969
// Set the path to the executable
70-
let wide_path: Vec<u16> = executable_path
71-
.to_str()
72-
.ok_or("Failed to convert path to string")?
73-
.encode_utf16()
74-
.chain(std::iter::once(0))
75-
.collect();
76-
(*shell_link).SetPath(wide_path.as_ptr());
77-
(*shell_link).SetShowCmd(SW_SHOW);
70+
let wide_path: Vec<u16> = match executable_path.to_str() {
71+
Some(path) => path.encode_utf16().chain(std::iter::once(0)).collect(),
72+
None => {
73+
(*shell_link).Release();
74+
CoUninitialize();
75+
return Err("Failed to convert path to string".to_string());
76+
}
77+
};
78+
let hr_set_path = (*shell_link).SetPath(wide_path.as_ptr());
79+
if !SUCCEEDED(hr_set_path) {
80+
(*shell_link).Release();
81+
CoUninitialize();
82+
return Err(format!("Failed to set shortcut path: {hr_set_path:#x}"));
83+
}
84+
85+
let hr_set_show = (*shell_link).SetShowCmd(SW_SHOW);
86+
if !SUCCEEDED(hr_set_show) {
87+
(*shell_link).Release();
88+
CoUninitialize();
89+
return Err(format!("Failed to set show command: {hr_set_show:#x}"));
90+
}
7891

7992
// Get the IPersistFile interface
8093
let mut persist_file: *mut IPersistFile = ptr::null_mut();
@@ -93,15 +106,24 @@ pub fn enable_autostart(app_handle: &AppHandle) -> Result<(), String> {
93106

94107
if !persist_file.is_null() {
95108
// Convert shortcut path to wide string
96-
let wide_shortcut_path: Vec<u16> = shortcut_path
97-
.to_str()
98-
.ok_or("Failed to convert shortcut path to string")?
99-
.encode_utf16()
100-
.chain(std::iter::once(0))
101-
.collect();
109+
let wide_shortcut_path: Vec<u16> = match shortcut_path.to_str() {
110+
Some(path) => path.encode_utf16().chain(std::iter::once(0)).collect(),
111+
None => {
112+
(*persist_file).Release();
113+
(*shell_link).Release();
114+
CoUninitialize();
115+
return Err("Failed to convert shortcut path to string".to_string());
116+
}
117+
};
102118

103119
// Save the shortcut
104-
(*persist_file).Save(wide_shortcut_path.as_ptr(), 1);
120+
let hr_save = (*persist_file).Save(wide_shortcut_path.as_ptr(), 1);
121+
if !SUCCEEDED(hr_save) {
122+
(*persist_file).Release();
123+
(*shell_link).Release();
124+
CoUninitialize();
125+
return Err(format!("Failed to save shortcut: {hr_save:#x}"));
126+
}
105127
(*persist_file).Release();
106128
}
107129

0 commit comments

Comments
 (0)