Recent

Author Topic: check if windows exe is running ?  (Read 10961 times)

regis44

  • Newbie
  • Posts: 3
check if windows exe is running ?
« on: April 16, 2014, 07:56:38 am »
Hi, first, i'm a newbie with Lazarus.

My problem :
I need to check if a windows exe program is running. I've found many Delphi examples but they didn't work with Lazarus  :'(

Is someone can help me please ?

Thank you.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: check if windows exe is running ?
« Reply #1 on: April 16, 2014, 08:02:31 am »
When you want to try some Delphi code use Delphi mode.

Replace {$mode objfpc} with {$mode delphi}

regis44

  • Newbie
  • Posts: 3
Re: check if windows exe is running ?
« Reply #2 on: April 16, 2014, 08:10:49 am »
I try '{$mode delphi}' and this code found on the web but 'TlHelp32' is not found :

Code: [Select]
uses TlHelp32;

    function processExists(exeFileName: string): Boolean;
var
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  Result := False;
  while Integer(ContinueLoop) <> 0 do
  begin
    if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
      UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
      UpperCase(ExeFileName))) then
    begin
      Result := True;
    end;
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if processExists('notepad.exe') then
    ShowMessage('process is running')
  else
    ShowMessage('process not running');
end;

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: check if windows exe is running ?
« Reply #3 on: April 16, 2014, 08:47:42 am »
...  but 'TlHelp32' is not found :
Replace TlHelp32 with jwatlhelp32.

regis44

  • Newbie
  • Posts: 3
Re: check if windows exe is running ?
« Reply #4 on: April 16, 2014, 10:25:47 am »
Great ! It works !  :D

Thanks a lot.


eric

  • Sr. Member
  • ****
  • Posts: 267
Re: check if windows exe is running ?
« Reply #5 on: April 16, 2014, 04:54:51 pm »
You don't need {$mode delphi} for this function. Just define ContinueLoop as Boolean, not BOOL, and replace the line "while Integer(ContinueLoop) <> 0 do" with "while ContinueLoop do".

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
Re: check if windows exe is running ?
« Reply #6 on: June 11, 2017, 01:23:49 pm »
...  but 'TlHelp32' is not found :
Replace TlHelp32 with jwatlhelp32.

Thank You

 

TinyPortal © 2005-2018