Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dirs-sys: migrate winapi to windows-sys #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion dirs-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ libc = "0.2"
redox_users = { version = "0.4.0", default-features = false }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["knownfolders", "objbase", "shlobj", "winbase", "winerror"] }
windows-sys = { version = "0.45.0", features = [
"Win32_UI_Shell",
"Win32_Foundation",
"Win32_Globalization",
"Win32_System_Com",
] }
42 changes: 22 additions & 20 deletions dirs-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,71 +122,73 @@ pub use self::target_unix_not_mac::{user_dir, user_dirs};
#[cfg(target_os = "windows")]
mod target_windows {

use std::ffi::c_void;
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use std::path::PathBuf;
use std::ptr;
use std::slice;

use winapi::shared::winerror;
use winapi::um::{combaseapi, knownfolders, shlobj, shtypes, winbase, winnt};
use windows::Win32;
use windows::Win32::UI::Shell;

pub fn known_folder(folder_id: shtypes::REFKNOWNFOLDERID) -> Option<PathBuf> {
pub fn known_folder(folder_id: windows::core::GUID) -> Option<PathBuf> {
unsafe {
let mut path_ptr: winnt::PWSTR = ptr::null_mut();
let result = shlobj::SHGetKnownFolderPath(folder_id, 0, ptr::null_mut(), &mut path_ptr);
if result == winerror::S_OK {
let len = winbase::lstrlenW(path_ptr) as usize;
let mut path_ptr: windows::core::PWSTR = std::ptr::null_mut();
let result =
Shell::SHGetKnownFolderPath(&folder_id, 0, Win32::Foundation::HANDLE::default(), &mut path_ptr);
if result == 0 {
let len = windows::Win32::Globalization::lstrlenW(path_ptr) as usize;
let path = slice::from_raw_parts(path_ptr, len);
let ostr: OsString = OsStringExt::from_wide(path);
combaseapi::CoTaskMemFree(path_ptr as *mut winapi::ctypes::c_void);
windows::Win32::System::Com::CoTaskMemFree(path_ptr as *const c_void);
Some(PathBuf::from(ostr))
} else {
windows::Win32::System::Com::CoTaskMemFree(path_ptr as *const c_void);
None
}
}
}

pub fn known_folder_profile() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Profile)
known_folder(Shell::FOLDERID_Profile)
}

pub fn known_folder_roaming_app_data() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_RoamingAppData)
known_folder(Shell::FOLDERID_RoamingAppData)
}

pub fn known_folder_local_app_data() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_LocalAppData)
known_folder(Shell::FOLDERID_LocalAppData)
}

pub fn known_folder_music() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Music)
known_folder(Shell::FOLDERID_Music)
}

pub fn known_folder_desktop() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Desktop)
known_folder(Shell::FOLDERID_Desktop)
}

pub fn known_folder_documents() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Documents)
known_folder(Shell::FOLDERID_Documents)
}

pub fn known_folder_downloads() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Downloads)
known_folder(Shell::FOLDERID_Downloads)
}

pub fn known_folder_pictures() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Pictures)
known_folder(Shell::FOLDERID_Pictures)
}

pub fn known_folder_public() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Public)
known_folder(Shell::FOLDERID_Public)
}
pub fn known_folder_templates() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Templates)
known_folder(Shell::FOLDERID_Templates)
}
pub fn known_folder_videos() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Videos)
known_folder(Shell::FOLDERID_Videos)
}
}

Expand Down