Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

WM_GETTEXT does not retrieve text from some foreign windows

Posted on 2008-10-27
5
1,231 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

860 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