1

I'm trying to show all processes which are currently running on my system. Can any one guide me to do that.

hivert
  • 10,579
  • 3
  • 31
  • 56
Ramkee
  • 900
  • 1
  • 10
  • 27
  • Sorry, but I have to know that :-) Why ? – TLama May 13 '14 at 08:02
  • Thanks for your reply.. Actually this is one of the task of mine which i have to show to my boss and i have read the inno help document. I got little idea. But i cant move forward regarding this issue. can you help me – Ramkee May 13 '14 at 09:58
  • 1
    I can't think of any real usage for this, but well, boss is boss :-) – TLama May 13 '14 at 11:47
  • haha well said TLama.But Am very new to inno scripts i need to learn more info about inno – Ramkee May 13 '14 at 12:20
  • @TLama (I wish you were still here...): a usage would be to know if a service program (e.g. a VNC server) is already installed, when such programs (e.g. TightVNC) don't really leave a trace of their installation directory in the registry. – Matthieu Nov 03 '19 at 08:15
  • 1
    @Matthieu, well, for that one would not list all the processes rather than just checking whether the specific process is running. – TLama Nov 04 '19 at 07:35

1 Answers1

2

I would use WMI for this task since WMI can list also 64-bit processes which I think the Windows API way cannot. Here is an example which uses the Win32_Process class to query list of running processes:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
const
  WM_SETREDRAW = $000B;
var
  ProcessList: TNewListBox;

// this is a substitution for missing BeginUpdate method
// of TStrings class
procedure WinControlBeginUpdate(Control: TWinControl);
begin
  SendMessage(Control.Handle, WM_SETREDRAW, 0, 0);
end;

// this is a substitution for missing EndUpdate method
// of TStrings class
procedure WinControlEndUpdate(Control: TWinControl);
begin
  SendMessage(Control.Handle, WM_SETREDRAW, 1, 0);
  Control.Refresh;
end;

function GetProcessNames(Items: TStrings): Integer;
var
  I: Integer;
  WQLQuery: string;
  WbemLocator: Variant;
  WbemServices: Variant;
  WbemObject: Variant;
  WbemObjectSet: Variant;
begin
  Result := 0;

  WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  WbemServices := WbemLocator.ConnectServer('localhost', 'root\CIMV2');

  WQLQuery := 'SELECT Name FROM Win32_Process';

  WbemObjectSet := WbemServices.ExecQuery(WQLQuery);
  if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
  begin
    Items.Clear;
    for I := 0 to WbemObjectSet.Count - 1 do
    begin
      WbemObject := WbemObjectSet.ItemIndex(I);
      if not VarIsNull(WbemObject) then
        Items.Add(WbemObject.Name);
    end;
    Result := Items.Count;
  end;
end;

procedure RefreshButtonClick(Sender: TObject);
begin
  // this try..finally block is used to reduce annoying visual effects
  // when filling the list box; Inno Setup doesn't publish BeginUpdate,
  // EndUpdate method pair, hence this home brewn solution
  WinControlBeginUpdate(ProcessList);
  try
    GetProcessNames(ProcessList.Items);
  finally
    WinControlEndUpdate(ProcessList);
  end;
end;

procedure InitializeWizard;
var
  RefreshBtn: TNewButton;
  ProcessPage: TWizardPage;
begin
  ProcessPage := CreateCustomPage(wpWelcome, 'Caption', 'Description');

  ProcessList := TNewListBox.Create(WizardForm);
  ProcessList.Parent := ProcessPage.Surface;
  ProcessList.SetBounds(0, 0, ProcessPage.SurfaceWidth,
    ProcessPage.SurfaceHeight - 33);

  RefreshBtn := TNewButton.Create(WizardForm);
  RefreshBtn.Parent := ProcessPage.Surface;
  RefreshBtn.Left := 0;
  RefreshBtn.Top := ProcessPage.SurfaceHeight - RefreshBtn.Height;
  RefreshBtn.Caption := 'Refresh';
  RefreshBtn.OnClick := @RefreshButtonClick;
end;
TLama
  • 75,147
  • 17
  • 214
  • 392
  • That is really help full. may i know what does the following line represents " WbemServices := WbemLocator.ConnectServer('localhost', 'root\CIMV2'); " – Ramkee May 13 '14 at 12:16
  • It connects to the `root\CIMV2` WMI namespace on the `localhost` computer and if succeeds return the `SWbemServices` object. For more information see the [`ConnectServer`](http://msdn.microsoft.com/en-us/library/aa393720(v=vs.85).aspx) method reference. – TLama May 13 '14 at 12:19
  • 1
    yes i got it dude.But even if i changed it in to "users\safesquid" instead of "root\CIMV2" i got the list of current running processes – Ramkee May 13 '14 at 12:23
  • 1
    That's not possible. The `Win32_Process` class is stored under the `root\CIMV2` namespace. You would get the *"Invalid namespace"* exception when you'd try to connect to a not existing namespace. – TLama May 13 '14 at 12:27
  • Here am working on win64 machine, i tried with different directories – Ramkee May 13 '14 at 12:27
  • yes i got that same error what you have said before ( invalid name space) – Ramkee May 13 '14 at 12:29
  • I have embedded this code in my installer, and on a rare occasion it cannot create WbetScripting.SWbemLocator object, and shows a corresponding dialog box. I tried troubleshooting. All the necessary dlls were in their places and registered. So I haven't figured out why it failed. – AlexStepanov Feb 16 '22 at 09:41