find window handle

Hi!

Imagine I have 2 applications opened, and they were both made using delphi!

their classes are the same (TForm).
The first one has no title.
The second has the title : "NoName - ApplicationName"
Note that the NoName can be changed.

How can I find the handle of the second one, knowing only that it has the string "ApplicationName"?


Hope that my english was clear enough!


Thanks

Joao
LVL 1
jpcsAsked:
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.

MadshiCommented:
Use this one:

FindWindow('TForm', 'NoName - ApplicationName');

Or, if "NoName" can change, enumerate all windows by using "EnumWindows".

Or, if you have D4 or D5, you can download the free unit "enumStuff.pas" from my homepage

http://beam.to/madshi

Then you can use the function GetWindowList. It's a bit easier to use than EnumWindows.

Regards, Madshi.
0
hubdogCommented:
var
  neededhandle:THandle;
Function WindowCallback(WHandle : HWnd; Var Parm : Pointer) : Boolean; stdcall;
{This function is called once for each window}
Var winString : PChar;
begin
    winString := Allocmem(255);
    GetWindowText(WHandle,winString,255);
    if AnsiStrPos(winstring,'ApplicationName')<>nil then
  begin
    neededhandle:=whandle;//get the window you needed
    result:=false;//stop enumerate
  end
  else
    Result := True; {not find the window. Continue to enumerate windows}
  FreeMem(winString,255);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  EnumWindows(@WindowCallback,0);
end;

good luck

hubdog
0
intheCommented:
listening
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

jpcsAuthor Commented:
Thanks people.

I've made a simple routine that checks it :

function comparestring (str1, str2 : string) : boolean;
var temp, maior, menor : string;
    i : integer;
begin
   if comparetext (str1, str2) = 0 then
   begin
      comparestring := true;
      exit;
   end;

   if length (str1) > length (str2) then
   begin
      maior := str1;
      menor := str2;
   end
   else
   begin
      maior := str2;
      menor := str1;
   end;
   if ( (maior<> '') AND (menor <> '') ) then
      for i := 0 to length (maior)-length (menor) do
      begin
         temp := maior;
         delete (temp, 1, i);
         setlength (temp, length (menor));

         if comparetext (temp, menor) = 0 then
         begin
            comparestring := true;
            exit;
         end;
      end;
   comparestring := false;
end;


I'm sorry, but I thought that there was some function that made it easier.

Thanks Madshi! Once again, i'm using your great unit. Nice Work.


Thanks people.


Joso
0
intheCommented:
you can alos do:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  capt : Array[0..99] of Char;
  hh : THandle;
implementation

{$R *.DFM}

Function TCallBackChildren(h : THandle;L : Longint):longBool;stdcall;
begin
  GetwindowText(h,@capt[0],100);
  if Pos('ApplicationName',StrPas(@capt[0])) <> 0 then
    hh := h;
  Result := True;
end;

Function TCallBackParent(H : THandle;L : Longint):longBool;stdcall;
begin
  GetwindowText(h,@capt[0],100);
  if Pos('ApplicationName',StrPas(@capt[0])) <> 0 then
  begin
    hh := h;
  end
  else
    EnumChildWindows(h,@TCallBackChildren,L);
  Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
var LL : Longint;
begin
  hh := 0;
  EnumWindows(@TCallBackParent,LL);
  if hh <> 0 then
  begin
Showmessage('yes we found second app');
end
else showmessage('nope second app not found');
  end;

end.


Regards Barry
0
MadshiCommented:
So I guess I can post an answer?   :-)
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
jpcsAuthor Commented:
yes!


Keep on the goo work on the unit.
0
MadshiCommented:
Well, I have already worked a lot on that unit (and other stuff). Watch my homepage - somewhen in the next weeks there will come a lot of new stuff. But probably I will only make the dcus available for free.
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.