[D3 winAPI] Get the text from another's app memo

Hello,
I'd like to read the contain of the memo
that is in another app.
Using windows messages.
I already have the handle of that
memo.

Let's say I have a memo. Help me read
another memo and fill mine with it's
text.

I tryed
  var
     p:TPoint;
     WinHandle: THandle;
     Buffer: array[0..50] of Char;
begin
     ReleaseCapture;
     GetCursorPos(p);
     WinHandle:=WindowFromPoint(p);
     GetClassName(WinHandle, Buffer, SizeOf(Buffer) - 1);
     edit1.Text:=Buffer;
     GetWindowText(WinHandle, Buffer, SizeOf(Buffer) - 1);
     edit2.Text:=Buffer;

but getwindowtext cannot get the text of another app.
Thanks to submit working code please. Thanks.
LVL 4
jeurkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MadshiCommented:
Hi John!!   :-)

This should work:

var i1 : integer;
    s1 : string;
begin
  i1 := SendMessage(WinHandle, WM_GETTEXTLENGTH, 0, 0);
  SetLength(s1, i1 + 1);
  SetLength(s1, SendMessage(WinHandle, WM_GETTEXT, i1 + 1, integer(s1)));
  memo1.text := s1;

Regards, Madshi.
0
jeurkAuthor Commented:
this is working perfectly.
but I do not understand what the
line SetLength(s1, SendMessage(WinHandle, WM_GETTEXT, i1 + 1, integer(s1)));
does and how it is working.
does not setlenght change the size of the given string ?
0
MadshiCommented:
Yes...   :-)

  // get the length of the window text and store it in "i1"
  // according to the documentation sometimes this returns a bigger length than nessecary...
  i1 := SendMessage(WinHandle, WM_GETTEXTLENGTH, 0, 0);

  // allocate memory for the window text
  SetLength(s1, i1 + 1);
 
  // get the window text
  // but the string length is too long still
  // but the result of the SendMessage call gives us the *exact* length
  // so we set the string to this exact length
  SetLength(s1, SendMessage(WinHandle, WM_GETTEXT, i1 + 1, integer(s1)));
 
Instead of the last line we could have also do this:

  // get the window text
  SendMessage(WinHandle, WM_GETTEXT, i1 + 1, integer(s1));
 
  // let Delphi cut the string at the first #0 character
  s1 := pchar(s1);

Regards, Madshi.

P.S: With the stuff you'll soon get from me you'll be able to do this:

  memo1.text := Window(WinHandle).Text;

:-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

jeurkAuthor Commented:
Hello,
thanks for your help.
and for the crystal clear comments.

About memo1.text := Window(WinHandle).Text;

are your doing more then GetWindowText(WinHandle, Buffer, SizeOf(Buffer) - 1);
?
I hope so. Anyway I'm gonna like your stuff life always.

CU
0
MadshiCommented:
I don't use GetWindowText, because it doesn't work alright. In my stuff I'm doing exactly what I've posted here (in fact the code I've posted here is just a slightly changed copy from my stuff)...   :-)
0
jeurkAuthor Commented:
Anyway I believe you.
Look I have another question
(just give me five minutes to type it;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.