Sign in to follow this  
Followers 0
smashly

struct INPUT ?

13 posts in this topic

#1 ·  Posted (edited)

Hi,

Due to my very limited understanding of structs can someone help me to get this INPUT struct in order?

MSDN INPUT Structure

I didn't understand the 'union' part of the struct and how I use the KEYBDINPUT structure in combination with it.

This is the KEYBINPUT struct: MSDN KEYBINPUT

This is the function I'm trying to use it with: MSDN SendInput

Non working code example

Global Const $INPUT_KEYBOARD = 1

$KEYBINPUT = DllStructCreate("dword vkCode;dword scanCode;dword flags;dword time;ptr dwExtraInfo")
DllStructSetData($KEYBINPUT, 'vkCode', 0x45) ;0x45 = e

$nInput = 1
$INPUT = DllStructCreate("dword type;????? ") ; I'm not sure how to add the KEYBINPUT struct
$pInput = DllStructGetPtr($INPUT)
$cbSize = DllStructGetSize($INPUT)

DllCall('user32.dll', 'int', 'SendInput', 'int', $nInput, 'ptr',  $pInput, 'int', $cbSize)

I'm not in any great need of the SendInput function, it's more like curiosity on how to create the INPUT struct.

Edited by smashly

Share this post


Link to post
Share on other sites



Global Const $KEYEVENTF_KEYUP =2
Global Const $KEYEVENTF_UNICODE = 4
Global Const $INPUT_KEYBOARD = 1

Global Const $tagKEYBDINPUT = _
    'ushort wVk;' & _
    'ushort wScan;' & _
    'dword dwFlags;' & _
    'dword time;' & _
    'ulong_ptr dwExtraInfo'

Global Const $tagINPUT = _
    'dword type;' & _
    $tagKEYBDINPUT & _
    ';dword pad;' & _
    'dword pad'

... or an unicode send example -> #682535

Share this post


Link to post
Share on other sites

Just take the KEYBDINPUT struct's fields and put them right in place.

Share this post


Link to post
Share on other sites

Don't forget that it has to be 28 bytes so you need to pad. That is how union works, you know.

Share this post


Link to post
Share on other sites

Thank you for the heads up, I would never have got the input stuct figured stabbing in the dark like I was ..lol

Cheers

Share this post


Link to post
Share on other sites

Union overlaps the data. But also, there is a size parameter, so you might be able to get away with not padding.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

You mean cbSize, the third parameter? It's still the size of the INPUT struct and not ki's or mi's only so I think that for anything less than the size of the mi struct you need to pad.

Edit: One thing is exception. If the size resulting size of the union size is something that is gonna be round to fall on an aligned address then the padding is unnecessary. For example if ki is 14 out of the greatest member of the union which is 16, there will we auto padding. ;]

Edited by Authenticity

Share this post


Link to post
Share on other sites

You provide the size of the data you are passing. That's what it's for.

Share this post


Link to post
Share on other sites

You provide the size of the data you are passing. That's what it's for.

But if I don't pad the INPUT struct then the SendInput function fails and returns 0

Share this post


Link to post
Share on other sites

What's the point of the size parameter then? O.o

Share this post


Link to post
Share on other sites

Heh, I see you've tested, or not. I guess that inside the function it does something like:

if (cbSize != sizeof(INPUT)){
    SetLastError(ERROR_INVALID_PARAMETER);
    return FALSE;
}

and also for backward compatibility and so the function won't write passing through the allocated memory and for security.

.... and I might be very wrong.

Share this post


Link to post
Share on other sites

Heh, I see you've tested, or not. ......

Yep tested it ..lol

As I said I'd never worked out the struct going via msdn's explanation and my very limited understanding.

I would of thought more along the lines of what Richard said about the cbSize until I actually tried and twiddled your code.

Thanks you once again.

Share this post


Link to post
Share on other sites

You can try and modify $tagINPUT to:

Global Const $tagKEYBDINPUT = _
    'ushort wVk;' & _
    'ushort wScan;' & _
    'dword dwFlags;' & _
    'dword time;' & _
    'ulong_ptr dwExtraInfo'

Global Const $tagINPUT = _
    'dword type;' & _
    $tagKEYBDINPUToÝ÷ ÙKZ)À

I believe you'll experience a hard crash or maybe the function is smart enough to not even try it and see if the size of the passed structure is 28. I believe that because it's impossible to know the size of a memory block based on a pointer which in most cases is 4 bytes long is the reason for the cbSize, but this is just assumption.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0