tags:

views:

638

answers:

1

I am using SendInput to send some keyboard commands to a dialog box. It is already the active foreground window when I call SendInput.

I first send "Return" and then a bunch of other stuff. The dialog box opens with a button selected by default, and when manually selecting Enter, the button is clicked on the dialog progresses to the next screen, but my SendInput call doesn't advance the dialog.

Here is my code. First the data structures and p/invoke

Imports System.Runtime.InteropServices
Imports System.Drawing

Public Class NativeMethods

Private Structure INPUT
    Dim dwType As Integer
    Dim mkhi As MOUSEKEYBDHARDWAREINPUT
End Structure

Private Structure KEYBDINPUT
    Public wVk As Short
    Public wScan As Short
    Public dwFlags As Integer
    Public time As Integer
    Public dwExtraInfo As Integer
End Structure

Private Structure HARDWAREINPUT
    Public uMsg As Integer
    Public wParamL As Short
    Public wParamH As Short
End Structure

<StructLayout(LayoutKind.Explicit)> _
Private Structure MOUSEKEYBDHARDWAREINPUT
    <FieldOffset(0)> Public mi As MOUSEINPUT
    <FieldOffset(0)> Public ki As KEYBDINPUT
    <FieldOffset(0)> Public hi As HARDWAREINPUT
End Structure

Private Structure MOUSEINPUT
    Public dx As Integer
    Public dy As Integer
    Public mouseData As Integer
    Public dwFlags As Integer
    Public time As Integer
    Public dwExtraInfo As Integer ' changed from IntPtr because of compiler error.
                                  ' I don't use this field '
End Structure

' Constants for SendInput '
Const INPUT_MOUSE As UInt32 = 0
Public Const INPUT_KEYBOARD As Integer = 1
Const INPUT_HARDWARE As Integer = 2
Public Const KEYEVENTF_EXTENDEDKEY As UInt32 = &H1
Public Const KEYEVENTF_KEYUP As UInt32 = &H2
Public Const KEYEVENTF_UNICODE As UInt32 = &H4
Public Const KEYEVENTF_SCANCODE As UInt32 = &H8
Const XBUTTON1 As UInt32 = &H1
Const XBUTTON2 As UInt32 = &H2
Const MOUSEEVENTF_MOVE As UInt32 = &H1
Const MOUSEEVENTF_LEFTDOWN As UInt32 = &H2
Const MOUSEEVENTF_LEFTUP As UInt32 = &H4
Const MOUSEEVENTF_RIGHTDOWN As UInt32 = &H8
Const MOUSEEVENTF_RIGHTUP As UInt32 = &H10
Const MOUSEEVENTF_MIDDLEDOWN As UInt32 = &H20
Const MOUSEEVENTF_MIDDLEUP As UInt32 = &H40
Const MOUSEEVENTF_XDOWN As UInt32 = &H80
Const MOUSEEVENTF_XUP As UInt32 = &H100
Const MOUSEEVENTF_WHEEL As UInt32 = &H800
Const MOUSEEVENTF_VIRTUALDESK As UInt32 = &H4000
Const MOUSEEVENTF_ABSOLUTE As UInt32 = &H8000

Public Const VK_TAB = &H9
Public Const VK_RETURN = &HD
Public Const VK_DOWN = &H28

<DllImport("user32.dll")> _
Private Shared Function SendInput(ByVal nInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow( _
 ByVal lpClassName As String, _
 ByVal lpWindowName As String) As IntPtr
End Function

<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowByClass( _
     ByVal lpClassName As String, _
     ByVal zero As IntPtr) As IntPtr
End Function

<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowByCaption( _
     ByVal zero As IntPtr, _
     ByVal lpWindowName As String) As IntPtr
End Function

<DllImport("user32.dll")> _
Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
End Function

<DllImport("user32.dll")> _
Private Shared Function SetFocus(ByVal hWnd As IntPtr) As IntPtr
End Function

' "As any" replacement seen here '
' when you see As Any in Pinvoke, replace with this ' 
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(<System.Runtime.InteropServices.MarshalAsAttribute( _
    System.Runtime.InteropServices.UnmanagedType.AsAny)> _
    ByVal pDst As Object, <System.Runtime.InteropServices.MarshalAsAttribute( _
    System.Runtime.InteropServices.UnmanagedType.AsAny)> _
    ByVal pSrc As Object, ByVal ByteLen As Long)

Then my code to write to this dialog box

' constants for keys needed '
Public Const VK_TAB = &H9
Public Const VK_RETURN = &HD
Public Const VK_DOWN = &H28

Public Shared Sub SendRebuildKeys()
    ' Enter ' 
    SendKeyDownAndUp(VK_RETURN)
    ' Down x 10 ' 
    For i = 1 To 10
        SendKeyDownAndUp(VK_DOWN)
    Next
    ' Enter '
    SendKeyDownAndUp(VK_RETURN)
    ' T 84 ' 
    SendKeyDownAndUp(CShort(84))
    ' S 83 '
    SendKeyDownAndUp(CShort(83))
    ' T 84 '
    SendKeyDownAndUp(CShort(84))
    ' Tab '
    SendKeyDownAndUp(VK_TAB)
    ' Enter '
    SendKeyDownAndUp(VK_RETURN)
End Sub

Public Shared Sub SendKeyDownAndUp(ByVal keyCode As Short)
    Dim inputd = GetINPUTFromKeyCode(keyCode, True)
    Dim inputu = GetINPUTFromKeyCode(keyCode, False)
    Dim inputs(2) As INPUT
    inputs(0) = inputd
    inputs(1) = inputu

    SendInput(2, inputs(0), Len(inputd))
End Sub

Private Shared Function GetINPUTFromKeyCode(ByVal keyCode As Short, _
    ByVal isDown As  Boolean) As INPUT
    Dim input As New INPUT()
    Dim unall As New MOUSEKEYBDHARDWAREINPUT()
    Dim keyb As New KEYBDINPUT()

    input.dwType = INPUT_KEYBOARD
    keyb.wVk = keyCode

    If isDown = False Then
        keyb.dwFlags = KEYEVENTF_KEYUP
    Else
        keyb.dwFlags = 0
    End If

    ' copy keyb into unall.ki '
    CopyMemory(unall.ki, keyb, Len(keyb))
    ' copy unall into input.mkhi ' 
    CopyMemory(input.mkhi, unall, Len(unall))

    Return input
End Function

What am I missing here

+1  A: 

The problem might be with marshalling the data structures you're using. Can you post those? Also, post the declarations of your P/Invoked functions.

Have you tested the SendInput function independently? Does it work? If you haven't done that yet, I would suggest coding a simpler test case to make sure there are no issues with your declarations of the P/Invoked functions and with the data structures you're using.

Miky Dinescu
I will try a simpler case
Tony Peterson
I get a return value of 0 from SendInput which means no chars were accepted I think
Tony Peterson
The problem was the cbSize needed to be measured using Marshal.SizeOf not LEN. That fixed things up.
Tony Peterson