Solved

Check for Exe running same for Application / Service

Posted on 2014-11-05
2
179 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 27

Accepted Solution

by:
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;
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VMware-ClientIntegrationPlugins 6 76
Batch rename files sequentially and move 7 29
Firemonkey Listview item popup menu ? 1 26
Disable SSL 3 6 27
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
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…

735 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