获取表单处理程序(Vb.net)时出错

获取表单处理程序(Vb.net)时出错,vb.net,math,sendmessage,findwindow,Vb.net,Math,Sendmessage,Findwindow,我有当前代码: Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ Private Shared Function

我有当前代码:

Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As HandleRef, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
End Function

Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

Public Function WindowHandle(ByVal sTitle As String) As Long
    WindowHandle = FindWindow(vbNullString, sTitle)
End Function

Dim CurrentProcess As Process
Dim CurrentHandle As IntPtr

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    SendMessage(CurrentHandle, 256, Keys.A, 0)
    SendMessage(CurrentHandle, 257, Keys.A, 65539)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        CurrentProcess = Process.GetProcessesByName(ListBox1.SelectedItem.ToString.Remove(0, ListBox1.SelectedItem.ToString.IndexOf("{") + 1).Replace("}", ""))(0)
        CurrentHandle = New IntPtr(WindowHandle(CurrentProcess.MainWindowTitle))
        Timer1.Start()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
Public声明函数findwindowlib“user32.dll”别名“FindWindowA”(ByVal lpClassName作为字符串,ByVal lpWindowName作为字符串),长度为
_
私有共享函数SendMessage(ByVal hWnd作为HandleRef,ByVal Msg作为UInteger,ByVal wParam作为IntPtr,ByVal lParam作为String)作为IntPtr
端函数
将自动函数SendMessage Lib“user32.dll”(ByVal hWnd作为IntPtr,ByVal msg作为Integer,ByVal wParam作为IntPtr,ByVal lParam作为IntPtr)声明为IntPtr
公共函数WindowHandle(ByVal缝合为字符串)长度相同
WindowHandle=FindWindow(vbNullString,缝合线)
端函数
作为进程的进程
Dim CurrentHandle As IntPtr
私有子Timer1_Tick(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理Timer1.Tick
SendMessage(CurrentHandle,256,key.A,0)
SendMessage(CurrentHandle,257,Keys.A,65539)
端接头
私有子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
尝试
CurrentProcess=Process.GetProcessByName(ListBox1.SelectedItem.ToString.Remove(0,ListBox1.SelectedItem.ToString.IndexOf(“{”)+1)。替换(“}”,”)(0)
CurrentHandle=New IntPtr(WindowHandle(CurrentProcess.MainWindowTitle))
Timer1.Start()
特例
MsgBox(例如消息)
结束尝试
端接头
如果你看一下按钮子,每次它运行时,都会出现“算术溢出错误!”

这里怎么了?这应该有用。。。对吧?

抱歉,这有点模糊,但据我所知。

您的Declare语句是正确的,但您称之为Declare的方式却不正确。VB.NET允许的松散键入正在给您带来麻烦,这是一种您无法诊断的问题。解决方法是让编译器告诉你你做错了。将其放在源代码文件的顶部:

 Option Strict On
您的Declare语句是正确的,但您调用它们的方式却不正确。VB.NET允许的松散键入正在给您带来麻烦,这是一种您无法诊断的问题。解决方法是让编译器告诉你你做错了。将其放在源代码文件的顶部:

 Option Strict On

当解析时(注意获取进程的行)是进程的名称。变量“CurrentProcess”的结果是find。为什么要将
Declare
语句与
DllImport
混合使用?它们实际上是等价的样式,但最好选择一种并坚持使用。当解析时(注意获取流程的行)就是流程的名称。变量“CurrentProcess”的结果是find。为什么要将
Declare
语句与
DllImport
混合使用?它们实际上是等效的样式,但最好选择一种并坚持使用。另外,对于您创建的每个新源代码文件,都启用了
选项Strict
。在“工具”菜单上,单击“选项”。打开“项目和解决方案”节点。选择VB默认值。修改选项Strict设置。嗯,不是所有的Declare语句都正确
FindWindow
的返回值为
Long
,而它的返回值应为
IntPtr
。它看起来像是从一些VB6代码中复制的。在VB.NET中,
Integer
Long
的大小已更改。今后,检查您的申报;它们将是.NET.+1的最新版本。另外,对于您创建的每个新源代码文件,都启用了
选项Strict
。在“工具”菜单上,单击“选项”。打开“项目和解决方案”节点。选择VB默认值。修改选项Strict设置。嗯,不是所有的Declare语句都正确
FindWindow
的返回值为
Long
,而它的返回值应为
IntPtr
。它看起来像是从一些VB6代码中复制的。在VB.NET中,
Integer
Long
的大小已更改。今后,检查您的申报;它们将是.NET的最新版本。