Skip to content

Instantly share code, notes, and snippets.

@lededev
lededev / struct_convert_to_usize.rs
Last active December 17, 2023 15:09
Rust(unsafe) 结构体转指针通过LPARAM / usize参数传递
struct InOutData {
pid: u32, // in
hwnd: HWND // out
}
unsafe extern "system" fn callback(hwnd: HWND, lparam: LPARAM) -> BOOL {
let s: &mut InOutData = mem::transmute(lparam); // LPARAM转回结构体指针
if ((*s).pid == GetWindowThreadProcessId(hwnd, Some(ptr::null_mut()))) {
(*s).hwnd = hwnd;
return false.into();