Recent

Author Topic: Pull an icon from a running process  (Read 462 times)

BIT

  • Full Member
  • ***
  • Posts: 143
Pull an icon from a running process
« on: October 03, 2023, 11:52:11 am »
Hello, I need your help; I can’t solve this problem myself.
Code: Pascal  [Select][+][-]
  1. procedure TForm2.ListProcesses(TreeView: TTreeView);
  2. var
  3.   Proc: TPROCESSENTRY32;
  4.   hSnap: HWND;
  5.   Looper: BOOL;
  6.   vNode: TTreeNode;
  7.   Ico: TIcon;
  8.  
  9. begin
  10.   TreeView.Items.Clear;
  11.   Proc.dwSize := SizeOf(Proc);
  12.   hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  13.   Ico := TIcon.Create;
  14.   try
  15.  
  16.     Looper := Process32First(hSnap, Proc);
  17.  
  18.     while integer(Looper) <> 0 do
  19.     begin
  20.  
  21.       vNode := TreeView1.Items.AddChild(nil, Proc.szExeFile + '-PID:.' +ntToStr(Proc.th32ProcessID));
  22.  
  23.       Ico.Handle := ExtractIcon(HINSTANCE, Proc.szExeFile, 0);
  24.  
  25.  
  26.       if Ico.Handle <> 0 then
  27.       begin
  28.         ImageList1.AddIcon(Ico);
  29.         vNode.ImageIndex := ImageList1.Count - 1;
  30.         vNode.SelectedIndex := ImageList1.Count - 1;
  31.       end;
  32.       Looper := Process32Next(hSnap, proc);
  33.     end;
  34.  
  35.   finally
  36.     CloseHandle(hSnap);
  37.   end;
  38.  
  39. end;        
  40.  

This way only displays a few icons ((
If there is a way to get all the icons, how does the device manager do it?
Code: Pascal  [Select][+][-]
  1.  Ico.Handle := ExtractIcon(HINSTANCE, Proc.szExeFile, 0);

Fibonacci

  • Full Member
  • ***
  • Posts: 222
  • #PDK
Re: Pull an icon from a running process
« Reply #1 on: October 03, 2023, 11:58:17 am »
szExeFile is just a file name, not full path, you need QueryFullProcessImageName

Read the docs

https://learn.microsoft.com/en-us/windows/win32/api/tlhelp32/ns-tlhelp32-processentry32

BIT

  • Full Member
  • ***
  • Posts: 143
Re: Pull an icon from a running process
« Reply #2 on: October 03, 2023, 12:20:25 pm »
szExeFile is just a file name, not full path, you need QueryFullProcessImageName

Read the docs

https://learn.microsoft.com/en-us/windows/win32/api/tlhelp32/ns-tlhelp32-processentry32
Could you give an example, I can’t figure it out)

balazsszekely

  • Guest
Re: Pull an icon from a running process
« Reply #3 on: October 03, 2023, 12:28:45 pm »
@BIT

See attached project for more details.

BIT

  • Full Member
  • ***
  • Posts: 143
Re: Pull an icon from a running process
« Reply #4 on: October 03, 2023, 01:10:29 pm »
@BIT

See attached project for more details.
Thank you for the code I did it!)

Code: Pascal  [Select][+][-]
  1. var
  2.   WS: WideString;
  3.   Len: DWORD;
  4.   HProcess: THandle;
  5. begin
  6.  
  7. ......................
  8. ..........................
  9.  
  10.   HProcess := OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, Proc.th32ProcessID);
  11.       if HProcess <> 0 then
  12.       begin
  13.         WS := '';
  14.         SetLength(WS, 1000);
  15.         Len := 1000 + 1;
  16.         if QueryFullProcessImageNameW(HProcess, 0, Pointer(WS), @Len) then
  17.         begin
  18.           SetLength(WS, Len);
  19.           Ico.Handle := ExtractIcon(HINSTANCE, pwidechar(WS), 0);
  20.         end;
  21.       end;      
  22.  
  23. ......................
  24. ..........................
  25.  

 

TinyPortal © 2005-2018