Solved

Check for Exe running same for Application / Service

Posted on 2014-11-05
2
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

739 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