0

I'm trying to convert a *mut c_void to HWND (https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Foundation/struct.HWND.html) but it keeps throwing this error:

mismatched types
  expected struct `HWND`
found raw pointer `*mut c_void`

How can I safely convert a *mut c_void to HWND (since HWND is built out of a c_void).

let hwnd = match parent.handle {
  RawWindowHandle::Win32(_handle) => _handle.hwnd,
  _ => panic!()
};


let mut test: windows::Win32::Foundation::HWND = hwnd;

I want an HWND from hwnd, but it throws this error:

mismatched types expected struct HWND found raw pointer *mut c_void

Thank you.

  • and where does come from this handle ? windows crate is the official windows bind of microsoft. So the crate you use to get this handle should use windows type. – Stargateur Jan 02 '23 at 14:52

1 Answers1

0

You can cast your pointer to isize and then construct HWND:

fn ptr_to_hwnd(ptr: *mut c_void) -> HWND {
    HWND(ptr as _) // struct HWND(pub isize)
}

I'd just transmute though

Miiao
  • 751
  • 1
  • 8