Link to home
Start Free TrialLog in
Avatar of marusaki
marusaki

asked on

Filling external Edit fields with text

Hi everyone!

"Lazyness is the mother of 9 out of 10 inventions"...
A friend of mine ;-) has his internet access blocked by a firewall that from time to time asks for a username and a password. Since he is a very good friend of mine I would like to help him by making a program that continuosly scans the desktop windows and when it finds the login window fills automatically the two edit fields and then simulates pressing the OK button.
The window has "Network password" as caption. The window class it's a misterious name: #32770. The two edits have the class names "Edit". Apart from these two edits there are also 2 buttons (OK+Cancel) and a check box ("Remember username and password". It doesn't remember...)
So far I've been able to find the window's handle and the classes of the two edits (for the latter I used an external program). How can I find the handle of the two edits? How can I then press the OK button?

Thanks
Mao
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
listening
Avatar of marusaki
marusaki

ASKER

I got the solution. On of the many I think may olve this problem...
I put a Timer on my fom that will scan every 200ms the desktop to find the password window.


function FindControlByNumber(hApp: HWND; ControlClassName: string; EditNr: Integer): HWND;
var
  i: Word;
  hEdit: HWND;
begin
  Result := 0;
  if not IsWindow(hApp) then Exit;
  for i := 0 to EditNr do
  begin
    hEdit := FindWindowEx(hApp, hEdit, PChar(ControlClassName), nil);
    if hEdit = 0 then Continue;
  end;
  Result := hEdit;
end;

function FindWindowByTitle(WindowTitle: string): Hwnd;
var
  NextHandle: Hwnd;
  NextTitle: array[0..260] of char;
begin
  NextHandle := GetWindow(Application.Handle, GW_HWNDFIRST);
  while NextHandle > 0 do
  begin
    GetWindowText(NextHandle, NextTitle, 255);
    if Pos(WindowTitle, StrPas(NextTitle)) <> 0 then
    begin
      Result := NextHandle;
      Exit;
    end
    else
      NextHandle := GetWindow(NextHandle, GW_HWNDNEXT);
  end;
  Result := 0;
end;

procedure TfrmMain.tmrAuthTimer(Sender: TObject);
var
  Hand: HWND;
  EdtHand: HWND;
  BtnHand:HWND;
begin
  Hand := FindWindowByTitle('Password di rete');
  if Hand > 0 then
  begin
    tmrTimer.Enabled := False;

    // find the first Edit
    EdtHand := FindControlByNumber(Hand,'Edit',1);
    SendMessage(EdtHand, WM_SETTEXT, 0, Integer(PChar('myuser')));

    // find the second Edit
    EdtHand := FindControlByNumber(Hand,'Edit',2);
    SendMessage(EdtHand, WM_SETTEXT, 0, Integer(PChar('mypass')));

   // find the OK button (the checkbox has also the ClassName 'Button' so i skip it...)
    BtnHand := FindControlByNumber(Hand,'Button',2);
    SendMessage(BtnHand, WM_LBUTTONDOWN, 0, 0);  // simulate pressing the button
    SendMessage(BtnHand, WM_LBUTTONUP, 0, 0);

    tmrTimer.Enabled := True;
  end;
end;

It works like a dream! Now I'm working on adding a TWebBrowser and a second timer that every 10 minutes will try to open a link thus making the authentication window to pop up even if I'm not navigating in Internet Explorer. In this way I will have a pseudo-permanently open internet connection. Well, my friend will...
I forgot to post my source of inspiration
  http://www.swissdelphicenter.com/en/showcode.php?id=327

Anyway, thanks meikl and GloomyFriar! I got some good tips from you.