Skip to content

SendInput takes only one INPUT #972

Closed

Description

I have just started learning Rust and was playing around.

I found out that SendInput takes only one INPUT but according to the Microsoft Windows API documentation it should take an array of INPUTs.

I think this is a bug but please let me know if I'm wrong and if so please tell me how to make it work!

This code compiles:

use bindings::{
    Windows::Win32::UI::KeyboardAndMouseInput::{INPUT, INPUT_0, INPUT_TYPE, KEYBDINPUT, KEYBD_EVENT_FLAGS, SendInput},
    Windows::Win32::UI::WindowsAndMessaging::GetMessageExtraInfo,
};
use std::mem::size_of;

fn main(){
    const CBSIZE: i32 = size_of::<INPUT>() as i32;

    for i in 1..10 {
        unsafe {
            let extra_info = GetMessageExtraInfo();
            let extra_info = extra_info.0.unsigned_abs();
            let cinputs: u32 = 1;
            let mut pinputs = INPUT {
                r#type: INPUT_TYPE {0: 1},
                Anonymous: INPUT_0 {
                    ki: KEYBDINPUT {
                        wVk: 0x41,
                        wScan: 1,
                        dwFlags: KEYBD_EVENT_FLAGS(0),
                        time: 0,
                        dwExtraInfo: extra_info,
                    }
                }
            };
            let res = SendInput(cinputs, &mut pinputs, CBSIZE);
            println!("{}. {}", i, res);
        }
    }
}

But this code dosn't:

use bindings::{
    Windows::Win32::UI::KeyboardAndMouseInput::{INPUT, INPUT_0, INPUT_TYPE, KEYBDINPUT, KEYBD_EVENT_FLAGS, SendInput},
    Windows::Win32::UI::WindowsAndMessaging::GetMessageExtraInfo,
};
use std::mem::size_of;

fn main(){
    const CBSIZE: i32 = size_of::<INPUT>() as i32;

    for i in 1..10 {
        unsafe {
            let extra_info = GetMessageExtraInfo();
            let extra_info = extra_info.0.unsigned_abs();
            let cinputs: u32 = 1;
            let mut pinputs = [
                INPUT {
                    r#type: INPUT_TYPE {0: 1},
                    Anonymous: INPUT_0 {
                        ki: KEYBDINPUT {
                            wVk: 0x41,
                            wScan: 1,
                            dwFlags: KEYBD_EVENT_FLAGS(0),
                            time: 0,
                            dwExtraInfo: extra_info,
                        }
                    }
                },
            ];
            let res = SendInput(cinputs, &mut pinputs, CBSIZE);
            println!("{}. {}", i, res);
        }
    }
}

Error:

error[E0308]: mismatched types
  --> src\main.rs:29:42
   |
29 |             let res = SendInput(cinputs, &mut pinputs, CBSIZE);
   |                                          ^^^^^^^^^^^^ expected struct `INPUT`, found array of 1 element
   |
   = note:    expected raw pointer `*mut INPUT`
           found mutable reference `&mut [INPUT; 1]`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `wintest`

To learn more, run the command again with --verbose.

You can find the working code here

Activity

  1. riverar commented on Jul 12, 2021

    @riverar
    Collaborator

    Hey CrumblyLiquid (@CrumblyLiquid) (love the name). Doesn't look like the crate handles NativeArrayInfo attributes yet, see also #790.

    public unsafe static extern uint SendInput([In] uint cInputs, [In][NativeArrayInfo(CountParamIndex = 0)] INPUT* pInputs, [In] int cbSize);

    cc: Kenny Kerr (@kennykerr)

  2. kennykerr commented on Jul 12, 2021

    @kennykerr
    Collaborator

    Yep, there are a few array-related issues currently open. There is a whole class of array parameter handling I still need to get to.

  3. kennykerr commented on Feb 23, 2022

    @kennykerr
    Collaborator

    Sorry for the delay, #1562 adds support for Win32 array parameters and fixes this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions