Search code examples
winapirustuser32

Cast a ptr::null() to a Windows handle


I'm trying to use the winapi crate to create a GUI for my VST plugin. Functions like user32::SetMenu need some handles, some of which can be NULL.

The following method is called when the plugin is loaded:

fn open(&mut self, window: *mut c_void) {
    unsafe {
        let menu = user32::CreateMenu();
        let sub_menu = user32::CreateMenu();
        let hwnd: HWND = window as HWND;
        user32::SetMenu(hwnd, menu);
        let mut data = OsString::from("heee");
        let raw = &mut data as *mut _ as LPWSTR;
        let mut menu_item = MENUITEMINFOW {
            cbSize: 0,
            fMask: 0o0000_0020 | 0o0000_0040,
            fType: 0,
            fState: 0,
            wID: MENUITEM_ID,
            hSubMenu: sub_menu,
            hbmpChecked: ptr::null() as HBITMAP,
            hbmpUnchecked: ptr::null() as HBITMAP,
            dwItemData: 0,
            dwTypeData: raw,
            cch: 0,
            hbmpItem: ptr::null() as *mut _,
        };
        menu_item.cbSize = mem::size_of_val(&menu_item) as u32;
        user32::InsertMenuItemW(menu, MENUITEM_ID, 0, &menu_item);
    }

    self.open = true;
}

However, I can't pass NULL for the handles:

hbmpChecked: ptr::null() as HBITMAP,

I get the error message

hbmpChecked: ptr::null() as HBITMAP,
             ^^^^^^^^^ cannot infer type for `_`

I can't find a solution in the docs of winapi/user32.


Solution

  • Here is how HBITMAP is defined in winapi:

    type HBITMAP = *mut HBITMAP__;
    

    This makes HBITMAP a mutable raw pointer. ptr::null() returns a const raw pointer. You should use ptr::null_mut() instead, as it returns a mutable raw pointer, so the compiler will be able to infer the correct type.