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

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

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

I tryed
     WinHandle: THandle;
     Buffer: array[0..50] of Char;
     GetClassName(WinHandle, Buffer, SizeOf(Buffer) - 1);
     GetWindowText(WinHandle, Buffer, SizeOf(Buffer) - 1);

but getwindowtext cannot get the text of another app.
Thanks to submit working code please. Thanks.
Who is Participating?
MadshiConnect With a Mentor Commented:
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;

Hi John!!   :-)

This should work:

var i1 : integer;
    s1 : string;
  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.
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 ?
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

jeurkAuthor Commented:
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.

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)...   :-)
jeurkAuthor Commented:
Anyway I believe you.
Look I have another question
(just give me five minutes to type it;)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.