Solved

Check for Exe running same for Application / Service

Posted on 2014-11-05
2
172 Views
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;
var
  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
  begin
    if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName)) or
       (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then
      Inc(nInstances) ;
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;

  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
  end
  else
  begin
    if (nInstances>0) then  // Instance running
      Result := True
  end;
  CloseHandle(FSnapshotHandle);
end;

Open in new window

0
Comment
Question by:Allan_Fernandes
2 Comments
 
LVL 25

Accepted Solution

by:
Sinisa Vuk earned 500 total points
Comment Utility
i use this function to check if service is running. Pass service name ... real name, not display name
function IsServiceStarted(sService : string): Boolean;
var
  schm, schs: SC_Handle;
  ss: TServiceStatus;
begin
  Result := False;
  ZeroMemory(@ss, SizeOf(TServiceStatus));
  
  // connect to the service control manager
  schm := OpenSCManager(Nil, Nil, SC_MANAGER_CONNECT);

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

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

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

      // close service handle
      CloseServiceHandle(schs);
    end;

    // close service control manager handle
    CloseServiceHandle(schm);
  end;
end;

Open in new window


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

Featured Post

Are your corporate email signatures appalling?

Is it scary how unprofessional your email signatures look? Do users create their own terrible designs and give themselves stupid job titles? You can make this a lot easier for yourself by choosing an email signature management solution from Exclaimer today.

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

762 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

10 Experts available now in Live!

Get 1:1 Help Now