How Can I Activate an App from Another App and Send Key Strokes

I have 2 separate applications running. From One application I need to activate (bring to front) the other application and send some keyboard keys to simulate the used pressing those keys.
The application that will activate the other is on VB.NET VS2005
Who is Participating?
To activate another app:  store the window handle of the other application and then use BringWindowToTop to activate the app.

Also, look at this:
ofern01Author Commented:
I do not call the other app from my VB program. It is already running when I exec my App. So i will need a way to find that handle from my VB program.
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can use code like this to get a handle to the other apps window and Send keystrokes:

    Private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hWnd As IntPtr) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim ps() As Process = Process.GetProcessesByName("notepad")
        If ps.Length > 0 Then
            Dim p As Process = ps(0)
            Dim hWnd As IntPtr = p.MainWindowHandle
            SendKeys.Send("Hello World!")
        End If
    End Sub

Notice that in GetProcessesByName(), we pass in the name of the Exectuable without any path and without the ".exe" extension.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.