winutil-0.1.1/Cargo.toml.orig00006440000000000000000000000556132162140760014271 0ustar0000000000000000[package] name = "winutil" version = "0.1.1" authors = ["Dave Lancaster "] description = "Simple library providing wrappers around a handful of useful winapi calls." readme = "README.md" license = "MIT" [target.'cfg(windows)'.dependencies] winapi = { version = "0.3", features = ["wow64apiset", "processthreadsapi", "winbase"] } winutil-0.1.1/Cargo.toml0000644000000015500007544 0ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "winutil" version = "0.1.1" authors = ["Dave Lancaster "] description = "Simple library providing wrappers around a handful of useful winapi calls." readme = "README.md" license = "MIT" [target."cfg(windows)".dependencies.winapi] version = "0.3" features = ["wow64apiset", "processthreadsapi", "winbase"] winutil-0.1.1/README.md00006440000000000000000000001111132162100060012632 0ustar0000000000000000# Winutil A simple library wrapping a handful of useful winapi functions. ## Usage Add this to your Cargo.toml: ```toml [dependencies] winutil = "^0.1" ``` and this to your crate root: ```rust extern crate winutil; ``` then you can use the following functions: ```rust // Detect if the current process is running under the WoW64 subsytem. is_wow64_process(); // Return an Option containing the NetBIOS name of the local computer. get_computer_name(); // Return an Option containing the user associated with the current thread. get_user_name(); ``` winutil-0.1.1/src/lib.rs00006440000000000000000000006100132162122760013274 0ustar0000000000000000extern crate winapi; use winapi::um::wow64apiset::IsWow64Process; use winapi::um::processthreadsapi::GetCurrentProcess; use winapi::um::winbase::{GetComputerNameA, GetUserNameA}; use winapi::um::winnt::CHAR; #[cfg(test)] mod test { use super::{is_wow64_process, get_computer_name, get_user_name}; #[test] fn is_wow64_process_returns_bool() { match is_wow64_process() { Ok(true) => assert!(true), Ok(false) => assert!(true), _ => assert!(false), } } #[test] fn get_computer_name_returns_option() { match get_computer_name() { Some(_) => assert!(true), None => assert!(false), }; } #[test] fn get_user_name_returns_option() { match get_user_name() { Some(_) => assert!(true), None => assert!(false), } } } #[derive(Debug)] pub enum WinUtilError<'a> { IsWow64ProcessError(&'a str), } /// Detect if the current process is running under the WoW64 subsystem. /// /// Possible results include: /// 64-bit binary on 64-bit OS -> Ok(false) /// 32-bit binary on 32-bit OS -> Ok(false) /// 32-bit binary on 64-bit OS -> Ok(true) /// pub fn is_wow64_process<'a>() -> Result> { let mut is_wow = 0; let is_wow_ptr = &mut is_wow as *mut i32; unsafe { match IsWow64Process(GetCurrentProcess(), is_wow_ptr) { 0 => Err(WinUtilError::IsWow64ProcessError("returned 0")), _ => { match *is_wow_ptr { 0 => Ok(false), 1 => Ok(true), _ => unreachable!(), } } } } } /// Return an Option containing the NetBIOS name of the local computer. /// pub fn get_computer_name() -> Option { const MAX_COMPUTERNAME_LENGTH: usize = 15; let mut buf = [0 as CHAR; MAX_COMPUTERNAME_LENGTH + 1]; let mut len = buf.len() as u32; unsafe { if GetComputerNameA(buf.as_mut_ptr(), &mut len) == 0 { return None; }; } let host: Vec = buf[0..len as usize] .iter() .map(|&e| e as u8) .collect(); match String::from_utf8(host) { Ok(h) => return Some(h), Err(_) => return None, }; } /// Return an Option containing the user associated with the current process. /// pub fn get_user_name() -> Option { const UNLEN: usize = 256; let mut buf = [0 as CHAR; UNLEN + 1]; let mut len = buf.len() as u32; unsafe { if GetUserNameA(buf.as_mut_ptr(), &mut len) == 0 { return None; }; } let user: Vec = buf[0..(len - 1) as usize] .iter() .map(|&e| e as u8) .collect(); match String::from_utf8(user) { Ok(u) => return Some(u), Err(_) => return None, }; }