News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Using SendInput

Started by Sean1337, March 09, 2009, 12:56:38 PM

Previous topic - Next topic

Sean1337

Hey everyone,

I'm trying to use SendInput to simulate a left mouse click and came across this code snippet written in VB.

Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As INPUT_TYPE, ByVal cbSize As Long) As Long

Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const INPUT_MOUSE = 0
Private Const INPUT_KEYBOARD = 1
Private Const INPUT_HARDWARE = 2

Type MOUSEINPUT
  dx As Long
  dy As Long
  mouseData As Long
  dwFlags As Long
  dwtime As Long
  dwExtraInfo As Long
End Type

Type INPUT_TYPE
  dwType As Long
  xi(0 To 23) As Byte
End Type


Public Sub ClickDaMouse()

    Dim intX As Integer
    Dim inputEvents(0 To 1) As INPUT_TYPE ' holds information about each event
    Dim mouseEvent As MOUSEINPUT ' temporarily hold mouse input info

    ' Load the information needed to synthesize pressing the left mouse button.
    mouseEvent.dx = 0 ' no horizontal movement
    mouseEvent.dy = 0 ' no vertical movement
    mouseEvent.mouseData = 0 ' not needed
    mouseEvent.dwFlags = MOUSEEVENTF_LEFTDOWN ' left button down
    mouseEvent.dwtime = 0 ' use the default
    mouseEvent.dwExtraInfo = 0 ' not needed
    ' Copy the structure into the input array's buffer.
    inputEvents(0).dwType = INPUT_MOUSE ' mouse input
    CopyMemory inputEvents(0).xi(0), mouseEvent, Len(mouseEvent)
     
    ' Do the same as above, but for releasing the left mouse button.
    mouseEvent.dx = 0 ' no horizontal movement
    mouseEvent.dy = 0 ' no vertical movement
    mouseEvent.mouseData = 0 ' not needed
    mouseEvent.dwFlags = MOUSEEVENTF_LEFTUP ' left button up
    mouseEvent.dwtime = 0 ' use the default
    mouseEvent.dwExtraInfo = 0 ' not needed
    ' Copy the structure into the input array's buffer.
    inputEvents(1).dwType = INPUT_MOUSE ' mouse input
    CopyMemory inputEvents(1).xi(0), mouseEvent, Len(mouseEvent)
     
    ' Now that all the information for the 2 input events has been placed
    ' into the array, finally send it into the input stream.
    intX = SendInput(2, inputEvents(0), Len(inputEvents(0))) ' place the events into the stream

End Sub


For the most part I think it does what I want however I am not sure whether CopyMemory is appropriate.

Could anyone convert this to ASM or provide me with direction as I'm not "fluent" in Visual Basic and I am not familiar with all of the notation. I would really appreciate it.

Thanks a lot!
-Sean

ecube

sendinput supports sending multiple requests at once via an array, I don't know how to do that in masm perhaps someone here can give an example,which i'd love to see, but here's your vb code converted just the requests are sent individually instead at once.

ClickDaMouse proto
MOUSEEVENTF_LEFTDOWN equ 2h
MOUSEEVENTF_LEFTUP   equ 4h
INPUT_MOUSE equ 0
INPUT_KEYBOARD     equ 1
INPUT_HARDWARE      equ 2

.data?
MOUSEINPUT STRUCT
  dx_m dd ? ;can't name it dx
  dy dd ?
  mouseData dd ?
  dwFlags dd ?
  dwtime dd ?
  dwExtraInfo dd ?
MOUSEINPUT ENDS

INPUT_TYPE STRUCT
  dwType dd ?
  xi byte 23 dup(?)
INPUT_TYPE ENDS

.code
ClickDaMouse proc
LOCAL intX:DWORD
LOCAL inputEvent1:INPUT_TYPE
LOCAL inputEvent2:INPUT_TYPE
LOCAL mouseEvent:MOUSEINPUT

    mov mouseEvent.dx_m,0 ; no horizontal movement
    mov mouseEvent.dy,0 ; no vertical movement
    mov mouseEvent.mouseData,0 ;not needed
    mov mouseEvent.dwFlags,MOUSEEVENTF_LEFTDOWN ; left button down
    mov mouseEvent.dwtime,0 ;use the default
    mov mouseEvent.dwExtraInfo,0 ;not needed
    ;Copy the structure into the input array's buffer.
    mov inputEvent1.dwType,INPUT_MOUSE ; mouse input
    invoke RtlMoveMemory,addr inputEvent1.xi,addr mouseEvent,sizeof mouseEvent
     
    ;Do the same as above, but for releasing the left mouse button.
     mov mouseEvent.dx_m,0 ;no horizontal movement
     mov mouseEvent.dy,0 ;no vertical movement
     mov mouseEvent.mouseData,0 ;not needed
     mov mouseEvent.dwFlags,MOUSEEVENTF_LEFTUP ;' left button up
     mov mouseEvent.dwtime,0 ;use the default
     mov mouseEvent.dwExtraInfo,0 ;not needed
    ;Copy the structure into the input array's buffer.

    mov inputEvent2.dwType,INPUT_MOUSE ; mouse input
    invoke RtlMoveMemory,addr inputEvent2.xi,addr mouseEvent,sizeof mouseEvent
         
    ;send first request
    invoke SendInput,1,addr inputEvent1,sizeof inputEvent1
   
    ;send second request
     invoke SendInput,1,addr inputEvent2,sizeof inputEvent2
ret
ClickDaMouse endp

Sean1337

Thanks a million E^cube.