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