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
LVL 1
ofern01Asked:
Who is Participating?
 
JRossi1Commented:
To activate another app:  store the window handle of the other application and then use BringWindowToTop to activate the app.

Also, look at this:  http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21112857.html#11941064
0
 
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.
0
 
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
            SetForegroundWindow(hWnd)
            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.
0
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.