@@ -5,58 +5,60 @@ use std::io;
55#[ cfg( feature = "set" ) ]
66use std:: os:: windows:: ffi:: OsStrExt ;
77use std:: os:: windows:: ffi:: OsStringExt ;
8+ use std:: ptr;
89
9- use windows:: core:: PWSTR ;
10- use windows:: Win32 :: System :: SystemInformation :: {
11- ComputerNamePhysicalDnsHostname , GetComputerNameExW ,
12- } ;
10+ mod bindings;
11+ use bindings:: { ComputerNamePhysicalDnsHostname , GetComputerNameExW , PWSTR } ;
1312
1413pub 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 =
20+ GetComputerNameExW ( ComputerNamePhysicalDnsHostname , ptr:: null_mut ( ) , & mut size) ;
21+ debug_assert_eq ! ( result, 0 ) ;
2222 } ;
2323
2424 let mut buffer = Vec :: with_capacity ( size as usize ) ;
2525
2626 let result = unsafe {
2727 GetComputerNameExW (
2828 ComputerNamePhysicalDnsHostname ,
29- PWSTR :: from_raw ( buffer. as_mut_ptr ( ) ) ,
29+ PWSTR :: from ( buffer. as_mut_ptr ( ) ) ,
3030 & mut size,
3131 )
3232 } ;
3333
3434 match result {
35- Ok ( _) => {
35+ 0 => Err ( io:: Error :: last_os_error ( ) ) ,
36+ _ => {
3637 unsafe {
3738 buffer. set_len ( size as usize ) ;
3839 }
3940
4041 Ok ( OsString :: from_wide ( & buffer) )
4142 }
42- Err ( e) => Err ( io:: Error :: from_raw_os_error ( e. code ( ) . 0 ) ) ,
4343 }
4444}
4545
4646#[ cfg( feature = "set" ) ]
4747pub fn set ( hostname : & OsStr ) -> io:: Result < ( ) > {
48- use windows:: core:: PCWSTR ;
49- use windows:: Win32 :: System :: SystemInformation :: SetComputerNameExW ;
48+ use bindings:: { SetComputerNameExW , PCWSTR } ;
5049
5150 let mut buffer = hostname. encode_wide ( ) . collect :: < Vec < _ > > ( ) ;
5251 buffer. push ( 0x00 ) ; // Appending the null terminator
5352
5453 let result = unsafe {
5554 SetComputerNameExW (
5655 ComputerNamePhysicalDnsHostname ,
57- PCWSTR :: from_raw ( buffer. as_ptr ( ) ) ,
56+ PCWSTR :: from ( buffer. as_ptr ( ) ) ,
5857 )
5958 } ;
6059
61- result. map_err ( |e| io:: Error :: from_raw_os_error ( e. code ( ) . 0 ) )
60+ match result {
61+ 0 => Err ( io:: Error :: last_os_error ( ) ) ,
62+ _ => Ok ( ( ) ) ,
63+ }
6264}
0 commit comments