How to loop through all of the windows.

Posted on 2004-12-01
Last Modified: 2010-04-05

I want to find the window which is named: #32770.
This is the ICQ message dialog.

Ofcourse there are more #32770 windows since it's a general name for a dialog,
so that's why I want to loop through all #32770 then get the pid from the handle and then check if the exe file is icq.exe or icqlite.exe.

I know how to get the pid from the handle, so all I need is that you could provide me a code that loops through all windows with the name #32770.

BTW, Trying to find window titles with the name Message Session, is not an option since it differs with different languages.
Question by:TonyJiz
    LVL 17

    Accepted Solution


    Drop a TListBox and a TButton on a form and use the code below. Pressing the button will fill the listbox with HWND and title of all top-level windows which class name is #32770.

      TForm1 = class(TForm)
        Button1: TButton;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
        { Private declarations }
        { Public declarations }

      Form1: TForm1;


    {$R *.DFM}

      WindowsList: TStrings;

    procedure WindowInfo(hWnd: HWND; List: TStrings);
      ErrorCode: Integer;
      TheText: array[0..255] of char;
      TheClassName: array[0..255] of char;
      ErrorCode := GetClassName(hWnd, TheClassName, 255);
      if (ErrorCode <> 0) and (TheClassName = '#32770') then begin
        // do something with the window using its handle 'hWnd'. For instance, get the window title and fill a TStrings
        GetWindowText(hWnd, TheText, 255);
        if (StrLen(TheText) = 0) then TheText := '[Window without title]';

    function EnumerateWindows(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;
      Result := TRUE;
      WindowInfo(hWnd, WindowsList);

    procedure TForm1.Button1Click(Sender: TObject);
      WindowsList := ListBox1.Items;
      EnumWindows(@EnumerateWindows, 0);


    Regards, Geo
    LVL 17

    Expert Comment

    by:Wim ten Brink
    At you can find a sample application that enumerates child windows too, in case you need an example. ;-)

    But Geo's code should do the trick, though.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
    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…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now