diff --git a/crates/samples/windows/kernel_event/src/main.rs b/crates/samples/windows/kernel_event/src/main.rs index 0a818e0f2f..b08d9e5666 100644 --- a/crates/samples/windows/kernel_event/src/main.rs +++ b/crates/samples/windows/kernel_event/src/main.rs @@ -1,13 +1,13 @@ use windows::{ - Win32::Foundation::CloseHandle, + core::Owned, Win32::System::Threading::{CreateEventW, SetEvent, WaitForSingleObject}, }; fn main() -> windows::core::Result<()> { unsafe { - let event = CreateEventW(None, true, false, None)?; - SetEvent(event)?; - WaitForSingleObject(event, 0); - CloseHandle(event) + let event = Owned::new(CreateEventW(None, true, false, None)?); + SetEvent(*event)?; + WaitForSingleObject(*event, 0); } + Ok(()) } diff --git a/crates/samples/windows/overlapped/src/main.rs b/crates/samples/windows/overlapped/src/main.rs index 630a5a64ee..93879acccd 100644 --- a/crates/samples/windows/overlapped/src/main.rs +++ b/crates/samples/windows/overlapped/src/main.rs @@ -10,7 +10,7 @@ fn main() -> Result<()> { let mut string = filename.as_path().to_str().unwrap().to_owned(); string.push('\0'); - let file = CreateFileA( + let file = Owned::new(CreateFileA( PCSTR(string.as_ptr()), // See: https://github.com/microsoft/win32metadata/issues/1457 FILE_GENERIC_READ.0, @@ -19,7 +19,7 @@ fn main() -> Result<()> { OPEN_EXISTING, FILE_FLAG_OVERLAPPED, None, - )?; + )?); let mut overlapped = OVERLAPPED { Anonymous: OVERLAPPED_0 { @@ -35,18 +35,16 @@ fn main() -> Result<()> { let mut buffer: [u8; 12] = Default::default(); - if let Err(error) = ReadFile(file, Some(&mut buffer), None, Some(&mut overlapped)) { + if let Err(error) = ReadFile(*file, Some(&mut buffer), None, Some(&mut overlapped)) { assert_eq!(error.code(), ERROR_IO_PENDING.into()); } WaitForSingleObject(overlapped.hEvent, 2000); let mut bytes_copied = 0; - GetOverlappedResult(file, &overlapped, &mut bytes_copied, false)?; + GetOverlappedResult(*file, &overlapped, &mut bytes_copied, false)?; assert!(bytes_copied == 12); - CloseHandle(file)?; - println!("{}", String::from_utf8_lossy(&buffer)); } diff --git a/crates/samples/windows/privileges/src/main.rs b/crates/samples/windows/privileges/src/main.rs index 4ba4c753bf..b09952d9ed 100644 --- a/crates/samples/windows/privileges/src/main.rs +++ b/crates/samples/windows/privileges/src/main.rs @@ -11,7 +11,7 @@ fn main() -> Result<()> { let mut bytes_required = 0; _ = GetTokenInformation(token, TokenPrivileges, None, 0, &mut bytes_required); - let buffer = LocalAlloc(LPTR, bytes_required as usize)?; + let buffer = Owned::new(LocalAlloc(LPTR, bytes_required as usize)?); GetTokenInformation( token, @@ -37,7 +37,6 @@ fn main() -> Result<()> { println!("{}", name.display()) } - LocalFree(buffer); Ok(()) } } diff --git a/crates/tests/bcrypt/tests/win.rs b/crates/tests/bcrypt/tests/win.rs index 038f7a2fec..832cc11672 100644 --- a/crates/tests/bcrypt/tests/win.rs +++ b/crates/tests/bcrypt/tests/win.rs @@ -3,18 +3,18 @@ use windows::{core::*, Win32::Security::Cryptography::*}; #[test] fn test() -> Result<()> { unsafe { - let mut rng = Default::default(); - BCryptOpenAlgorithmProvider(&mut rng, BCRYPT_RNG_ALGORITHM, None, Default::default()) + let mut rng = Owned::default(); + BCryptOpenAlgorithmProvider(&mut *rng, BCRYPT_RNG_ALGORITHM, None, Default::default()) .ok()?; - let mut des = Default::default(); - BCryptOpenAlgorithmProvider(&mut des, BCRYPT_3DES_ALGORITHM, None, Default::default()) + let mut des = Owned::default(); + BCryptOpenAlgorithmProvider(&mut *des, BCRYPT_3DES_ALGORITHM, None, Default::default()) .ok()?; let mut object_len = [0; 4]; let mut bytes_copied = 0; BCryptGetProperty( - des, + *des, BCRYPT_OBJECT_LENGTH, Some(&mut object_len), &mut bytes_copied, @@ -24,14 +24,14 @@ fn test() -> Result<()> { let object_len = u32::from_le_bytes(object_len); let mut shared_secret = vec![0; object_len as usize]; - BCryptGenRandom(rng, &mut shared_secret, Default::default()).ok()?; + BCryptGenRandom(*rng, &mut shared_secret, Default::default()).ok()?; - let mut encrypt_key = Default::default(); - BCryptGenerateSymmetricKey(des, &mut encrypt_key, None, &shared_secret, 0).ok()?; + let mut encrypt_key = Owned::default(); + BCryptGenerateSymmetricKey(*des, &mut *encrypt_key, None, &shared_secret, 0).ok()?; let mut block_len = [0; 4]; BCryptGetProperty( - des, + *des, BCRYPT_BLOCK_LENGTH, Some(&mut block_len), &mut bytes_copied, @@ -47,7 +47,7 @@ fn test() -> Result<()> { let mut encrypted_len = 0; BCryptEncrypt( - encrypt_key, + *encrypt_key, Some(&send_buffer), None, None, @@ -59,7 +59,7 @@ fn test() -> Result<()> { let mut encrypted = vec![0; encrypted_len as usize]; BCryptEncrypt( - encrypt_key, + *encrypt_key, Some(&send_buffer), None, None, @@ -69,12 +69,12 @@ fn test() -> Result<()> { ) .ok()?; - let mut decrypt_key = Default::default(); - BCryptGenerateSymmetricKey(des, &mut decrypt_key, None, &shared_secret, 0).ok()?; + let mut decrypt_key = Owned::default(); + BCryptGenerateSymmetricKey(*des, &mut *decrypt_key, None, &shared_secret, 0).ok()?; let mut decrypted_len = 0; BCryptDecrypt( - decrypt_key, + *decrypt_key, Some(&encrypted), None, None, @@ -86,7 +86,7 @@ fn test() -> Result<()> { let mut decrypted = vec![0; decrypted_len as usize]; BCryptDecrypt( - decrypt_key, + *decrypt_key, Some(&encrypted), None, None, @@ -100,6 +100,7 @@ fn test() -> Result<()> { std::str::from_utf8(trim_null_end(&decrypted)).expect("Not a valid message"); assert_eq!(send_message, receive_message); } + Ok(()) }