Convert *mut c_void to HWND

478 views Asked by At

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.

1

There are 1 answers

2
Miiao On BEST ANSWER

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