Bad API Call (SendInput) from ACCESS module

G

Guest

I want to use SendInput to send keystrokes and mouseclicks from ACCESS to
another application. I am trying first to send some text to a text box that
already has the focus in the current form. However when the API call is
executed I get a "bad API call" error. As far as I can tell the declarations
and datatypes are correct, but obviously I've missed something. Can anyone
advise what could be wrong? The database is in ACCESS 2003, but set up as
ACCESS 2000 (for backward compatibility), the OS is XP SP2. The code is
currently as follows. I use a command button to run Simkeys, which is
supposed to send the letter c to the textbox which has the focus on the same
form.:

Option Compare Database
Option Explicit
'
Private Type KEYBDINPUT
wVk As Integer
wScan As Integer
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type
'
Private Type GENERALINPUT
dwType As Long
xi(0 To 23) As Byte
End Type
'
Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte)
As Long
Private Declare Function SetKeyboardState Lib "user32" (llpbKeyState As
Byte) As Long
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long,
pInputs As GENERALINPUT, ByVal cbsize As Long)
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory"
(pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Private Sub Simkeys()
Dim GInput(0 To 1) As GENERALINPUT
Dim KInput As KEYBDINPUT, nInputs As Long
Dim bKey As Byte
bKey = vbKeyC
nInputs = 2
' Code for Pressing Key
KInput.wVk = bKey 'Key value
KInput.dwFlags = 0 'Press the key
GInput(0).dwType = 1 'Keyboard input
CopyMemory GInput(0).xi(0), KInput, Len(KInput)
' Code for Releasing Key
KInput.wVk = bKey 'Key value
KInput.dwFlags = &H2 'Release the key
GInput(1).dwType = 1 'Keyboard input
CopyMemory GInput(1).xi(0), KInput, Len(KInput)
' Send Input to message stream of thread
SendInput nInputs, GInput(0), Len(GInput(0))
'
End Sub
 
G

Guest

I have since realised that the Sendinput Declaration did not have "As Long"
on the end of it (after the brackets) which it must have as it is returning a
value (the number of events sent). Unfortunately although the call works it
does return a value of zero, and the Windows system error is 87, which means
invalid parameters have been sent via SendInput.

The parameters that have been sent are:

dwType = 1 (keyboard input)
wVk = 67 (key C)
wScan = 0
dwFlags = 0 or &H2 (press or release the key)
time = 0
dwExtraInfo = 0
(I have checked the return value for dwExtraInfo using getextrainfo and it
is zero)
I have changed xi to xi(0 to 15) as it should only contain 16 bytes (?). I
have checked the size of GInput(0) and GInput(1) and they are both 20 bytes
(as expected?). In converting long > byte and integer > byte CopyMemory
swaps bytes lo to hi etc, but I presume this is correct. Thoughts anyone?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top