Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_platform/src/dirs/windows.rs
30636 views
1
extern crate windows_sys as windows;
2
use alloc::slice;
3
use core::ffi::c_void;
4
use std::ffi::OsString;
5
use std::os::windows::ffi::OsStringExt;
6
use std::path::PathBuf;
7
use windows::Win32::UI::Shell;
8
9
/// Returns the path to the directory used for application settings.
10
// From https://github.com/dirs-dev/dirs-sys-rs/blob/main/src/lib.rs
11
#[expect(unsafe_code, reason = "Uses unsafe Windows API functions")]
12
fn known_folder(folder_id: windows::core::GUID) -> Option<PathBuf> {
13
// SAFETY: SHGetKnownFolderPath allocates path_ptr which must be freed with CoTaskMemFree.
14
unsafe {
15
let mut path_ptr: windows::core::PWSTR = core::ptr::null_mut();
16
let result =
17
Shell::SHGetKnownFolderPath(&folder_id, 0, core::ptr::null_mut(), &mut path_ptr);
18
if result == 0 {
19
let len = windows::Win32::Globalization::lstrlenW(path_ptr) as usize;
20
let path = slice::from_raw_parts(path_ptr, len);
21
let ostr: OsString = OsStringExt::from_wide(path);
22
windows::Win32::System::Com::CoTaskMemFree(path_ptr as *const c_void);
23
Some(PathBuf::from(ostr))
24
} else {
25
windows::Win32::System::Com::CoTaskMemFree(path_ptr as *const c_void);
26
None
27
}
28
}
29
}
30
31
/// Returns the path to the directory used for application settings.
32
pub fn preferences_dir() -> Option<PathBuf> {
33
known_folder(Shell::FOLDERID_LocalAppData)
34
}
35
36