Recent

Author Topic: How to use GetWindowText() correctly?  (Read 532 times)

was

  • Newbie
  • Posts: 5
How to use GetWindowText() correctly?
« on: June 27, 2022, 12:56:52 pm »
Hello, I use GetWindowText() to get Application's title, but Non English characters cannot be displayed.



paweld

  • Hero Member
  • *****
  • Posts: 991
Re: How to use GetWindowText() correctly?
« Reply #1 on: June 27, 2022, 01:17:43 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   szText: WideString;
  4. begin
  5.   Edit1.Text := Caption;
  6.  
  7.   if GetWindowTextW(GetForegroundWindow, PWideChar(szText), GetWindowTextLengthW(GetForegroundWindow)) > 0 then
  8.     Edit2.Text := szText;
  9. end;
Best regards / Pozdrawiam
paweld

PascalDragon

  • Hero Member
  • *****
  • Posts: 5462
  • Compiler Developer
Re: How to use GetWindowText() correctly?
« Reply #2 on: June 27, 2022, 01:20:58 pm »
@paweld: you should not mix PWideChar and WideString like this, cause GetWindowTextW won't allocate the buffer for you. So you should do (not tested):

Code: Pascal  [Select][+][-]
  1. SetLength(szText, GetWindowTextLengthW(GetForegroundWindow));
  2. if GetWindowTextW(GetForegroundWindow, PWideChar(szText), Length(szText)) > 0 then
« Last Edit: June 28, 2022, 01:30:09 pm by PascalDragon »

paweld

  • Hero Member
  • *****
  • Posts: 991
Re: How to use GetWindowText() correctly?
« Reply #3 on: June 27, 2022, 02:10:03 pm »
@PascalDragon: thank you very much for the explanation
Best regards / Pozdrawiam
paweld

was

  • Newbie
  • Posts: 5
Re: How to use GetWindowText() correctly?
« Reply #4 on: June 27, 2022, 03:27:42 pm »
@paweld, @PascalDragon: thank you very much.

 

TinyPortal © 2005-2018