Retrieving Text From Active Window Outside of My Application / API Calls

ss7thirty

Well-known member
Joined
Jun 14, 2005
Messages
455
Location
New Jersey, US
Programming Experience
5-10
I am working on a program and wish to add some extra functionality to the application. I wish to recognize other windows that are gaining focus. And based on which windows takes focus it will send keystrokes to that window. Sometimes closing it, sometimes sending information, sometimes doing nothing. I wish to know which API call or appropriate .NET function there is to gain control of the active window (often outside of the application).

For example, if my application is running and someone clicks on notepad, my application will recognize that a new windows has gained focus and that this is not a window that I need. If they open one of the windows I am looking for I would like to be able to send keystrokes to this application (Which I know how to do) and possibly get text from a textbox (Don't know). This application could be written in a variety of different languages and therefore I need to use functions that are not platform dependant to retrieve information from another application/window.

I have been researching this for a while but am still drawing blanks. But there has to be a way to see what window has gained focus and retrieve text from the various controls on that windows. After all, Windows does it. You can highlight the text and copy it and windows will then store it into memory so then what do I have to do to essentially force the same action programmatically when a certain windows comes into focus.

I have done some additional research and found a Visual C++ Project that did some similar things. It is open source, however, I am having trouble interpreting some of the code because it is pretty complex. However, it does prove that this is possible, I am just not sure if it can be easily accomplished in VB.NET. The following link contains the source code and the executable. The executable is all that needs to be reviewed if I can just figure out which API calls they are making or how they access the information that they access I should be able to easily figure out how to implement it. Anyway, here is the link:

http://www.nirsoft.net/utils/winexp.html
 
Last edited:
The code from this Last activated application post works. It RegisterWindowMessage("SHELLHOOK") and RegisterShellHookWindow, this enables the application to get messages through WndProc where you can detect HSHELL_WINDOWACTIVATED.
 
That really works perfectly. I cut and pasted it in and already was almost doing what I wanted it to this is great. Thats it, I'm moving to Norway. :eek:

I do need one more thing. The ability read text in the chat windows in the program. This chat window does not contain selectable text so this may prove to be harder than I thought. It is a chat window attached but in a separate "application" according to the task manager but on the same process. Would it be possible to read the text coming through there through some other API call. Now that I can select the window and send key strokes to it.
 
Thats it, I'm moving to Norway. :eek:
Stay, you won't get acclimated to the weather in a lifetime. ;) (expect 30-60F always and lots rain)

Try Winspector to find the type of control and where it is, using the current handle and Sendmessage WM_GETTEXT might get you the text. Later FindWindow and FindWindowEx can be used to dynamically getting the handle.
 
My apologies I am really knew to the whole API call thing, but they seem to be a pretty powerful tool. Unfortunately, there is no clearly documented "archive" or all of the Windows API calls. I wonder why. Probably because they can be used for Spyware and stuff like that.

I see that it will work. I have downloaded the program and navigated to the window I want. I right-clicked and displayed messages and see WM_GETTEXT. I see the contents of the window. This means that it would be possible to get the text. Therefore what would the API call look like to get the text for that window. Or is it possible that I could do it through this software. Meaning send this program a message and retrieve the text. I am a bit confused.
 
What control is it?

The web is practically flooded with samples of stuff like this, just search "WM_GETTEXT". Here's one more, 'hwnd' is the handle to the window (eg control):
VB.NET:
Dim tl As Integer = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0)
Dim t As New String(" ", tl)
tl = SendMessage(hwnd, WM_GETTEXT, tl + 1, t)
msgbox(t)
These declaration were used, get ApiViewer:
VB.NET:
    Const WM_GETTEXT As Int32 = &HD
    Const WM_GETTEXTLENGTH As Int32 = &HE
    Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
        ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
        ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Int32
 
Dear ss7thirty,
I have read the following post of u: Retrieving Text From Active Window Outside of My Application / API Calls
I also need the solution of same issue. but not understanding winexp.exe source . cause its hard to interprate for a .net programmer.
please i need ur assist to get a solution on it. u told that u solved it in .net. can i get the source of ur application or any part of application by which i can understand the details of what i have to do.
i need to know the api u used and also the coding that make mt target.
thank you very much........
 
Back
Top