diff --git a/src/lib.rs b/src/lib.rs index c3bea77..e5b56d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,6 @@ pub enum MacAddressError { InternalError, } - #[cfg(any( target_os = "linux", target_os = "macos", diff --git a/src/windows.rs b/src/windows.rs index 99819fe..521517e 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -1,5 +1,10 @@ -use core::convert::TryInto; -use std::{convert::TryFrom, ffi::OsString, os::windows::ffi::OsStringExt, ptr, slice}; +use std::{ + convert::{TryFrom, TryInto}, + ffi::CStr, + ffi::OsString, + os::windows::ffi::OsStringExt, + ptr, slice, +}; use winapi::shared::{ntdef::ULONG, winerror::ERROR_SUCCESS, ws2def::AF_UNSPEC}; use winapi::um::{iphlpapi::GetAdaptersAddresses, iptypes::IP_ADAPTER_ADDRESSES_LH}; @@ -36,6 +41,20 @@ pub fn get_mac(name: Option<&str>) -> Result, MacAddressError> { if adapter_name == name { return Ok(Some(bytes)); + } else { + #[cfg(not(target_pointer_width = "32"))] + let adapter_name = unsafe { CStr::from_ptr((*ptr).AdapterName) }; + + #[cfg(target_pointer_width = "32")] + let adapter_name = unsafe { CStr::from_ptr(ptr.read_unaligned().AdapterName) }; + + match adapter_name.to_str() { + Ok(s) if s == name => return Ok(Some(bytes)), + Ok(_) => {} + Err(_) => { + return Err(MacAddressError::InternalError); + } + } } } else if bytes.iter().any(|&x| x != 0) { return Ok(Some(bytes));