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
marusakiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
if you have the window,
you can use the enumChildWindows-API
to get the handles of the controls

then u could use the wm_settext message to fillin the edits

for pressing ok you can send a enter-key via keybd_event (dialog must have the focos)
or you can send a wm_lbuttondown-message followed by a wm_lbuttonup to
the ok button using its handle

so far the theory
(could be that windows blocks messages
for password-dialogs, because it would be a security-leak,
don't know yet)

if i got time, then a sample may follow

meikl ;-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GloomyFriarCommented:
Here is a part from my working code:

var
  Form1: TForm1;
  g_w_handle: HWND;
  g_w_cap: string;
  g_stage: longint;
  g_str: array[0..2048] of char;


function EnumChildsFunc(wnd: HWND; lParam: longint): longint; stdcall;
var Res: integer;
    str: array[0..1024] of char;
begin
  Res := GetWindowText(wnd, g_str, 2048);

  case g_stage of
    1: begin
      if g_str[0] = #0 then begin
        if StrComp(@g_str[1], 'First Name:') = 0 then begin
          StrPCopy(str, 'alex');
          SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
        end else if StrComp(@g_str[1], 'Last Name:') = 0 then begin
          StrPCopy(str, 'm');
          SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
        end else if StrComp(@g_str[1], 'Gender:') = 0 then begin
          SendMessage(wnd, CB_SETCURSEL, 1, 0);
        end else if StrComp(@g_str[1], 'Zip / Postal Code:') = 0 then begin
          StrPCopy(str, '22222');
          SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
        end else if StrComp(@g_str[1], 'Occupation:') = 0 then begin
          SendMessage(wnd, CB_SETCURSEL, 2, 0);
        end;
      end else if ((StrComp(@g_str[0], '&Contact me from time to time about specials and new products') = 0) or
                   (StrComp(@g_str[0], '„Ÿ„‚„„‡„‡ >') = 0)) then begin
          SendMessage(wnd, BM_CLICK, 0, 0);
      end;
    end;
    2: begin
      if g_str[0] = #0 then begin
        if StrComp(@g_str[1], 'reate My Own') = 0 then begin
          StrPCopy(str, Form1.Edit1.Text);
          SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
        end else if StrComp(@g_str[1], 'e&type Password:') = 0 then begin
          StrPCopy(str, '123123');
          SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
        end else if StrComp(@g_str[1], 'Password:') = 0 then begin
          StrPCopy(str, '123123');
          SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
        end;
      end else if ((StrComp(@g_str[0], 'Create My Own') = 0) or
                   (StrComp(@g_str[0], '&Remember my ID and Password') = 0) or
                   (StrComp(@g_str[0], '„Ÿ„‚„„‡„‡ >') = 0)) then begin
          SendMessage(wnd, BM_CLICK, 0, 0);
      end;
    end;
    3: begin
      if g_str[0] = #0 then begin
        if StrComp(@g_str[1], 'Question we''ll ask:') = 0 then begin
          SendMessage(wnd, CB_SETCURSEL, 2, 0);
        end else if StrComp(@g_str[1], 'our &answer:') = 0 then begin
          StrPCopy(str, '1234567890');
          SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
        end else if StrComp(@g_str[1], 'our &birthday:') = 0 then begin
          StrPCopy(str, '01/01/1975');
          SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
        end else if StrComp(@g_str[1], 'urrent &Email Address:') = 0 then begin
          StrPCopy(str, 'alexn911@hotmail.com');
          SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
        end;
      end else if (StrComp(@g_str[0], '„Ÿ„‚„„‡„‡ >') = 0) then begin
          SendMessage(wnd, BM_CLICK, 0, 0);
      end;
    end;
  end;
end;



procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  if FindWindowByCap('Window caption') <> HWND(-1) then begin
     g_stage := 1;
    FillMemory(@g_str[0], 2048, BYTE('#'));
    EnumChildWindows(g_w_handle, @EnumChildsFunc, 0);
  end;
end;


function FindWindowByCap(text: string): HWND;
begin
  g_w_handle := HWND(-1);
  g_w_cap := text;
  EnumWindows(@EnumFunc, 0);
  Result := g_w_handle;
end;


function EnumFunc(wnd: HWND; lParam: longint): longint; stdcall;
var str: array[0..2048] of char;
    Res: integer;
begin
   Res := GetWindowText(wnd, str, 2048);
   if ((Res > 0) and
       (CompareStr(g_w_cap, StrPas(str)) = 0)) then begin
     g_w_handle := wnd;
     Result := 0;
   end else begin
     Result := 1;
   end;
end;

0
JDuncanCommented:
listening
0
marusakiAuthor Commented:
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...
0
marusakiAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

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.