Skip to content

Commit f761bad

Browse files
committed
Switch to windows-bindgen
1 parent 60309d2 commit f761bad

File tree

6 files changed

+59
-31
lines changed

6 files changed

+59
-31
lines changed

Cargo.lock

Lines changed: 4 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ cfg-if = "^1.0"
2626
libc = "^0.2"
2727

2828
[target.'cfg(target_os = "windows")'.dependencies]
29-
windows = { version = "^0.52", features = ["Win32_Foundation", "Win32_System_SystemInformation"] }
29+
windows-link = "0.1.1"
3030

3131
[dev-dependencies]
3232
version-sync = "0.9"
3333

34+
[target.'cfg(target_os = "windows")'.dev-dependencies]
35+
windows-bindgen = { version = "0.61" }
36+
similar-asserts = { version = "1.6.1" }
37+
3438
[package.metadata.docs.rs]
3539
features = ["set"]
3640
rustdoc-args = ["--cfg", "docsrs"]

src/windows/bindings.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![allow(non_snake_case, non_upper_case_globals, non_camel_case_types, dead_code, clippy::all)]
2+
3+
windows_link::link!("kernel32.dll" "system" fn GetComputerNameExW(nametype : COMPUTER_NAME_FORMAT, lpbuffer : PWSTR, nsize : *mut u32) -> BOOL) ; pub type BOOL = i32 ; pub type COMPUTER_NAME_FORMAT = i32 ; pub const ComputerNamePhysicalDnsHostname : COMPUTER_NAME_FORMAT = 5i32 ; pub type PWSTR = * mut u16 ;

src/windows.rs renamed to src/windows/mod.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,62 @@
22
use std::ffi::OsStr;
33
use std::ffi::OsString;
44
use std::io;
5+
use std::ptr;
56
#[cfg(feature = "set")]
67
use std::os::windows::ffi::OsStrExt;
78
use std::os::windows::ffi::OsStringExt;
89

9-
use windows::core::PWSTR;
10-
use windows::Win32::System::SystemInformation::{
11-
ComputerNamePhysicalDnsHostname, GetComputerNameExW,
12-
};
10+
mod bindings;
11+
use bindings::{ComputerNamePhysicalDnsHostname, PWSTR, GetComputerNameExW};
1312

1413
pub fn get() -> io::Result<OsString> {
1514
let mut size = 0;
1615
unsafe {
1716
// Don't care much about the result here,
1817
// it is guaranteed to return an error,
1918
// since we passed the NULL pointer as a buffer
20-
let result = GetComputerNameExW(ComputerNamePhysicalDnsHostname, PWSTR::null(), &mut size);
21-
debug_assert!(result.is_err());
19+
let result = GetComputerNameExW(ComputerNamePhysicalDnsHostname, ptr::null_mut(), &mut size);
20+
debug_assert_eq!(result, 0);
2221
};
2322

2423
let mut buffer = Vec::with_capacity(size as usize);
2524

2625
let result = unsafe {
2726
GetComputerNameExW(
2827
ComputerNamePhysicalDnsHostname,
29-
PWSTR::from_raw(buffer.as_mut_ptr()),
28+
PWSTR::from(buffer.as_mut_ptr()),
3029
&mut size,
3130
)
3231
};
3332

3433
match result {
35-
Ok(_) => {
34+
0 => Err(io::Error::last_os_error()),
35+
_ => {
3636
unsafe {
3737
buffer.set_len(size as usize);
3838
}
3939

4040
Ok(OsString::from_wide(&buffer))
4141
}
42-
Err(e) => Err(io::Error::from_raw_os_error(e.code().0)),
4342
}
4443
}
4544

4645
#[cfg(feature = "set")]
4746
pub fn set(hostname: &OsStr) -> io::Result<()> {
48-
use windows::core::PCWSTR;
49-
use windows::Win32::System::SystemInformation::SetComputerNameExW;
47+
use bindings::SetComputerNameExW;
5048

5149
let mut buffer = hostname.encode_wide().collect::<Vec<_>>();
5250
buffer.push(0x00); // Appending the null terminator
5351

5452
let result = unsafe {
5553
SetComputerNameExW(
5654
ComputerNamePhysicalDnsHostname,
57-
PCWSTR::from_raw(buffer.as_ptr()),
55+
PCWSTR::from(buffer.as_ptr()),
5856
)
5957
};
6058

61-
result.map_err(|e| io::Error::from_raw_os_error(e.code().0))
59+
match result {
60+
0 => Err(io::Error::last_os_error()),
61+
_ => Ok(()),
62+
}
6263
}

tests/bindings.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--out src/windows/bindings.rs
2+
--flat --sys --no-comment
3+
--filter
4+
ComputerNamePhysicalDnsHostname
5+
GetComputerNameExW
6+
SetComputerNameExW

tests/codegen.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![cfg(windows)]
2+
3+
use std::fs;
4+
use windows_bindgen::bindgen;
5+
6+
#[test]
7+
fn gen_bindings() {
8+
let output = "src/windows/bindings.rs";
9+
let existing = fs::read_to_string(output).unwrap();
10+
11+
bindgen(["--no-deps", "--etc", "tests/bindings.txt"]);
12+
13+
// Check the output is the same as before.
14+
// Depending on the git configuration the file may have been checked out with `\r\n` newlines or
15+
// with `\n`. Compare line-by-line to ignore this difference.
16+
let mut new = fs::read_to_string(output).unwrap();
17+
if existing.contains("\r\n") && !new.contains("\r\n") {
18+
new = new.replace("\n", "\r\n");
19+
} else if !existing.contains("\r\n") && new.contains("\r\n") {
20+
new = new.replace("\r\n", "\n");
21+
}
22+
23+
similar_asserts::assert_eq!(existing, new);
24+
if !new.lines().eq(existing.lines()) {
25+
panic!("generated file `{}` is changed.", output);
26+
}
27+
}

0 commit comments

Comments
 (0)