Q:

How can we determine a program is already running in delphi

I want run a program(exe) from my delphi application.  i can do it, but i want to check whether program is already running or not before run it.  Is there any way to do it.. Thanks in advance. 

A:

The following function checks whether a process is running.  Add "TlHelp32" to the uses clause. 
function ProcessRunning (sExeName: String) : Boolean;
{ -> sExeName : Name of the EXE without path.  Does not have to be the full EXE name. } 

var
    hSnapShot : THandle;
    ProcessEntry32 : TProcessEntry32;

begin
    Result := false;

    hSnapShot := CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
    Win32Check (hSnapShot <> INVALID_HANDLE_VALUE);

    sExeName := LowerCase (sExeName);

    FillChar (ProcessEntry32, SizeOf (TProcessEntry32), #0);
    ProcessEntry32.dwSize := SizeOf (TProcessEntry32);

    if (Process32First (hSnapShot, ProcessEntry32)) then
        repeat
            if (Pos (sExeName,
                     LowerCase (ProcessEntry32.szExeFile)) = 1) then
            begin
                Result := true;
                Break;
            end; { if }
        until (Process32Next (hSnapShot, ProcessEntry32) = false);

    CloseHandle (hSnapShot);
end; { ProcessRunning }

