【问题标题】:Error Getting Form Handler (Vb.net)获取表单处理程序时出错 (Vb.net)
【发布时间】:2011-04-12 23:26:27
【问题描述】:

我有当前代码:

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

如果您查看按钮 sub,每次运行时,都会出现“算术溢出错误!”的错误!

这里有什么问题?这应该可以……对吧?

对不起,这有点含糊,但据我所知。

【问题讨论】:

  • 在解析时(注意获取进程的行)是进程的名称。变量“CurrentProcess”原来是find。
  • 为什么将Declare 语句与DllImport 混合使用?它们几乎是相同的风格,但最好选择一种并坚持下去。

标签: vb.net math sendmessage findwindow


【解决方案1】:

您的 Declare 陈述是正确的,但您称呼它们的方式却不正确。 VB.NET 允许的松散类型给你带来了麻烦,你无法诊断。解决这个问题的方法是让编译器告诉你你做错了。将其放在源代码文件的顶部:

 Option Strict On

【讨论】:

  • +1。还有change your VB settings,以便Option Strict 对您创建的每个新源代码文件都启用。在工具菜单上,单击选项。打开项目和解决方案节点。选择 VB 默认值。修改 Option Strict 设置。
  • 嗯,并非所有声明语句都是正确的。 FindWindow 的返回值是 Long,而它的返回值应该是 IntPtr。看起来那是从一些 VB 6 代码中复制的。 IntegerLong 的大小在 VB.NET 中发生了变化。将来,请检查pinvoke.net 以获取您的声明;它们将是 .NET 的最新版本。
猜你喜欢
  • 1970-01-01
  • 2013-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
  • 2013-05-14
相关资源
最近更新 更多