Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 用64位系统模拟SendKey键盘鼠标_.net_Vb.net - Fatal编程技术网

.net 用64位系统模拟SendKey键盘鼠标

.net 用64位系统模拟SendKey键盘鼠标,.net,vb.net,.net,Vb.net,我有一套发送键函数,是我不久前写的。它在32位系统中工作。现在我再次测试了它,并意识到它在我新的win7 64位系统中不再工作了。 我在网上找到了几篇文章,然后编写了Input32/64函数。然而,它似乎不起作用 Dim result As Integer=SendInput(1,input,cbSize) 始终返回0,并且未模拟击键。你能看一下吗 (注意:我只制作了KEYBOARDINPUT64结构,它不工作,所以我没有制作MOUSEINPUT64或HARDWAREINPUT64结构…) ”鼠

我有一套发送键函数,是我不久前写的。它在32位系统中工作。现在我再次测试了它,并意识到它在我新的win7 64位系统中不再工作了。 我在网上找到了几篇文章,然后编写了Input32/64函数。然而,它似乎不起作用

Dim result As Integer=SendInput(1,input,cbSize)

始终返回0,并且未模拟击键。你能看一下吗

(注意:我只制作了KEYBOARDINPUT64结构,它不工作,所以我没有制作MOUSEINPUT64或HARDWAREINPUT64结构…)

”鼠标功能
专用共享子SendMouseInput(ByVal标志为MOUSEEVENTF,ByVal点为System.Drawing.point)
如果Marshal.SizeOf(New IntPtr())=8,则
SendMouseInput64(标志、点)
其他的
SendMouseInput32(标志、点)
如果结束
端接头
私有共享子SendMouseInput64(ByVal标志为MOUSEEVENTF,ByVal点为System.Drawing.point)
将屏幕变暗为矩形=My.Computer.screen.Bounds
Dim globalx作为整数=CInt((65535*point.X)/screen.Width)X从0到65535的比例
整体尺寸为整数=CInt((65535*point.Y)/screen.Height)Y比例从0到65535
将输入变暗为新输入64
input.dwType=InputType.Mouse
input.mi.dx=globalx
input.mi.dy=globaly
input.mi.dwFlags=MOUSEEVENTF.ABSOLUTE或flags
input.mi.dwExtraInfo=IntPtr.Zero
input.mi.mouseData=0
input.mi.time=0
SendInput(1,输入,封送处理大小(输入))
端接头
专用共享子SendMouseInput32(ByVal标志为MOUSEEVENTF,ByVal点为System.Drawing.point)
将屏幕变暗为矩形=My.Computer.screen.Bounds
Dim globalx作为整数=CInt((65535*point.X)/screen.Width)X从0到65535的比例
整体尺寸为整数=CInt((65535*point.Y)/screen.Height)Y比例从0到65535
将输入变暗为新输入32
input.dwType=InputType.Mouse
input.mi.dx=globalx
input.mi.dy=globaly
input.mi.dwFlags=MOUSEEVENTF.ABSOLUTE或flags
input.mi.dwExtraInfo=IntPtr.Zero
input.mi.mouseData=0
input.mi.time=0
SendInput(1,输入,封送处理大小(输入))
端接头
“键盘功能
私有共享子SendKeyInput(ByVal标志为KEYEVENTF,ByVal键为Int16)
如果Marshal.SizeOf(New IntPtr())=8,则
SendKeyInput64(标志、键)
其他的
SendKeyInput32(标志、键)
如果结束
端接头
私有共享子SendKeyInput64(ByVal标志为KEYEVENTF,ByVal键为Int16)
将输入变暗为新输入64
将ki变暗为新的键bdinput64
input.dwType=InputType.Keyboard
input.ki=ki
input.ki.wVk=键
input.ki.wScan=0
input.ki.time=0
input.ki.dwFlags=标志
input.ki.dwExtraInfo=IntPtr.Zero
Dim cbSize As Integer=Marshal.SizeOf(GetType(INPUT64))
Dim结果为整数=发送输入(1,输入,cbSize)
如果结果=0,则Console.WriteLine(“SendKeyInput:&Marshal.GetLastWin32Error”)
端接头
私有共享子SendKeyInput32(ByVal标志为KEYEVENTF,ByVal键为Int16)
将输入变暗为新输入32
将ki调暗为新的键B输入32
input.dwType=InputType.Keyboard
input.ki=ki
input.ki.wVk=键
input.ki.wScan=0
input.ki.time=0
input.ki.dwFlags=标志
input.ki.dwExtraInfo=IntPtr.Zero
Dim cbSize As Integer=Marshal.SizeOf(GetType(INPUT32))
Dim结果为整数=发送输入(1,输入,cbSize)
如果结果=0,则消息传递.Instance.ReportError(“MouseKeyboard::SendKeyInput:”&Marshal.GetLastWin32Error)
端接头
''低级别输入功能
_
私有共享函数SendInput(ByVal nInputs为整数,ByRef pInputs为INPUT64,ByVal cbSize为整数)为整数
端函数
_
私有共享函数SendInput(ByVal nInputs为整数,ByRef pInputs为INPUT32,ByVal cbSize为整数)为整数
端函数
_
私有共享函数BlockInput(ByVal fBlockIt为布尔值)为布尔值
端函数
_
私有结构输入64
作为InputType的公共dwType
作为鼠标输入的公共mi
公共ki作为KEYBDINPUT64
公共hi作为硬件输入
端部结构
_
私有结构输入32
作为InputType的公共dwType
作为鼠标输入的公共mi
公共ki作为KEYBDINPUT32
公共hi作为硬件输入
端部结构
_
私有结构鼠标输入
公共dx As Int32
公共数据类型为Int32
公共鼠标数据作为Int32
作为MOUSEEVENTF的公共标志
公共时间为Int32
公共dwExtraInfo作为IntPtr
端部结构
_
私有结构keybInput64
公共wVk As Int16
公共wScan As Int16
公共dwFlags作为KEYEVENTF
公共时间为Int32
公共dwExtraInfo作为IntPtr
端部结构
_
私有结构KEYBDINPUT32
公共wVk As Int16
公共wScan As Int16
公共dwFlags作为KEYEVENTF
公共时间为Int32
公共dwExtraInfo作为IntPtr
端部结构
_
私有结构硬件输入
公共uMsg As Int32
公共wParamL As Int16
公共wParamH As Int16
端部结构
私有枚举InputType为整数
鼠标=0
键盘=1
硬件=2
结束枚举
_
私有枚举MOUSEEVENTF为整数
移动=&H1
LEFTDOWN=&H2
LEFTUP=&H4
右下=&H8
RIGHTUP=&H10
米德尔顿=&H20
MIDDLEUP=&H40
XDOWN=&H80
XUP=&H100
虚拟磁盘=&H400
车轮=&H800
绝对值=&H8000
结束枚举
_
私有枚举KEYEVENTF为整数
KEYDOWN=0
EXTENDEDKEY=1
KEYUP=2
[UNICODE]=4
扫描码=8
结束枚举

这不是问题的解决方案,但您是否尝试过InputSimulator库:

我在两个32/64位版本的Windows上都使用过它,没有任何问题。

请查看这个。

我想这也适用于其他user32.dll调用

  • 使用IntPtr而不是UIntrPtr:UIntPtr类型不符合CLS
  • 决不要将“int”或“integer”用作LPRAM。您的代码将在64位windows上崩溃。只有你
    ''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