Skip to content

Commit

Permalink
Workaround for confusing LocalFree behavior (#2889)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Feb 27, 2024
1 parent 3807aba commit 77dc028
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions crates/libs/bindgen/src/rust/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ fn handle_last_error(def: metadata::MethodDef, signature: &metadata::Signature)
if map.flags().contains(metadata::PInvokeAttributes::SupportsLastError) {
if let metadata::Type::TypeDef(return_type, _) = &signature.return_type {
if metadata::type_def_is_handle(*return_type) {
// https://github.com/microsoft/windows-rs/issues/2392#issuecomment-1477765781
if def.name() == "LocalFree" {
return false;
}
if return_type.underlying_type().is_pointer() {
return true;
}
Expand Down
5 changes: 2 additions & 3 deletions crates/libs/windows/src/Windows/Win32/Foundation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ where
(!result__.is_invalid()).then(|| result__).ok_or_else(::windows_core::Error::from_win32)
}
#[inline]
pub unsafe fn LocalFree<P0>(hmem: P0) -> ::windows_core::Result<HLOCAL>
pub unsafe fn LocalFree<P0>(hmem: P0) -> HLOCAL
where
P0: ::windows_core::IntoParam<HLOCAL>,
{
::windows_targets::link!("kernel32.dll" "system" fn LocalFree(hmem : HLOCAL) -> HLOCAL);
let result__ = LocalFree(hmem.into_param().abi());
(!result__.is_invalid()).then(|| result__).ok_or_else(::windows_core::Error::from_win32)
LocalFree(hmem.into_param().abi())
}
#[inline]
pub unsafe fn RtlNtStatusToDosError<P0>(status: P0) -> u32
Expand Down
2 changes: 1 addition & 1 deletion crates/samples/windows/privileges/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn main() -> Result<()> {
println!("{}", name.display())
}

_ = LocalFree(buffer);
LocalFree(buffer);
Ok(())
}
}
1 change: 1 addition & 0 deletions crates/tests/resources/tests/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use windows_sys::{
#[test]
fn sys() {
unsafe {
SetLastError(0);
assert_eq!(IDI_APPLICATION as u16, 32512);
assert_ne!(LoadIconW(0, IDI_APPLICATION), 0);
assert_eq!(GetLastError(), 0);
Expand Down

0 comments on commit 77dc028

Please sign in to comment.