Check for Exe running same for Application / Service

Posted on 2014-11-05
Last Modified: 2014-11-06
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

Question by:Allan_Fernandes
LVL 26

Accepted Solution

Sinisa Vuk earned 500 total points
ID: 40427276
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.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now