Results 1 to 14 of 14

Thread: Visual Basic Get Text of Another Window's TextBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    10

    Visual Basic Get Text of Another Window's TextBox

    Good day to all of you VB gurus!

    I would like to requests some help on how to create a VB6 code which will show me how to capture text from the EasyGPRS (Samsung) program. Within that program, SMS messages from a cellphone will be displayed their. I would like to get the actual message from the SMS window like shown below.



    Basically, the word/string "Pau" must be obtained from the window then transferred to a text file (*.txt) so we can be able to apply it to our program.

    Hope someone can help me on how I can do that.

    Thanks!

  2. #2
    Lively Member
    Join Date
    Jul 2007
    Posts
    98

    Re: Visual Basic Get Text of Another Window's TextBox

    if that can be done at all VB is not the language. Theoretically you must get a handle to the window you showed in the screenshot. That is easy to be done.
    The next part is fuzzy - you must locate somehow the pointer to the desired textbox. After you got it you must convert it to an object that is editable from your source code and extract the data. The problem that remains is how to open the desired tool window, and how to recognize it.
    I think it would be better to try locating the programms database and to extract the data direct from there.

  3. #3
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Visual Basic Get Text of Another Window's TextBox

    It can be done through the use of APIs (and Spy). You'd need to be able to somehow identify the pop-up window (perhaps by the widow's title) and obtain it's hwnd. (Using FindWindow) Once you've got that you can search that window's components to find the hwnd of the TextBox.(FindWindowEx) You can then use SendMessage to return the contents of the TextBox.

    Spy will help you find the appropriate names of the windows you are interested in.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    10

    Re: Visual Basic Get Text of Another Window's TextBox

    Thanks for the replies. However, I have no idea about the program (?) Spy. Can you guys enlighten me on how to use the FindWindow() function? I am still a newbie when it comes to programming.

    TIA!

  5. #5
    New Member
    Join Date
    Jan 2008
    Location
    South East England, UK
    Posts
    4

    Re: Visual Basic Get Text of Another Window's TextBox

    See my question on a different subject at;
    http://www.codeguru.com/forum/showthread.php?t=444508

    In the attached program is a module 'mod_WindowInfo.mod'
    (This code was edited from code I found on the net)

    You don't need to have a reference to
    Microsoft Shell Controls and Automation
    or Microsoft Internet Controls
    if you delete the functions ListAllFolderPaths() and GetExplorerFolderPath()

    Uses GetWindowInfo() to get info under the pointer.
    Eg. Text1.Text = GetWindowInfo(1) & " :: Name=" & GetWindowInfo(2)

    See http://allapi.mentalis.org/ for a list of API's.
    for info and demo on using FindWindow().

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    10

    Re: Visual Basic Get Text of Another Window's TextBox

    sorry but i'm still confused on how to do this. i researched about getwindowtext() functions but i still can;t figure out how to do it. can you give me a simple example just like in my query on the first post of this thread?

  7. #7
    Lively Member
    Join Date
    Jul 2007
    Posts
    98

    Re: Visual Basic Get Text of Another Window's TextBox

    Quote Originally Posted by BlackSunny
    can you give me a simple example just like in my query on the first post of this thread?
    nope, what you are tring to do is not simple at all(if possible). You have to use the hints given from the collegues above an do it by yourself.

  8. #8
    Member
    Join Date
    Dec 2007
    Posts
    57

    Re: Visual Basic Get Text of Another Window's TextBox

    Quote Originally Posted by tatkosmurff
    nope, what you are tring to do is not simple at all(if possible). You have to use the hints given from the collegues above an do it by yourself.
    This most certainly is possible - it's a simple screen-scraping task.

    BlackSunny - Here's the basics:

    Every window on your screen has a 'handle' which is a unique number that's used as a reference to interact with the window. Controls (buttons, textboxes etc.) are also 'windows' so they have handles too. The actual value of the handle (just a long number) isn't important because each window gets a new handle every time it's opened, so we need a way to retrieve the handle every time we want to run our program. This can be done with the FindWindow() function. FindWindow is a Windows API function, so to use it in your program you need to declare it at the top of your code, like this:
    Code:
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal strClassName As String, ByVal strWindowText As String) As Long
    The 'class name' refers to the type of window you're trying to retrieve - it can take some investigation to find out what this should be. The 'window text' is the text that appears in the titlebar (for windows) or on the control. You don't even have to specify the class, so, for example, you could write...
    Code:
    lngHandle = FindWindow("", "Untitled - Notepad")
    ...to get the handle of a blank Notepad window. Once you've got the handle of your window, you can also use another function called FindWindowEx (the 'ex' is short for 'extended') that enables you to search for 'child' windows inside another window - in other words - controls. Finally, once you've got the handle of your control, you can retrieve the text inside that textbox by sending it a message. Read up about SendMessage() and WM_GETTEXT. Unfortunately, the GetWindowText() function isn't designed to work between one program and another, but the WM_GETTEXT message should work fine.

    I hope that helps, remember to keep Googling and always refer to the documentation at MSDN.

    reinholdmesner

    Edit: In your case, it may be difficult to retrieve the correct handle because the 'View Message' window isn't a standard window with a standard titlebar (with 'window text'). However, there are various ways to find out the correct 'class name' to search for - I think the 'Spy' program mentioned helps with this (I've never used it myself). If all else fails, there is an API function that retrieves the handle of whichever window is directly underneath your mouse cursor, so you can use this to find out the handle, then GetClassName() to find out which class to specify in FindWindow().
    Last edited by reinholdmesner; Feb 6th, 2008 at 08:50 AM.

  9. #9
    Lively Member
    Join Date
    Jul 2007
    Posts
    98

    Re: Visual Basic Get Text of Another Window's TextBox

    reinholdmesner

    what you say is so, but :

    1. Since that is not a normal window, it can not be captured with the
    FindWindow function (as you said)
    2. The Spy tool will return some data for the window that is searched, but I am not sure if the data is the one needed to get a handle to the window.
    3. The handle you propose to be searched is not allways the same since that is not a system device but a dialog window.

    Why I said above that is not a simple task. Using API in VB is ok but VB is not so good at handling API data. This is why I suppose using a more powerfull language as C++ C# etc.

    I think that the best solution for this problem is to locate where the programm stores its data or to bild "SMS Driver" on your own. There are sample solutions on the net.
    In the end of all programming is about processing data, not stealing it from third party apps via a confused use of Windows API.

  10. #10
    Member
    Join Date
    Dec 2007
    Posts
    57

    Re: Visual Basic Get Text of Another Window's TextBox

    1. Whilst the window doesn't look like a standard dialog, it is most probably still a window. I doubt very much that every control is custom-drawn to a windowless device context - it's just not worth the effort. I see no reason why FindWindow() can't be used.

    2. There are any number of methods that can be used to get the window's handle. In the past, I've even written screen-scraping software that enumerated child windows and retrieved the correct control's handle by its coordinates from the parent window's origin. If 'Spy' tells you at least the class name then you're half-way there.

    3. Of course the handle won't be the same, that's why we write code to retrieve it programmatically. It doesn't make any difference whether a window is system-created or application-defined.

    Lastly, I agree that serious applications should written in C or Assembly, but for something as simple as this, VB6 is a good choice. When used correctly, VB has complete support for all data types used by the Win32API, and is great at handling any type of data, including pointers. I've worked in world-leading financial institutions that make extensive use of screen-scraping software because it is often the simplest, quickest and cheapest way of getting something done reliably. If we all had the time we'd use different development environments but VB is widely recognised as the best balance of control and power versus ease of use and development time.

    reinholdmesner

  11. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Visual Basic Get Text of Another Window's TextBox

    Just putting the technical issues to the side for a minute. How do you expect the overall System to function?

    It looks as if the User has to open the Samsung Application and click on something to display a message. They then have to, presumably, click on a button in your application which will locate the contents of the 'Message' window and write it to a file.

    That seems to be quite user intensive and it might be easier to manually Copy and Paste it into Notepad.

    Going back to the technical issues, if you're thinking of automatically capturing the message contents every / any time the Message window of the Samsung Application is opened by the user, then that's another set of issues that need to be addressed.

    I'm wondering whether, as you seem to have connectivity to the device established, using MSComm (if the device appears as a Comm port) to directly talk to the device, using the extended AT Command set to extract messages, would be a more suitable solution.

    For example, there's an AT command (AT+CMGL="REC UNREAD") which will return all the un-read messgages from the device. Having had a quick search on the Internet it appears that Samsung have implemented the Extended AT Command set so the above command should work.
    Last edited by Doogle; Feb 7th, 2008 at 05:53 AM.

  12. #12
    New Member
    Join Date
    Jul 2008
    Posts
    7

    Re: Visual Basic Get Text of Another Window's TextBox

    Sorry to bump a thread, but my question is related..

    Here's the code i use to capture text from a chatprogram

    vb Code:
    1. Imports System.Runtime.InteropServices
    2. Public Class Form1
    3.     Private Const WM_GETTEXT As Integer = &HD
    4.     Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, _
    5.     ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    6.     <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    7.     Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
    8.                                          ByVal childAfter As IntPtr, _
    9.                                          ByVal lclassName As String, _
    10.                                          ByVal windowTitle As String) As IntPtr
    11.     End Function
    12.  
    13.     Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    14.  
    15.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    16.         'Find the running notepad window
    17.         Dim Hwnd As IntPtr = FindWindow("BRWindowClass", Nothing)
    18.  
    19.         'Alloc memory for the buffer that recieves the text
    20.         Dim Handle As IntPtr = Marshal.AllocHGlobal(100)
    21.  
    22.         'send WM_GWTTEXT message to the notepad window
    23.         Dim NumText As Integer = SendMessage(Hwnd, WM_GETTEXT, 50, Handle)
    24.  
    25.         'copy the characters from the unmanaged memory to a managed string
    26.         Dim Text As String = Marshal.PtrToStringUni(Handle)
    27.  
    28.         'Display the string using a label
    29.         Label1.Text = Text
    30.  
    31.         'Find the Edit control of the Running Notepad
    32.         Dim ChildHandle As IntPtr = FindWindowEx(Hwnd, IntPtr.Zero, "Edit", Nothing)
    33.  
    34.         'Alloc memory for the buffer that recieves the text
    35.         Dim Hndl As IntPtr = Marshal.AllocHGlobal(200)
    36.  
    37.         'Send The WM_GETTEXT Message
    38.         NumText = SendMessage(ChildHandle, WM_GETTEXT, 200, Hndl)
    39.  
    40.         'copy the characters from the unmanaged memory to a managed string
    41.         Text = Marshal.PtrToStringUni(Hndl)
    42.  
    43.         'Display the string using a label
    44.         Label2.Text = Text
    45.  
    46.     End Sub

    The problem is instead of getting the actual text i get characters like: ē Ġ ĭ. everytime i click the button.
    Ive used Winspy++ to get the classnames and using Winspy i can capture text properly. Ive also set it to another class inside the program, and using this code it captures properly, but it doesnt work for the textbox i want to capture. What could be the cause of getting the random characters?

  13. #13
    New Member
    Join Date
    Jul 2008
    Posts
    7

    Re: Visual Basic Get Text of Another Window's TextBox

    Ok, i think ive found the problem.
    The "Edit" class is actually a child of a class called "MDINFO"

    so, i think i have to modify to code to first find the main parent = "Brwindowclass" - current code does this
    second, it must connect to the child of Brwindowclass = "MDINFO" - current code does this

    but now 3rd step: how do I connect and retrieve the text of the child of MDINFO, called "edit"?

    Ive tried this but it doesnt work.

    vb Code:
    1. Imports System.Runtime.InteropServices
    2. Public Class Form1
    3.     Private Const WM_GETTEXT As Integer = &HD
    4.     Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, _
    5.     ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    6.     <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    7.     Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
    8.                                          ByVal childAfter As IntPtr, _
    9.                                          ByVal lclassName As String, _
    10.                                          ByVal windowTitle As String) As IntPtr
    11.     End Function
    12.  
    13.     Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    14.  
    15.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    16.         'Find the running notepad window
    17.         Dim Hwnd As IntPtr = FindWindow("BRWindowClass", Nothing)
    18.  
    19.         'Alloc memory for the buffer that recieves the text
    20.         Dim Handle As IntPtr = Marshal.AllocHGlobal(100)
    21.  
    22.         'send WM_GWTTEXT message to the notepad window
    23.         Dim NumText As Integer = SendMessage(Hwnd, WM_GETTEXT, 50, Handle)
    24.  
    25.         'copy the characters from the unmanaged memory to a managed string
    26.         Dim Text As String = Marshal.PtrToStringUni(Handle)
    27.  
    28.         'Display the string using a label
    29.         Label1.Text = Text
    30.  
    31.         'Find the Edit control of the Running Notepad
    32.         Dim ChildHandle As IntPtr = FindWindowEx(Hwnd, IntPtr.Zero, "MDIClient", Nothing)
    33.         Dim Childhandle2 As IntPtr = FindWindowEx(ChildHandle, IntPtr.Zero, "Edit", Nothing)
    34.  
    35.  
    36.         'Alloc memory for the buffer that recieves the text
    37.         Dim Hndl As IntPtr = Marshal.AllocHGlobal(200)
    38.  
    39.         'Send The WM_GETTEXT Message
    40.         NumText = SendMessage(Childhandle2, WM_GETTEXT, 200, Hndl)
    41.  
    42.         'copy the characters from the unmanaged memory to a managed string
    43.         Text = Marshal.PtrToStringUni(Hndl)
    44.  
    45.         'Display the string using a label
    46.         TextBox1.Text = Text
    47.  
    48.     End Sub
    49. End Class
    Last edited by neoxyn; Mar 26th, 2010 at 11:39 AM.

  14. #14
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: Visual Basic Get Text of Another Window's TextBox

    Use any API-Spy utility to spy on that area of the Edit window.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width