1

I have the current code:

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

If you take a look at the button sub, every time it runs, it errors with "Arithmetic overflow error!"

What is wrong here? This should work... right?

Sorry, this is a bit vague but it's as much as I know.

Freesnöw
  • 30,619
  • 30
  • 89
  • 138
  • Stuff that, when parsed (notice the line that gets the process) is the name of a process. The variable "CurrentProcess" turn out find. – Freesnöw Apr 12 '11 at 23:37
  • 1
    Why are you mixing `Declare` statements with `DllImport`? They're virtually equivalent styles, but it's best to pick one and stick with it. – Cody Gray - on strike Apr 13 '11 at 05:42

1 Answers1

5

Your Declare statements are correct but the way you call them is not. The loose typing allowed by VB.NET is getting you in trouble, the kind you can't diagnose. The cure for that is letting the compiler tell you that you did it wrong. Put this at the top of your source code file:

 Option Strict On
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    +1. Also [change your VB settings](http://msdn.microsoft.com/en-us/library/zcd4xwzs.aspx) so that `Option Strict` is on for every new source code file you create. On the Tools menu, click Options. Open the Projects and Solutions node. Choose VB Defaults. Modify the Option Strict setting. – MarkJ Apr 13 '11 at 08:15
  • Hmm, not *all* the Declare statements are correct. `FindWindow` has a return value of `Long` when it should have a return value of `IntPtr`. It looks like that one was copied from some VB 6 code. The sizes of `Integer` and `Long` have changed in VB.NET. In the future, check [pinvoke.net](http://www.pinvoke.net/default.aspx/user32.findwindow) for your declarations; they'll be up to date for .NET. – Cody Gray - on strike Apr 14 '11 at 05:22