2009-07-17 9 views
0

Ich verwende SendInput, um einige Tastaturbefehle an ein Dialogfeld zu senden. Es ist bereits das aktive Vordergrundfenster, wenn ich SendInput aufruft.Warum funktioniert mein Aufruf an SendInput nicht?

Ich sende zuerst "Return" und dann noch ein paar andere Sachen. Das Dialogfeld wird mit einer standardmäßig ausgewählten Schaltfläche geöffnet. Wenn Sie die Eingabetaste manuell betätigen, wird die Schaltfläche im Dialogfeld auf den nächsten Bildschirm geklickt, aber mein SendInput-Aufruf führt das Dialogfeld nicht weiter.

Hier ist mein Code. Zuerst werden die Datenstrukturen und p/aufrufen

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) 

Dann meinen Code zu diesem Dialogfeld

' 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 

Was soll ich hier

Antwort

1

Das Problem mit Rangier die Datenstrukturen Sie könnten fehlen schreiben verwende es. Kannst du das posten? Veröffentlichen Sie auch die Deklarationen Ihrer P/Invoked-Funktionen.

Haben Sie die SendInput-Funktion unabhängig getestet? Funktioniert es? Wenn Sie dies noch nicht getan haben, würde ich vorschlagen, einen einfacheren Testfall zu codieren, um sicherzustellen, dass es keine Probleme mit Ihren Deklarationen der P/Aufgerufenen Funktionen und den von Ihnen verwendeten Datenstrukturen gibt.

+0

Ich werde einen einfacheren Fall versuchen –

+0

Ich bekomme einen Rückgabewert von 0 von SendInput was bedeutet, dass keine Zeichen akzeptiert wurden Ich denke –

+0

Das Problem war die cbSize benötigt, um mit Marshal.SizeOf nicht LEN gemessen werden. Das hat alles repariert. –

Verwandte Themen