win-desktop-utils 0.5.2

Windows desktop helpers for shell, shortcuts, app data, elevation, and single-instance Rust apps
Documentation
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Shortcut helpers for Windows `.lnk` and `.url` files.

use std::ffi::OsString;
use std::path::{Path, PathBuf};

use windows::core::{Interface, PCWSTR};
use windows::Win32::System::Com::{CoCreateInstance, IPersistFile, CLSCTX_INPROC_SERVER};
use windows::Win32::UI::Shell::{IShellLinkW, ShellLink};

use crate::error::{Error, Result};
use crate::win::{
    join_quoted_args, os_str_contains_nul, path_contains_nul, run_in_sta, to_wide_os, to_wide_str,
    ComApartment,
};

/// Icon configuration for a Windows shortcut.
///
/// # Examples
///
/// ```
/// let icon = win_desktop_utils::ShortcutIcon::new(r"C:\Windows\notepad.exe", 0);
///
/// assert_eq!(icon.index, 0);
/// assert!(icon.path.ends_with("notepad.exe"));
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ShortcutIcon {
    /// Path to an icon resource, executable, or DLL.
    pub path: PathBuf,
    /// Zero-based icon index inside the resource.
    pub index: i32,
}

impl ShortcutIcon {
    /// Creates icon configuration for a shortcut.
    pub fn new(path: impl Into<PathBuf>, index: i32) -> Self {
        Self {
            path: path.into(),
            index,
        }
    }
}

/// Options used when creating a Windows `.lnk` shortcut.
///
/// # Examples
///
/// ```
/// let options = win_desktop_utils::ShortcutOptions::new()
///     .argument("--safe-mode")
///     .working_directory(r"C:\Windows")
///     .icon(r"C:\Windows\notepad.exe", 0)
///     .description("Open a tool");
///
/// assert_eq!(options.arguments.len(), 1);
/// assert!(options.working_directory.is_some());
/// assert!(options.icon.is_some());
/// assert_eq!(options.description.as_deref(), Some("Open a tool"));
/// ```
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ShortcutOptions {
    /// Command-line arguments stored in the shortcut.
    pub arguments: Vec<OsString>,
    /// Optional working directory for the shortcut target.
    pub working_directory: Option<PathBuf>,
    /// Optional icon resource for the shortcut.
    pub icon: Option<ShortcutIcon>,
    /// Optional user-facing shortcut description.
    pub description: Option<String>,
}

impl ShortcutOptions {
    /// Creates empty shortcut options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Replaces the shortcut argument list.
    pub fn arguments<I, S>(mut self, arguments: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<OsString>,
    {
        self.arguments = arguments.into_iter().map(Into::into).collect();
        self
    }

    /// Appends one shortcut argument.
    pub fn argument(mut self, argument: impl Into<OsString>) -> Self {
        self.arguments.push(argument.into());
        self
    }

    /// Sets the shortcut working directory.
    pub fn working_directory(mut self, path: impl Into<PathBuf>) -> Self {
        self.working_directory = Some(path.into());
        self
    }

    /// Sets the shortcut icon resource.
    pub fn icon(mut self, path: impl Into<PathBuf>, index: i32) -> Self {
        self.icon = Some(ShortcutIcon::new(path, index));
        self
    }

    /// Sets the shortcut description.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }
}

#[derive(Clone, Debug)]
struct ShortcutRequest {
    shortcut_path: PathBuf,
    target_path: PathBuf,
    options: ShortcutOptions,
}

fn join_args_for_shortcut(args: &[OsString]) -> String {
    join_quoted_args(args)
}

fn has_extension(path: &Path, expected: &str) -> bool {
    path.extension()
        .map(|extension| extension.to_string_lossy().eq_ignore_ascii_case(expected))
        .unwrap_or(false)
}

fn validate_output_path(path: &Path, extension: &str, label: &'static str) -> Result<()> {
    if path.as_os_str().is_empty() {
        return Err(Error::InvalidInput(label));
    }

    if path_contains_nul(path) {
        return Err(Error::InvalidInput(
            "shortcut path cannot contain NUL bytes",
        ));
    }

    if !path.is_absolute() {
        return Err(Error::PathNotAbsolute);
    }

    if !has_extension(path, extension) {
        return Err(Error::InvalidInput(match extension {
            "lnk" => "shortcut path must use .lnk extension",
            "url" => "shortcut path must use .url extension",
            _ => "shortcut path has an unsupported extension",
        }));
    }

    let parent = path
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
        .ok_or(Error::InvalidInput(
            "shortcut path must have a parent directory",
        ))?;

    if !parent.exists() {
        return Err(Error::PathDoesNotExist);
    }

    Ok(())
}

fn validate_existing_absolute_path(
    path: &Path,
    empty_message: &'static str,
    nul_message: &'static str,
) -> Result<()> {
    if path.as_os_str().is_empty() {
        return Err(Error::InvalidInput(empty_message));
    }

    if path_contains_nul(path) {
        return Err(Error::InvalidInput(nul_message));
    }

    if !path.is_absolute() {
        return Err(Error::PathNotAbsolute);
    }

    if !path.exists() {
        return Err(Error::PathDoesNotExist);
    }

    Ok(())
}

fn validate_options(options: &ShortcutOptions) -> Result<()> {
    if options
        .arguments
        .iter()
        .any(|arg| os_str_contains_nul(arg.as_os_str()))
    {
        return Err(Error::InvalidInput(
            "shortcut arguments cannot contain NUL bytes",
        ));
    }

    if let Some(description) = &options.description {
        if description.contains('\0') {
            return Err(Error::InvalidInput(
                "shortcut description cannot contain NUL bytes",
            ));
        }
    }

    if let Some(working_directory) = &options.working_directory {
        validate_existing_absolute_path(
            working_directory,
            "working_directory cannot be empty",
            "working_directory cannot contain NUL bytes",
        )?;

        if !working_directory.is_dir() {
            return Err(Error::InvalidInput("working_directory must be a directory"));
        }
    }

    if let Some(icon) = &options.icon {
        validate_existing_absolute_path(
            &icon.path,
            "icon path cannot be empty",
            "icon path cannot contain NUL bytes",
        )?;
    }

    Ok(())
}

fn validate_url(url: &str) -> Result<&str> {
    let trimmed = url.trim();

    if trimmed.is_empty() {
        return Err(Error::InvalidInput("url cannot be empty"));
    }

    if trimmed.contains('\0') {
        return Err(Error::InvalidInput("url cannot contain NUL bytes"));
    }

    if trimmed.contains('\r') || trimmed.contains('\n') {
        return Err(Error::InvalidInput("url cannot contain line breaks"));
    }

    Ok(trimmed)
}

fn create_shortcut_in_sta(request: ShortcutRequest) -> Result<()> {
    let _com = ComApartment::initialize_sta("CoInitializeEx")?;
    let link: IShellLinkW = unsafe { CoCreateInstance(&ShellLink, None, CLSCTX_INPROC_SERVER) }
        .map_err(|err| Error::WindowsApi {
            context: "CoCreateInstance(ShellLink)",
            code: err.code().0,
        })?;

    let target_w = to_wide_os(request.target_path.as_os_str());
    unsafe { link.SetPath(PCWSTR(target_w.as_ptr())) }.map_err(|err| Error::WindowsApi {
        context: "IShellLinkW::SetPath",
        code: err.code().0,
    })?;

    if !request.options.arguments.is_empty() {
        let arguments = join_args_for_shortcut(&request.options.arguments);
        let arguments_w = to_wide_str(&arguments);
        unsafe { link.SetArguments(PCWSTR(arguments_w.as_ptr())) }.map_err(|err| {
            Error::WindowsApi {
                context: "IShellLinkW::SetArguments",
                code: err.code().0,
            }
        })?;
    }

    if let Some(working_directory) = &request.options.working_directory {
        let working_directory_w = to_wide_os(working_directory.as_os_str());
        unsafe { link.SetWorkingDirectory(PCWSTR(working_directory_w.as_ptr())) }.map_err(
            |err| Error::WindowsApi {
                context: "IShellLinkW::SetWorkingDirectory",
                code: err.code().0,
            },
        )?;
    }

    if let Some(description) = &request.options.description {
        let description_w = to_wide_str(description);
        unsafe { link.SetDescription(PCWSTR(description_w.as_ptr())) }.map_err(|err| {
            Error::WindowsApi {
                context: "IShellLinkW::SetDescription",
                code: err.code().0,
            }
        })?;
    }

    if let Some(icon) = &request.options.icon {
        let icon_w = to_wide_os(icon.path.as_os_str());
        unsafe { link.SetIconLocation(PCWSTR(icon_w.as_ptr()), icon.index) }.map_err(|err| {
            Error::WindowsApi {
                context: "IShellLinkW::SetIconLocation",
                code: err.code().0,
            }
        })?;
    }

    let persist: IPersistFile = link.cast().map_err(|err| Error::WindowsApi {
        context: "IShellLinkW::cast(IPersistFile)",
        code: err.code().0,
    })?;
    let shortcut_w = to_wide_os(request.shortcut_path.as_os_str());

    unsafe { persist.Save(PCWSTR(shortcut_w.as_ptr()), true) }.map_err(|err| Error::WindowsApi {
        context: "IPersistFile::Save",
        code: err.code().0,
    })
}

fn run_in_shortcut_sta<T, F>(work: F) -> Result<T>
where
    T: Send + 'static,
    F: FnOnce() -> Result<T> + Send + 'static,
{
    run_in_sta("shortcut STA worker thread panicked", work)
}

/// Creates or overwrites a Windows `.lnk` shortcut.
///
/// `shortcut_path` must be an absolute `.lnk` path whose parent directory exists.
/// `target_path` must be an existing absolute path. Use [`ShortcutOptions`] to set
/// arguments, a working directory, an icon, or a description.
///
/// # Errors
///
/// Returns [`Error::InvalidInput`] for empty paths, NUL bytes, unsupported extensions,
/// invalid options, or malformed text fields.
/// Returns [`Error::PathNotAbsolute`] if a required path is relative.
/// Returns [`Error::PathDoesNotExist`] if the target path or output parent directory
/// does not exist.
/// Returns [`Error::WindowsApi`] if COM or Shell Link APIs report failure.
///
/// # Examples
///
/// ```no_run
/// let shortcut = std::env::current_dir()?.join("notepad.lnk");
/// let options = win_desktop_utils::ShortcutOptions::new()
///     .description("Open Notepad");
///
/// win_desktop_utils::create_shortcut(
///     &shortcut,
///     r"C:\Windows\notepad.exe",
///     &options,
/// )?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn create_shortcut(
    shortcut_path: impl AsRef<Path>,
    target_path: impl AsRef<Path>,
    options: &ShortcutOptions,
) -> Result<()> {
    let shortcut_path = shortcut_path.as_ref();
    let target_path = target_path.as_ref();

    validate_output_path(shortcut_path, "lnk", "shortcut path cannot be empty")?;
    validate_existing_absolute_path(
        target_path,
        "target path cannot be empty",
        "target path cannot contain NUL bytes",
    )?;
    validate_options(options)?;

    let request = ShortcutRequest {
        shortcut_path: shortcut_path.to_path_buf(),
        target_path: target_path.to_path_buf(),
        options: options.clone(),
    };

    run_in_shortcut_sta(move || create_shortcut_in_sta(request))
}

/// Creates or overwrites an Internet Shortcut `.url` file.
///
/// `shortcut_path` must be an absolute `.url` path whose parent directory exists.
/// Surrounding whitespace is trimmed from `url`.
///
/// # Errors
///
/// Returns [`Error::InvalidInput`] for empty paths, malformed URLs, NUL bytes,
/// line breaks in the URL, or unsupported extensions.
/// Returns [`Error::PathNotAbsolute`] if `shortcut_path` is relative.
/// Returns [`Error::PathDoesNotExist`] if the output parent directory does not exist.
/// Returns [`Error::Io`] if writing the shortcut file fails.
///
/// # Examples
///
/// ```
/// let shortcut = std::env::temp_dir().join(format!(
///     "win-desktop-utils-docs-{}.url",
///     std::process::id()
/// ));
///
/// win_desktop_utils::create_url_shortcut(
///     &shortcut,
///     "https://docs.rs/win-desktop-utils",
/// )?;
/// std::fs::remove_file(shortcut)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn create_url_shortcut(shortcut_path: impl AsRef<Path>, url: &str) -> Result<()> {
    let shortcut_path = shortcut_path.as_ref();
    let url = validate_url(url)?;

    validate_output_path(shortcut_path, "url", "shortcut path cannot be empty")?;

    let body = format!("[InternetShortcut]\r\nURL={url}\r\n");
    std::fs::write(shortcut_path, body)?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{
        create_url_shortcut, join_args_for_shortcut, validate_output_path, validate_url,
        ShortcutOptions,
    };
    use std::ffi::OsString;

    #[test]
    fn shortcut_options_builder_sets_values() {
        let options = ShortcutOptions::new()
            .argument("--help")
            .working_directory(r"C:\Windows")
            .icon(r"C:\Windows\notepad.exe", 0)
            .description("Demo shortcut");

        assert_eq!(options.arguments, [OsString::from("--help")]);
        assert_eq!(options.description.as_deref(), Some("Demo shortcut"));
        assert!(options.working_directory.is_some());
        assert!(options.icon.is_some());
    }

    #[test]
    fn join_args_quotes_each_argument() {
        let args = [OsString::from("alpha"), OsString::from("two words")];
        assert_eq!(join_args_for_shortcut(&args), "\"alpha\" \"two words\"");
    }

    #[test]
    fn validate_url_trims_surrounding_whitespace() {
        assert_eq!(
            validate_url("  https://example.com/docs  ").unwrap(),
            "https://example.com/docs"
        );
    }

    #[test]
    fn validate_url_rejects_line_breaks() {
        let result = validate_url("https://example.com/\r\nIconFile=bad.ico");
        assert!(matches!(
            result,
            Err(crate::Error::InvalidInput("url cannot contain line breaks"))
        ));
    }

    #[test]
    fn validate_output_path_rejects_relative_paths() {
        let result = validate_output_path(
            std::path::Path::new("demo.lnk"),
            "lnk",
            "shortcut path cannot be empty",
        );
        assert!(matches!(result, Err(crate::Error::PathNotAbsolute)));
    }

    #[test]
    fn validate_output_path_rejects_wrong_extension() {
        let path = std::env::temp_dir().join("demo.txt");
        let result = validate_output_path(&path, "lnk", "shortcut path cannot be empty");
        assert!(matches!(
            result,
            Err(crate::Error::InvalidInput(
                "shortcut path must use .lnk extension"
            ))
        ));
    }

    #[test]
    fn create_url_shortcut_writes_url_file() {
        let path = std::env::temp_dir().join(format!(
            "win-desktop-utils-url-shortcut-test-{}.url",
            std::process::id()
        ));
        let _ = std::fs::remove_file(&path);

        create_url_shortcut(&path, " https://example.com/docs ").unwrap();

        let body = std::fs::read_to_string(&path).unwrap();
        assert_eq!(
            body,
            "[InternetShortcut]\r\nURL=https://example.com/docs\r\n"
        );

        std::fs::remove_file(path).unwrap();
    }
}