diff options
| author | Andy Russell <arussell123@gmail.com> | 2023-01-15 13:43:15 -0500 |
|---|---|---|
| committer | Andy Russell <arussell123@gmail.com> | 2023-03-20 13:19:35 -0400 |
| commit | bb7c373fdf6c7c3fb8e204dcc178d870644fcc4b (patch) | |
| tree | 32094fdb49a0bb37a86af2e10f3189c9070cf513 /compiler/rustc_session/src | |
| parent | 13b7aa4d7faafbed78d195c27f6868f6bd90c2f8 (diff) | |
| download | rust-bb7c373fdf6c7c3fb8e204dcc178d870644fcc4b.tar.gz rust-bb7c373fdf6c7c3fb8e204dcc178d870644fcc4b.zip | |
migrate compiler, bootstrap, and compiletest to windows-rs
Diffstat (limited to 'compiler/rustc_session/src')
| -rw-r--r-- | compiler/rustc_session/src/filesearch.rs | 47 |
1 files changed, 25 insertions, 22 deletions
diff --git a/compiler/rustc_session/src/filesearch.rs b/compiler/rustc_session/src/filesearch.rs index f1fbf38217d..e734599cbfc 100644 --- a/compiler/rustc_session/src/filesearch.rs +++ b/compiler/rustc_session/src/filesearch.rs @@ -87,35 +87,38 @@ fn current_dll_path() -> Result<PathBuf, String> { use std::ffi::OsString; use std::io; use std::os::windows::prelude::*; - use std::ptr; - use winapi::um::libloaderapi::{ - GetModuleFileNameW, GetModuleHandleExW, GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, + use windows::{ + core::PCWSTR, + Win32::Foundation::HINSTANCE, + Win32::System::LibraryLoader::{ + GetModuleFileNameW, GetModuleHandleExW, GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, + }, }; + let mut module = HINSTANCE::default(); unsafe { - let mut module = ptr::null_mut(); - let r = GetModuleHandleExW( + GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, - current_dll_path as usize as *mut _, + PCWSTR(current_dll_path as *mut u16), &mut module, - ); - if r == 0 { - return Err(format!("GetModuleHandleExW failed: {}", io::Error::last_os_error())); - } - let mut space = Vec::with_capacity(1024); - let r = GetModuleFileNameW(module, space.as_mut_ptr(), space.capacity() as u32); - if r == 0 { - return Err(format!("GetModuleFileNameW failed: {}", io::Error::last_os_error())); - } - let r = r as usize; - if r >= space.capacity() { - return Err(format!("our buffer was too small? {}", io::Error::last_os_error())); - } - space.set_len(r); - let os = OsString::from_wide(&space); - Ok(PathBuf::from(os)) + ) } + .ok() + .map_err(|e| e.to_string())?; + + let mut filename = vec![0; 1024]; + let n = unsafe { GetModuleFileNameW(module, &mut filename) } as usize; + if n == 0 { + return Err(format!("GetModuleFileNameW failed: {}", io::Error::last_os_error())); + } + if n >= filename.capacity() { + return Err(format!("our buffer was too small? {}", io::Error::last_os_error())); + } + + filename.truncate(n); + + Ok(OsString::from_wide(&filename).into()) } pub fn sysroot_candidates() -> SmallVec<[PathBuf; 2]> { |
