Communicate with Win objects

Ralph DePetrillo

New member
Joined
Sep 26, 2007
Messages
2
Programming Experience
10+
Does anyone know how I can identify and communicate with Windows objects in external applications, such as Textboxes or Labels in another app? Ideally I need my app in memory collecting values from other app objects.
 
You need to use the Windows API, i.e. unmanaged code. You'd use the FindWindow function to get the parent window, then FindWindowEx to get the child controls, then SendMessage to interact with them. Here's some example code that populates user ID and password fields and then presses an OK button:
VB.NET:
    Private Sub handleTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles handleTimer.Tick
        If Me.handleWatch.ElapsedMilliseconds < Me.Timeout Then
            Me.Hook.TargetHandle = NativeMethods.FindWindow(Me._dialogueClassName, Me.dialogueTitle)

            If Me.Hook.TargetHandle <> IntPtr.Zero Then
                Me.handleTimer.Stop()
                Me.Hook.SetHook()
                Me.ProcessHookedDialogue()
            End If
        Else
            'The target window was not found within the timeout period.
            Me.handleTimer.Stop()
            MessageBox.Show(String.Format("The RMS '{0}' dialogue was not found.", _
                                          Me.DialogueTitle), _
                            "Dialogue Not Found", _
                            MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)
            Me.DialogResult = System.Windows.Forms.DialogResult.Abort
        End If
    End Sub


    Protected Overrides Sub ProcessHookedDialogue()
        MyBase.ProcessHookedDialogue()

        If Me.AutoLogin Then
            Dim dialogueHandle As IntPtr = Me.Hook.TargetHandle

            'Get the handles of the user ID and password fields and the OK button.
            Dim userIDFieldHandle As IntPtr = NativeMethods.FindWindowEx(dialogueHandle, _
                                                                         IntPtr.Zero, _
                                                                         My.Resources.RmsTextBoxClassNameString, _
                                                                         String.Empty)
            Dim passwordFieldHandle As IntPtr = NativeMethods.FindWindowEx(dialogueHandle, _
                                                                           userIDFieldHandle, _
                                                                           My.Resources.RmsTextBoxClassNameString, _
                                                                           String.Empty)
            Dim frameHandle As IntPtr = NativeMethods.FindWindowEx(dialogueHandle, _
                                                                   IntPtr.Zero, _
                                                                   My.Resources.RmsFrameClassNameString, _
                                                                   String.Empty)
            Dim okButtonHandle As IntPtr = NativeMethods.FindWindowEx(frameHandle, _
                                                                      IntPtr.Zero, _
                                                                      My.Resources.RmsButtonClassNameString, _
                                                                      "OK")

            If userIDFieldHandle <> IntPtr.Zero Then
                'Enter the user ID.
                NativeMethods.SendMessage(userIDFieldHandle, _
                                          NativeConstants.WM_SETTEXT, _
                                          0, _
                                          Me.userID)
            End If

            If passwordFieldHandle <> IntPtr.Zero Then
                'Enter the password.
                NativeMethods.SendMessage(passwordFieldHandle, _
                                          NativeConstants.WM_SETTEXT, _
                                          0, _
                                          Me.password)
            End If

            If okButtonHandle <> IntPtr.Zero Then
                'Click the OK button.
                NativeMethods.SendMessage(okButtonHandle, _
                                          NativeConstants.BM_CLICK, _
                                          0, _
                                          0)
            End If
        End If
    End Sub
I have declared the API functions in a C# library so showing you won't really help too much. I suggest that you first read about the Declare key word and the DllImport attribute, which are the two ways you can declare external functions in VB.NET, in the MSDN library. Then you should read about those functions on MSDN. Then you should get yourself some tools that are helpful for API programming, including WinID, API Viewer, API Guide and also Spy++, which is supplied with VS (although maybe not Express or Standard).
 
You don't need it. You can get the method signatures using the API Viewer. You can set the language to VB.NET or C#. It will also give you the values of the appropriate constants you will need to pass to SendMessage to accomplish different tasks.

Note that interaction with external programs almost always involves the Windows API.
 
Back
Top