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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LocalFree and GlobalFree functions return an error if the free succeeds, while returning a success if the free fails #2787

Closed
Rileran opened this issue Jan 11, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Rileran
Copy link

Rileran commented Jan 11, 2024

Summary

Functions LocalFree and GlobalFree return Err when the free is successful, and Ok when the free fails.

The expected outcome would be the opposite.

Looking at the documentation for LocalFree tells us that if the function succeeds, the return value is NULL (or invalid).

Looking at the generated source code (for crate version 0.52.0), the function returns Ok when the return value is NOT NULL (or valid).

pub unsafe fn LocalFree<P0>(hmem: P0) -> ::windows_core::Result<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)
}

Crate manifest

[package]
name = "windows-playground"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
windows = { version = "0.52.0", features = ["Win32_Foundation", "Win32_System_Memory"] }

Crate code

use windows::Win32::Foundation::LocalFree;
use windows::Win32::System::Memory::{LocalAlloc, LMEM_FIXED, LMEM_ZEROINIT};

fn main() {
    unsafe {
        let data = LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, 10).unwrap();
        // the next call fails if free is successfull
        // Error { code: HRESULT(0x00000000), message: "The operation completed successfully." }
        LocalFree(data).unwrap();
    }
}
@Rileran Rileran added the bug Something isn't working label Jan 11, 2024
@Rileran
Copy link
Author

Rileran commented Jan 11, 2024

Just saw this is already on issue on the win32metadata repository microsoft/win32metadata#1557, closing this one.

@Rileran Rileran closed this as completed Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant