Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Listview and their associated Icons .

Posted on 2003-11-03
2
Medium Priority
?
338 Views
Last Modified: 2010-04-05
Hello Experts.

 Please help me with this code:(tip on Swissdelphicenter )

{Enumerate processes and terminate them }

interface  

uses  
 {...,}TLHelp32 {important !}  

// Global Variables, Globale Variablen  

var  
  aSnapshotHandle: THandle;  
  aProcessEntry32: TProcessEntry32;  
   
implementation  

procedure TForm1.BtnRefreshClick(Sender: TObject);  
var  
  i: Integer;  
  bContinue: BOOL;  
  NewItem: TListItem;  
begin  
  ListView1.Items.Clear;  
  aSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  
  aProcessEntry32.dwSize := SizeOf(aProcessEntry32);  
  bContinue := Process32First(aSnapshotHandle, aProcessEntry32);  
  while Integer(bContinue) <> 0 do  
  begin  
    NewItem := ListView1.Items.Add;  
    NewItem.Caption := ExtractFileName(aProcessEntry32.szExeFile);  
    NewItem.subItems.Add(IntToHex(aProcessEntry32.th32ProcessID, 4));  
    NewItem.subItems.Add(aProcessEntry32.szExeFile);  
    bContinue := Process32Next(aSnapshotHandle, aProcessEntry32);  
  end;  
  CloseHandle(aSnapshotHandle);  
end;  


procedure TForm1.ListView1DblClick(Sender: TObject);  
var  
  Ret: BOOL;  
  PrID: Integer; // processidentifier  
  Ph: THandle;   // processhandle  
begin  
  with ListView1 do  
  begin  
    if MessageDlg('Do you want to Terminate "' + ItemFocused.Caption + '"?' + ^J +  
                  'It''s possible the system becames instable or out of' + ^J +  
                  'control......',  
        mtConfirmation, [mbYes, mbNo], 0) = mrYes then  
     begin  
       PrID := StrToInt('$' + ItemFocused.SubItems[0]);  
       Ph := OpenProcess(1, BOOL(0), PrID);  
       Ret := TerminateProcess(Ph, 0);  
       if Integer(Ret) = 0 then  
         MessageDlg('Cannot terminate "' + ItemFocused.Caption + '"',  
                     mtInformation, [mbOK], 0)  
       else  
         ItemFocused.Delete;  
     end;  
   end;  
end;  


procedure TForm1.FormCreate(Sender: TObject);  
begin  
  {  
    ListView1.Columns.Add;  
    ListView1.Columns.Add;  
    ListView1.Columns.Add;  
    ListView1.ViewStyle := vsReport;  
  }  
  BtnRefresh.Click;  
end;  


my question: How extract associated Icons of processes then show in Listview1.

Thank you.
0
Comment
Question by:z27
2 Comments
 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 320 total points
ID: 9670438
you can extract icons from the exefilename, but to do this you need also the full path (you know that aProcessEntry32.szExeFile returns just the filename without the full path).
But don't worry, here's a solution:
Firts put on your for a TImageList (that will contain the extracted icons)...and link it to the TListView (small, large and state)

then add to uses the ShellApi unit (needed to use SHGetFileInfo).

Then replace the btnrefresh.onclick code with this;

procedure TForm1.BtnRefreshClick(Sender: TObject);
var  
  i: Integer;  
  bContinue: BOOL;  
  NewItem: TListItem;
  Icon: TIcon;
  FileInfo: SHFILEINFO;
  c : cardinal;
  ME : TModuleEntry32;
begin
  imagelist1.Clear;      
  ListView1.Items.Clear;
  aSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  
  aProcessEntry32.dwSize := SizeOf(aProcessEntry32);  
  bContinue := Process32First(aSnapshotHandle, aProcessEntry32);  
  while Integer(bContinue) <> 0 do  
  begin
   NewItem := ListView1.Items.Add;
        Icon := TIcon.Create;
        try
    // Get the Module for this ProcessID
       c := CreateToolhelp32Snapshot(TH32CS_SnapModule,
                                                                    aProcessEntry32.th32ProcessID);
               me.dwSize := sizeof(me);
               Module32First(c, me);
       //now extract the icon from the file    
           SHGetFileInfo(me.szExePath, 0, FileInfo, SizeOf(FileInfo), SHGFI_ICON);
           CloseHandle(c);
           icon.Handle := FileInfo.hIcon;
       //and add it to the image list
           newitem.ImageIndex := imagelist1.AddIcon(icon);
    finally
           icon.Free;
    end;
    NewItem.Caption := ExtractFileName(aProcessEntry32.szExeFile);  
    NewItem.subItems.Add(IntToHex(aProcessEntry32.th32ProcessID, 4));  
    NewItem.subItems.Add(aProcessEntry32.szExeFile);  
    bContinue := Process32Next(aSnapshotHandle, aProcessEntry32);
  end;  
  CloseHandle(aSnapshotHandle);  
end;

F68 ;-)
0
 

Author Comment

by:z27
ID: 9698104
OK ,It works well...
  Thank you very much.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
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…
Suggested Courses

772 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