Reputation: 16246
I have a set of send key functions that I wrote a while ago. It was working in 32 bit systems. Now I tested it again and realised it doesn't work in my new win7 64 bit system any more. I found a few articles on the net, and followed their way to make the Input32/64 functions. However it doesn't seem to work.
Dim result As Integer = SendInput(1, input, cbSize)
always return 0 and no keystroke was simulated. Could you please take a look?
(Note: I only made the KEYBOARDINPUT64 structure, it didn't work, so I didn't make MOUSEINPUT64 or HARDWAREINPUT64 structures...)
''mouse functions
Private Shared Sub SendMouseInput(ByVal flags As MOUSEEVENTF, ByVal point As System.Drawing.Point)
If Marshal.SizeOf(New IntPtr()) = 8 Then
SendMouseInput64(flags, point)
Else
SendMouseInput32(flags, point)
End If
End Sub
Private Shared Sub SendMouseInput64(ByVal flags As MOUSEEVENTF, ByVal point As System.Drawing.Point)
Dim screen As Rectangle = My.Computer.Screen.Bounds
Dim globalx As Integer = CInt((65535 * point.X) / screen.Width) ' x scale from 0 to 65535
Dim globaly As Integer = CInt((65535 * point.Y) / screen.Height) ' y scale from 0 to 65535
Dim input As New INPUT64
input.dwType = InputType.Mouse
input.mi.dx = globalx
input.mi.dy = globaly
input.mi.dwFlags = MOUSEEVENTF.ABSOLUTE Or flags
input.mi.dwExtraInfo = IntPtr.Zero
input.mi.mouseData = 0
input.mi.time = 0
SendInput(1, input, Marshal.SizeOf(input))
End Sub
Private Shared Sub SendMouseInput32(ByVal flags As MOUSEEVENTF, ByVal point As System.Drawing.Point)
Dim screen As Rectangle = My.Computer.Screen.Bounds
Dim globalx As Integer = CInt((65535 * point.X) / screen.Width) ' x scale from 0 to 65535
Dim globaly As Integer = CInt((65535 * point.Y) / screen.Height) ' y scale from 0 to 65535
Dim input As New INPUT32
input.dwType = InputType.Mouse
input.mi.dx = globalx
input.mi.dy = globaly
input.mi.dwFlags = MOUSEEVENTF.ABSOLUTE Or flags
input.mi.dwExtraInfo = IntPtr.Zero
input.mi.mouseData = 0
input.mi.time = 0
SendInput(1, input, Marshal.SizeOf(input))
End Sub
''keyboard functions
Private Shared Sub SendKeyInput(ByVal flags As KEYEVENTF, ByVal key As Int16)
If Marshal.SizeOf(New IntPtr()) = 8 Then
SendKeyInput64(flags, key)
Else
SendKeyInput32(flags, key)
End If
End Sub
Private Shared Sub SendKeyInput64(ByVal flags As KEYEVENTF, ByVal key As Int16)
Dim input As New INPUT64
Dim ki As New KEYBDINPUT64
input.dwType = InputType.Keyboard
input.ki = ki
input.ki.wVk = key
input.ki.wScan = 0
input.ki.time = 0
input.ki.dwFlags = flags
input.ki.dwExtraInfo = IntPtr.Zero
Dim cbSize As Integer = Marshal.SizeOf(GetType(INPUT64))
Dim result As Integer = SendInput(1, input, cbSize)
If result = 0 Then Console.WriteLine("SendKeyInput:" & Marshal.GetLastWin32Error)
End Sub
Private Shared Sub SendKeyInput32(ByVal flags As KEYEVENTF, ByVal key As Int16)
Dim input As New INPUT32
Dim ki As New KEYBDINPUT32
input.dwType = InputType.Keyboard
input.ki = ki
input.ki.wVk = key
input.ki.wScan = 0
input.ki.time = 0
input.ki.dwFlags = flags
input.ki.dwExtraInfo = IntPtr.Zero
Dim cbSize As Integer = Marshal.SizeOf(GetType(INPUT32))
Dim result As Integer = SendInput(1, input, cbSize)
If result = 0 Then Messaging.Instance.ReportError("MouseKeyboard::SendKeyInput:" & Marshal.GetLastWin32Error)
End Sub
''lower level input functions
<DllImport("User32.dll", SetLastError:=True)> _
Private Shared Function SendInput(ByVal nInputs As Integer, ByRef pInputs As INPUT64, ByVal cbSize As Integer) As Integer
End Function
<DllImport("User32.dll", SetLastError:=True)> _
Private Shared Function SendInput(ByVal nInputs As Integer, ByRef pInputs As INPUT32, ByVal cbSize As Integer) As Integer
End Function
<DllImport("User32.dll")> _
Private Shared Function BlockInput(ByVal fBlockIt As Boolean) As Boolean
End Function
<StructLayout(LayoutKind.Explicit, pack:=1)> _
Private Structure INPUT64
<FieldOffset(0)> Public dwType As InputType
<FieldOffset(8)> Public mi As MOUSEINPUT
<FieldOffset(8)> Public ki As KEYBDINPUT64
<FieldOffset(8)> Public hi As HARDWAREINPUT
End Structure
<StructLayout(LayoutKind.Explicit, pack:=1)> _
Private Structure INPUT32
<FieldOffset(0)> Public dwType As InputType
<FieldOffset(4)> Public mi As MOUSEINPUT
<FieldOffset(4)> Public ki As KEYBDINPUT32
<FieldOffset(4)> Public hi As HARDWAREINPUT
End Structure
<StructLayout(LayoutKind.Sequential, pack:=1)> _
Private Structure MOUSEINPUT
Public dx As Int32
Public dy As Int32
Public mouseData As Int32
Public dwFlags As MOUSEEVENTF
Public time As Int32
Public dwExtraInfo As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential, pack:=1)> _
Private Structure KEYBDINPUT64
<FieldOffset(0)> Public wVk As Int16
<FieldOffset(2)> Public wScan As Int16
<FieldOffset(4)> Public dwFlags As KEYEVENTF
<FieldOffset(12)> Public time As Int32
<FieldOffset(16)> Public dwExtraInfo As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential, pack:=1)> _
Private Structure KEYBDINPUT32
Public wVk As Int16
Public wScan As Int16
Public dwFlags As KEYEVENTF
Public time As Int32
Public dwExtraInfo As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential, pack:=1)> _
Private Structure HARDWAREINPUT
Public uMsg As Int32
Public wParamL As Int16
Public wParamH As Int16
End Structure
Private Enum InputType As Integer
Mouse = 0
Keyboard = 1
Hardware = 2
End Enum
<Flags()> _
Private Enum MOUSEEVENTF As Integer
MOVE = &H1
LEFTDOWN = &H2
LEFTUP = &H4
RIGHTDOWN = &H8
RIGHTUP = &H10
MIDDLEDOWN = &H20
MIDDLEUP = &H40
XDOWN = &H80
XUP = &H100
VIRTUALDESK = &H400
WHEEL = &H800
ABSOLUTE = &H8000
End Enum
<Flags()> _
Private Enum KEYEVENTF As Integer
KEYDOWN = 0
EXTENDEDKEY = 1
KEYUP = 2
[UNICODE] = 4
SCANCODE = 8
End Enum
Upvotes: 1
Views: 2582
Reputation: 463
Please check out this. C# PInvoking user32.dll on a 64 bit system
I imagine this also applies to the other user32.dll calls...
Source http://www.pinvoke.net/default.aspx/user32.SendMessage
Also if you are in a pinch... SendInput Keys in Win32 & Win64 machines
Upvotes: 1
Reputation: 14088
Not so much a solution to the problem, but have you tried the InputSimulator library: https://inputsimulator.codeplex.com/
I've used it on both 32/64-bit versions of Windows without a hitch.
Upvotes: 2