Skip to content

Commit

Permalink
Use embedded bindings
Browse files Browse the repository at this point in the history
Closes GH-11
  • Loading branch information
Jake-Shadle authored and swsnr committed May 13, 2023
1 parent 4ef7892 commit 327e133
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- Removed the `windows` dependency in favor of using embedded bindings, see [GH-11].

[GH-11]: https://github.com/swsnr/gethostname.rs/pull/11

## [0.4.2] – 2023-04-13

### Changed
Expand Down
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,4 @@ rust-version = "1.64"
libc = "0.2.141"

[target.'cfg(windows)'.dependencies]
windows = { version = "0.48.0", features = [
"Win32_System_SystemInformation",
"Win32_Foundation",
] }
windows-targets = "0.48"
34 changes: 20 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,14 @@ fn gethostname_impl() -> OsString {
#[inline]
fn gethostname_impl() -> OsString {
use std::os::windows::ffi::OsStringExt;
use windows::core::PWSTR;
use windows::Win32::System::SystemInformation::{
ComputerNamePhysicalDnsHostname, GetComputerNameExW,
};

// The DNS host name of the local computer. If the local computer is a node
// in a cluster, lpBuffer receives the DNS host name of the local computer,
// not the name of the cluster virtual server.
pub const COMPUTER_NAME_PHYSICAL_DNS_HOSTNAME: i32 = 5;

// https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getcomputernameexw
::windows_targets::link!("kernel32.dll" "system" fn GetComputerNameExW(nametype: i32, lpbuffer: *mut u16, nsize: *mut u32) -> i32);

let mut buffer_size: u32 = 0;

Expand All @@ -101,8 +105,8 @@ fn gethostname_impl() -> OsString {
// get the required buffer size. GetComputerNameExW then fills buffer_size with the size
// of the host name string plus a trailing zero byte.
GetComputerNameExW(
ComputerNamePhysicalDnsHostname,
PWSTR::null(),
COMPUTER_NAME_PHYSICAL_DNS_HOSTNAME,
std::ptr::null_mut(),
&mut buffer_size,
)
};
Expand All @@ -113,15 +117,17 @@ fn gethostname_impl() -> OsString {

let mut buffer = vec![0_u16; buffer_size as usize];
unsafe {
GetComputerNameExW(
ComputerNamePhysicalDnsHostname,
PWSTR::from_raw(buffer.as_mut_ptr()),
if GetComputerNameExW(
COMPUTER_NAME_PHYSICAL_DNS_HOSTNAME,
buffer.as_mut_ptr(),
&mut buffer_size,
)
.expect(
"GetComputerNameExW failed to read hostname.
Please report this issue to <https://github.com/swsnr/gethostname.rs/issues>!",
)
) == 0
{
panic!(
"GetComputerNameExW failed to read hostname.
Please report this issue to <https://github.com/swsnr/gethostname.rs/issues>!"
);
}
}
assert!(
// GetComputerNameExW returns the size _without_ the trailing zero byte on the second call
Expand Down

0 comments on commit 327e133

Please sign in to comment.