Solved

Active Task List

Posted on 1998-05-12
5
154 Views
Last Modified: 2010-04-06
How do I retrieve a list of all running tasks within Windows (like the task manager?).

Stu
0
Comment
Question by:Stuart_Johnson
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
inter earned 80 total points
ID: 1345115
Hi,
This is from one of my previous anwers:

    The following enumaretes more than you want, you can restrict or losen the criteria to display the  memory contents on even window basis. Just change the IF statement in the EnumWindowsProc. To   compile , create a new unit, add A button, A ListBox, assign OnClick event of button to the following   code(Button1Click) then run.

    var
      S : TStringList;

    function EnumWindowsProc(hwnd : HWND ;lParam : longint):boolean;stdcall;
    var
      wn : array[0..255] of char;
      pid: THandle;
      w : string;
    begin
      if hwnd <> 0 then
      begin
        GetWindowThreadProcessId(hwnd, @pid);
        // filter out if this is our application window
        // and all child windows
        if (pid <> GetCurrentProcessId) and (GetParent(hwnd)= 0) then
        begin
          // get the caption
          // or use the commented IF to get application names
          if GetWindowText(hwnd, wn,sizeof(wn)) <> 0 then
    //    if GetModuleFileName(GetWindowLong(hwnd,GWL_HINSTANCE),wn,sizeof(wn)) <> 0 then
          begin{}
            w:= Format('%6d ',[pid])+StrPas(wn);
            S.Add(w);
          end;{}
       end;
        result := true;
      end else result := false;
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      try
       S := TStringList.Create;
       S.Sorted := true;
       S.Duplicates := dupIgnore;
       ListBox1.Items.Clear;
       S.Clear;
       EnumWindows(@EnumWindowsProc,0);
       ListBox1.Items.AddStrings(S);
      finally
        S.Free;
      end;
    end;

    Bye
    Igor
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 1345116
inter:

Thanks for the post, however when we tried it we continually got protection errors.  

I had to change the StringList to a TStrings type.  That fixed the protection fault, but there is a problem when we are converting the WN array to the string.  Although the array contains information, it is not being converted to a string.  It is as though there is nothing in the array.  Wierd, eh?

Any suggestions?

Stuart.
0
 
LVL 5

Expert Comment

by:inter
ID: 1345117
Hi, sorry but the code works without modification. Just add a list box and a button to an empty form, copy and paste the code after implenetation clause and run. I am using Delphi 2.0. If you want I can send you the whole project and/or exe so that we can see if there is something with your windows.
About WN: (Examine my explanation below)

if GetWindowText(hwnd, wn,sizeof(wn)) <> 0 then //If windows has caption proceed
begin
    w:= Format('%6d ',[pid])+StrPas(wn); //StrPas routine converts the null terminated.
    S.Add...//add it to the list
end;
Friend, if this simple code does not works this should be do to some other thing which I can not understand by now.
Regards, Igor
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 1345118
Igor,

I humbly appologise for that.  I just took the word of someone else (granted I over looked it too).  Seems we made the mistake.


Thank you for your help!


Stuart.
0
 
LVL 5

Expert Comment

by:inter
ID: 1345119
You are wellcome, thanks!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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