Home > Enterprise >  Error trying to pass a LPARAM when going through windows
Error trying to pass a LPARAM when going through windows

Time:11-26

I'm trying to go through all windows (using Windows API) and get a list of the windows, but when I try to pass through a vector as a LPARAM then I get an error:

  1. non-primitive cast: *mut Vec<isize> as LPARAM an as expression can only be used to convert between primitive types or to coerce to a specific trait object

Code:

unsafe {
    let windows: Vec<isize> = vec![];
    let _ = WindowsAndMessaging::EnumWindows(Some(enum_window), &mut windows as *mut Vec<isize> as LPARAM).ok();
};

CodePudding user response:

The error diagnostic contains two vital pieces of information:

  1. The expression that failed to compile
    *mut Vec<isize> as LPARAM
    
  2. The language rule that's being violated
    an as expression can only be used to convert between primitive types
    

Either one is spot-on. With any *mut _ a primitive type, it necessarily means that LPARAM isn't. And that is correct, too. Like most types that appear in the transformed API surface exposed by the windows crate, LPARAM follows the newtype idiom.

This prevents common errors where different types share the same underlying type (e.g. HWND and HDC), but also requires that clients construct values of the wrapper type. Casting won't work, as it would with the winapi or windows-sys crates (that use type aliases for primitive types instead).

The second argument to EnumWindows needs to be

LPARAM(&mut windows as *mut Vec<isize> as isize)
  • Related