Solved

WM_GETTEXT does not retrieve text from some foreign windows

Posted on 2008-10-27
5
1,220 Views
Last Modified: 2012-08-14
I am using a EM_GETTEXTLENGTHEX, the SendMessage() and EnumChildWindows function to retrieve the text that is displayed in other windows.

The combination of these results in me being able to perfectly well retrieve the text from the current form that I have created in Visual Basic, as well as text from some other applications, such as Notepad.

However, these calls do not work when I try and retrieve the text from some other applications, for example Windows Live Messenger, Word, Internet Explorer or Firefox.

This leads me to believe that the way I am retrieving and using the hWnd's from EnumChildWindows is incorrect or I am missing a vital part of code.

At the moment, if I were to use EnumChildWindows to retrieve the child hWnd's from say, Word or Live Messenger, I would have several hWnd's returned, but none of them have any textual response from SendMessage() with the EM_GETTEXTLENGTHEX param.

My question is, looking at the code below, should I be using something other than EnumChildWindows and/or does EnumChildWindows return the children of the children of the original hWnd, or just the children of the hWnd I passed it originally? If the answer to the latter is that it only passes me the children of the hWnd, how would I go about retrieving the children of those children? (If that makes any sense what-so-ever.)

ps. My apologies for the absolutely dreadful code, unfortunately it's messy because it I'm trying to make it work, not make it look pretty (yet!)

TextBox4.Text = ""
 

        Dim hWnd As IntPtr
 

        hWnd = GetForegroundWindow()
 

	// Calls to the TextBox4.Text are purely for seeing the result

	// of each function during testing

        TextBox4.Text = "hWnd = " + hWnd.ToString() + Environment.NewLine
 

        EnumChildWindows(hWnd, AddressOf EnumChildProc, 0)
 

        For i = 0 To UBound(lHwnd)

           
 

            Dim hWnd22 As IntPtr

	    

	    // hWnd22 is the child of hWnd

            hWnd22 = lHwnd(i)

            TextBox4.Text = TextBox4.Text + "Child hWnd = " + hWnd22.ToString() + Environment.NewLine
 
 

            ' StringBuilder SendMessage test

            Dim sb As StringBuilder = New StringBuilder("", 512)
 
 

            Dim tl As Integer = SendMessage(hWnd22, WM_GETTEXTLENGTH, vbNull, sb)

            Dim t As New String(" ", tl + 512)

          

	    // This is the call that retrieve the text from the hWnd

            tl = SendMessage(hWnd22, EM_GETTEXTLENGTHEX, 5120, sb)
 

            TextBox4.Text = TextBox4.Text + "STRING = " + sb.ToString() + Environment.NewLine + Environment.NewLine
 

        Next i

Open in new window

0
Comment
Question by:DarkFish
  • 3
  • 2
5 Comments
 
LVL 3

Author Comment

by:DarkFish
ID: 22812368
Actually; thinking about it a bit more, the reason I'm not getting a response from windows such as Live Messenger, Firefox or IE is because they don't use work in the same way as Notepad, meaning that even using Spy++, it's not possible to get the text that is being rendered on screen.

So, is it possible (using another method) to be able to read the text that these applications output without having to use libraries and APIs for each individual specific application? (E.g. without using the IE object to get the document content)

Another option might be to use an OCR screen reader, but this would be slow, cumbersome and not what I'm really after.


0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22824576
"...without having to use libraries and APIs for each individual specific application?"

The simple answer is No.  =(

If the control doesn't respond to a standard message such as WM_GETTEXT then you have to "specialize" your code.

As far as I know, there is no UNIVERSAL method to get text from ANY control...
0
 
LVL 3

Author Comment

by:DarkFish
ID: 22824842
That's what I was fearing. I know that I could write specific API code to retrieve the text from these sort of applications; the trouble is the application I'm writing needs to be able to scan the entire screen or the foreground window for specific words - it has to be able to read every window.

I could use an OCR screen reader and do it that way; but unfortunately as I said previously this would be slow and use up too might CPU considering I'd need to hook that in every time an event on screen occurred.

The application needs to be deployed in a environment where there are a number of unknown applications that might not respond to WM_GETTEXT and similar messages.

There are applications that do something like what I am after, which scan the screen for specific keywords - while that's what I'm trying to do, it's not quite the same. If I knew how these applications worked exactly that would be fantastic, but considering they are closed source that's not going to happen. In addition they are able to scan all on screen text quite efficiently.

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22825014
0
 
LVL 3

Author Comment

by:DarkFish
ID: 22831068
Sorry EE didn't inform me (or I most likely missed it) that you had posted an update.

Thank you very much for your links, I never thought of looking for something similar to that. :)

Again, many thanks and I'll be reading over that as soon as I submit this. :)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now