11use std:: io;
2- use std:: ptr;
32#[ cfg( feature = "set" ) ]
43use std:: ffi:: OsStr ;
54use std:: ffi:: OsString ;
65#[ cfg( feature = "set" ) ]
76use std:: os:: windows:: ffi:: OsStrExt ;
87use std:: os:: windows:: ffi:: OsStringExt ;
98
10- use winapi:: um:: sysinfoapi;
9+
10+
11+ use windows:: Win32 :: System :: SystemInformation :: ComputerNamePhysicalDnsHostname ;
12+
13+
1114
1215pub fn get ( ) -> io:: Result < OsString > {
16+ use windows:: core:: PWSTR ;
17+ use windows:: Win32 :: System :: SystemInformation :: GetComputerNameExW ;
18+
1319 let mut size = 0 ;
1420 unsafe {
1521 // Don't care much about the result here,
1622 // it is guaranteed to return an error,
1723 // since we passed the NULL pointer as a buffer
18- let result = sysinfoapi :: GetComputerNameExW (
19- sysinfoapi :: ComputerNamePhysicalDnsHostname ,
20- ptr :: null_mut ( ) ,
24+ let result = GetComputerNameExW (
25+ ComputerNamePhysicalDnsHostname ,
26+ PWSTR :: null ( ) ,
2127 & mut size,
2228 ) ;
23- debug_assert_eq ! ( result, 0 ) ;
29+ debug_assert_eq ! ( result. 0 , 0 ) ;
2430 } ;
2531
2632 let mut buffer = Vec :: with_capacity ( size as usize ) ;
33+
2734 let result = unsafe {
28- sysinfoapi :: GetComputerNameExW (
29- sysinfoapi :: ComputerNamePhysicalDnsHostname ,
30- buffer. as_mut_ptr ( ) ,
35+ GetComputerNameExW (
36+ ComputerNamePhysicalDnsHostname ,
37+ PWSTR :: from_raw ( buffer. as_mut_ptr ( ) ) ,
3138 & mut size,
3239 )
3340 } ;
3441
35- if result == 0 {
42+ if ! result. as_bool ( ) {
3643 Err ( io:: Error :: last_os_error ( ) )
3744 } else {
3845 unsafe {
@@ -45,15 +52,19 @@ pub fn get() -> io::Result<OsString> {
4552
4653#[ cfg( feature = "set" ) ]
4754pub fn set ( hostname : & OsStr ) -> io:: Result < ( ) > {
55+ use windows:: core:: PCWSTR ;
56+ use windows:: Win32 :: System :: SystemInformation :: SetComputerNameExW ;
57+
4858 let buffer = hostname. encode_wide ( ) . collect :: < Vec < _ > > ( ) ;
59+
4960 let result = unsafe {
50- sysinfoapi :: SetComputerNameExW (
51- sysinfoapi :: ComputerNamePhysicalDnsHostname ,
52- buffer. as_ptr ( ) ,
61+ SetComputerNameExW (
62+ ComputerNamePhysicalDnsHostname ,
63+ PCWSTR :: from_raw ( buffer. as_ptr ( ) ) ,
5364 )
5465 } ;
5566
56- if result == 0 {
67+ if ! result. as_bool ( ) {
5768 Err ( io:: Error :: last_os_error ( ) )
5869 } else {
5970 Ok ( ( ) )
0 commit comments