Check for Exe running same for Application / Service

I was using below code for checking is application is running. Can I use same code for Windows Services too. Testing works fine on XP & win7 do not know behavior on latest OS and Server OS.

function CheckIfTaskRunningAlready(ExeFileName: string): Boolean;
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
  nInstances:Integer ;
begin  // This function will abruptly halt a application. eg: KillTask(Outlook.exe)
  result := False ;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  nInstances:=0 ;
  while integer(ContinueLoop) <> 0 do
    if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName)) or
       (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then
      Inc(nInstances) ;
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);

  if UpperCase(ExeFileName)=UpperCase(ExtractFileName(Application.ExeName)) then
  begin // Exe to be checked is same as current application therefore 1 count will be incremented for self
    if (nInstances>1) then  // This is 2nd instance
      Result := True
    if (nInstances>0) then  // Instance running
      Result := True

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sinisa VukSoftware architectCommented:
i use this function to check if service is running. Pass service name ... real name, not display name
function IsServiceStarted(sService : string): Boolean;
  schm, schs: SC_Handle;
  ss: TServiceStatus;
  Result := False;
  ZeroMemory(@ss, SizeOf(TServiceStatus));
  // connect to the service control manager
  schm := OpenSCManager(Nil, Nil, SC_MANAGER_CONNECT);

  if(schm > 0)then
    // open a handle to the specified service
    schs := OpenService(schm, PChar(sService), SERVICE_INTERROGATE or SERVICE_QUERY_STATUS);

    if(schs > 0)then
      if not ControlService(schs, SERVICE_CONTROL_INTERROGATE, ss) then
        QueryServiceStatus(schs, ss);

      if ss.dwCurrentState = SERVICE_RUNNING then
        Result := True;

      // close service handle

    // close service control manager handle

Open in new window

... main goal is to contact service manager and check for service state.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.