Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for confusing LocalFree behavior #2889

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
kennykerr marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(IDI_APPLICATION as u16, 32512);
assert_ne!(LoadIconW(0, IDI_APPLICATION), 0);
assert_eq!(GetLastError(), 0);
Expand Down