Getwindowtext does not retrieve text
I've tried the following code, but it doesn't fetch the text from the foreground window!
procedure TForm1.Button1Click(Sender: TObject);
var
title : pansichar;
s : string;
begin
GetWindowText(GetForegroundWindow(), title,GetWindowTextLength(GetForegroundWindow()) + 1);
s := title;
showmessage(s);
end;
+2
source to share
5 answers
Use this:
var
hwndForeground: HWND;
titleLength: Integer;
title: string;
begin
hwndForeground := GetForegroundWindow();
titleLength := GetWindowTextLength(hwndForeground);
SetLength(title, titleLength);
GetWindowText(hwndForeground, PChar(title), titleLength + 1);
title := PChar(title);
ShowMessage(title);
end;
+9
source to share
procedure TForm1.Button1Click (Sender: TObject); var liHwnd, liLength: Integer; lpChar: PChar; begin liHwnd: = GetForegroundWindow (); liLength: = GetWindowTextLength (liHwnd) + 1; lpChar: = StrAlloc (liLength); Try GetWindowText (liHwnd, lpChar, liLength); showmessage (lpChar); Finally StrDispose (lpChar); End; end;
+1
source to share