Solved

Retrieve text of focused control in another application, namely IE

Posted on 2003-12-02
8
678 Views
Last Modified: 2007-12-19
I want to write a function to retrieve the text of the active (focused) control, even if the control belongs to another application.

The function below does that in most cases.  However, it doesn't work if Internet Explorer is the foreground application and the user is in, for example, a TEXTAREA field.  (Like the one that I am typing in right now.)  Does that control not respond to the WM_GetText message or do you think GetFocus() is returning the wrong handle?  In either case, how would I go about reading the text from this type of control within IE?

function GetActiveText: String;
const
  MaxTextLen = 5000;
var
  FW, FC : HWND;
  ThID : DWORD;
  Buf : array[0..MaxTextLen] of Char;
begin
  FW := 0;
  FC := 0;
  FW := GetForegroundWindow;
  ThID := GetWindowThreadProcessId(FW,0);
  AttachThreadInput(GetCurrentThreadId(),ThID,TRUE);
  try
    FC  := GetFocus();
  finally
    AttachThreadInput(GetCurrentThreadId(),ThID,FALSE);
  end;
  SendMessage(FC,WM_GETTEXT,MaxTextLen,Integer(@Buf));
  Result := Buf;
end;

0
Comment
Question by:roknjohn
  • 4
  • 2
  • 2
8 Comments
 
LVL 8

Expert Comment

by:gmayo
ID: 9861101
Not sure specifically why your code does not work. However, I use the following to get text from another app's edit boxes:

function GetWindowTextEx(win : HWND) : string;
var
      s : array [1..513] of char;
      i : integer;
      res : DWORD;
begin
      Result := '';
      SendMessageTimeout(win, WM_GETTEXT, 512, Integer(@s[1]), 0, 1000, res);
      i := 1;
      while (i < 513) and (s[i] <> #0) do begin
            Result := Result + s[i];
            Inc(i);
      end;
end;

It could be improved but it works.

Geoff M.
0
 
LVL 1

Author Comment

by:roknjohn
ID: 9861251
geoff,

can you get your method to read text from an IE TextArea control?  For example, the control that I am typing this message into?
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 100 total points
ID: 9861681

Won't work, because they are not "true" window controls (they don't have an hwnd). Without getting into the document object model for IE (IHTMLDocument2), you are probably going to be hard pressed to come up with a generic method that works in all cases.

Regards,
Russell
0
 
LVL 8

Expert Comment

by:gmayo
ID: 9861759
You're right. That's the first time I've come across a program in which that didn't work. It does work in the address bar though.

Geoff M.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:roknjohn
ID: 9861899
I found this:

Peeking into Password Edit Controls & Internet Explorer - Super Password Spy++
http://www.developer.com/net/cplus/article.php/1561741

It seems to focus on password fields, but I think I can extract the information I need from this.  Now, if I can only find the time. :-)
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 9861926

Yes, saw that one two....
But like i said, you need to delve into the document interface in order to get this information. This will work fine if you only intend on dealing with IE though... (won't work with anything else ;-)

Regards,
Russell
0
 
LVL 1

Author Comment

by:roknjohn
ID: 9861975
Hmm, looks more complicated than its worth right now.

My plan was to put together a freeware spell checker that would work across applications.  But, I think I'll just google for "Spell Checker" instead of "IHTMLDocument2  delphi"...lol

Every heard of "As-U-Type"?  
http://www.asutype.com
0
 
LVL 1

Author Comment

by:roknjohn
ID: 9862150
I think I found a real gem! The trail version works cool as heck!  It works on IE & Outlook, just like I wanted to do.  Only $29.95 (US) and I don't have to learn IHTMLDocument2!!  

PS. You can disable it for certain apps, like Delphi.  

Thanks Russell, your information solved my curousity.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi with SQL Natvie Client 15 86
Thread safe  opinion 7 126
Delphi component that can load a DLL in design time? 8 53
Downloading email attachments 2 67
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

14 Experts available now in Live!

Get 1:1 Help Now