[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get window title from a process

Posted on 2009-02-16
9
Medium Priority
?
3,363 Views
Last Modified: 2012-05-06
I would get the title of the window of the iexplore.exe process.
Important: it is not the active window, you get directly from the process!

any solution ?
0
Comment
Question by:davizinx
8 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23653902
use process explorer from www.sysinternals.com
0
 
LVL 13

Expert Comment

by:ThievingSix
ID: 23655009
Once you had the PID of the process you would enumerate all the windows and match it.

Something like this:


type
  PProcessID = ^TProcessID;
  TProcessID = record
    PID : DWORD;
    Title : String[255];
  end;
 
function GetWindowNameFromPID(PID: DWORD): String;
var
  ProcessID : PProcessID;
begin
  GetMem(ProcessID,SizeOf(TProcessID));
  ProcessID^.PID := PID;
  If Not EnumWindows(@EnumProcess,Pointer(ProcessID)) Then
    begin
    If MessageDlg('Could not get hook to the process handles.' + #13#10 + 'Try Again?',mtConfirmation,[mbYes,mbNo],0) = mrYes Then
      begin
      GetWindowNameFromPID(PID);
    end;
  end;
end;
 
function EnumProcess(Handle: HWND; lParam: Integer): BOOL; stdcall;
var
  PID : Cardinal;
  ProcessID : TProcessID;
  Title : String;
begin
  If Handle = NULL Then
    begin
    Result := False;
  end
  Else
    begin
    ProcessID := PProcessID(Pointer(lParam))^;
    GetWindowThreadProcessID(Handle,PID);
    If ProcessID.PID = lParam Then
      begin
      SetLength(Title,255);
      SetLength(Title,GetWindowText(Handle,PChar(Title),Length(Title)));
      If Title = '' Then
        begin
        Title := 'Empty';
      end;
      ProcessID.Title := Title;
    end;
    Result := True;
  end;
end;

Open in new window

0
 
LVL 7

Expert Comment

by:ahalya
ID: 23655248
A process can have several windows.  You would like to get a list of all the captions, I presume.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 13

Expert Comment

by:ThievingSix
ID: 23655255
^ This is true. The code I posted will get the last window title. It can be modified to shove them in an array.
0
 

Author Comment

by:davizinx
ID: 23657745


[Error] Unit1.pas(64): Incompatible types: 'Integer' and 'Pointer'

for

If Not EnumWindows(@EnumProcess,Pointer(ProcessID)) Then
0
 
LVL 13

Accepted Solution

by:
ThievingSix earned 1500 total points
ID: 23658025
Ah, change it to:

If Not EnumWindows(@EnumProcess,Integer(Pointer(ProcessID))) Then
0
 

Author Comment

by:davizinx
ID: 23665166
ThievingSix:

Thanks for reply, i try this code:

Label1.caption:= GetWindowNameFromPID(3688); //3688 = firefox

and i try with anothers process and dont work.
the label is blank, do not show the window name.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 24290815
please post your entire routine in which the error occurs.

I think it might be easier to insert into a TStringList variable than an array.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question