Guest User

GetWindowText / WM_GETTEXT test

a guest
Aug 12th, 2016
110
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unit UnitMain;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, Spin;
  8.  
  9. type
  10.   TfmMain = class(TForm)
  11.     lbThisWndHandle: TLabel;
  12.     btSpawnAnotherInstance: TButton;
  13.     btSetCaptionForThisWnd: TButton;
  14.     edCaption: TEdit;
  15.     btGetWindowText: TButton;
  16.     btHangThisApp: TButton;
  17.     btGetText: TButton;
  18.     seTargetWndHandle: TSpinEdit;
  19.     cbOverloadWMGetText: TCheckBox;
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure btSpawnAnotherInstanceClick(Sender: TObject);
  22.     procedure btSetCaptionForThisWndClick(Sender: TObject);
  23.     procedure btGetWindowTextClick(Sender: TObject);
  24.     procedure btHangThisAppClick(Sender: TObject);
  25.     procedure btGetTextClick(Sender: TObject);
  26.   public
  27.     procedure DefaultHandler(var Message); override;
  28.   end;
  29.  
  30. var
  31.   fmMain: TfmMain;
  32.  
  33. implementation
  34.  
  35. uses
  36.   ShellAPI;
  37.  
  38. {$R *.dfm}
  39.  
  40. const
  41.   OverrideCaption = '!override!';
  42.   CaptionLen      = Length(OverrideCaption);
  43.  
  44. procedure TfmMain.FormCreate(Sender: TObject);
  45. const
  46.   Let: String = '0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm';
  47. var
  48.   S: String;
  49.   X: Integer;
  50. begin
  51.   lbThisWndHandle.Caption := Format('Handle: %d', [Handle]);
  52.  
  53.   seTargetWndHandle.Value := Integer(Handle);
  54.  
  55.   SetLength(S, CaptionLen);
  56.   Randomize;
  57.   for X := 1 to Length(S) do
  58.     S[X] := Let[Random(Length(Let))];
  59.  
  60.   edCaption.Text := S;
  61.   btSetCaptionForThisWndClick(nil);
  62. end;
  63.  
  64. procedure TfmMain.btSpawnAnotherInstanceClick(Sender: TObject);
  65. begin
  66.   ShellExecute(Handle, nil, PChar(ParamStr(0)), nil, nil, SW_NORMAL);
  67. end;
  68.  
  69. procedure TfmMain.btHangThisAppClick(Sender: TObject);
  70. begin
  71.   Sleep(60000);
  72. end;
  73.  
  74. procedure TfmMain.btSetCaptionForThisWndClick(Sender: TObject);
  75. begin
  76.   edCaption.Text := Copy(edCaption.Text, 1, CaptionLen);
  77.   SetWindowText(Handle, PChar(edCaption.Text));
  78. end;
  79.  
  80. procedure TfmMain.btGetWindowTextClick(Sender: TObject);
  81. var
  82.   S: String;
  83.   T: DWORD;
  84. begin
  85.   SetLength(S, CaptionLen + 1);
  86.   FillChar(Pointer(S)^, Length(S), 0);
  87.   T := GetTickCount;
  88.   GetWindowText(THandle(seTargetWndHandle.Value), PChar(S), Length(S));
  89.   SetLength(S, StrLen(PChar(S)));
  90.   T := GetTickCount - T;
  91.  
  92.   if T > 3000 then
  93.     ShowMessage(Format('TIMEOUT!' + sLineBreak + 'Caption: "%s"', [S]))
  94.   else
  95.     ShowMessage(Format('OK!' + sLineBreak + 'Caption: "%s"', [S]));
  96. end;
  97.  
  98. procedure TfmMain.btGetTextClick(Sender: TObject);
  99. var
  100.   S: String;
  101.   T: DWORD;
  102. begin
  103.   SetLength(S, CaptionLen + 1);
  104.   FillChar(Pointer(S)^, Length(S), 0);
  105.   T := GetTickCount;
  106.   SendMessage(THandle(seTargetWndHandle.Value), WM_GETTEXT, Length(S), LPARAM(PChar(S)));
  107.   SetLength(S, StrLen(PChar(S)));
  108.   T := GetTickCount - T;
  109.  
  110.   if T > 3000 then
  111.     ShowMessage(Format('TIMEOUT!' + sLineBreak + 'Caption: "%s"', [S]))
  112.   else
  113.     ShowMessage(Format('OK!' + sLineBreak + 'Caption: "%s"', [S]));
  114. end;
  115.  
  116. procedure TfmMain.DefaultHandler(var Message);
  117. begin
  118.   if (
  119.        (TMessage(Message).Msg <> WM_GETTEXT) and
  120.        (TMessage(Message).Msg <> WM_GETTEXTLENGTH)
  121.      ) or
  122.      (cbOverloadWMGetText = nil) or
  123.      (not cbOverloadWMGetText.Checked) then
  124.   begin
  125.     inherited DefaultHandler(Message);
  126.     Exit;
  127.   end;
  128.  
  129.   if TMessage(Message).Msg = WM_GETTEXT then
  130.   begin
  131.     StrLCopy(PChar(TMessage(Message).lParam), OverrideCaption, TMessage(Message).wParam);
  132.     TMessage(Message).Result := Length(OverrideCaption);
  133.     if TMessage(Message).Result > TMessage(Message).wParam then
  134.       TMessage(Message).Result := TMessage(Message).wParam;
  135.   end
  136.   else
  137.   // TMessage(Message).Msg = WM_GETTEXTLENGTH
  138.   begin
  139.     TMessage(Message).Result := Length(OverrideCaption);
  140.   end;
  141. end;
  142.  
  143. end.
RAW Paste Data