Beranda > Delphi > Windows services – Checking status ()

Windows services – Checking status ()

Ingin tau mencari status dari ServiceWindows ?
gunakan fungsi “QueryServiceStatus()” Caranya adalah sebagai berikut:

uses WinSvc;

//-------------------------------------
// get service status
// return status code if successful -1 if not
// return codes:
//   SERVICE_STOPPED
//   SERVICE_RUNNING
//   SERVICE_PAUSED
//   SERVICE_START_PENDING
//   SERVICE_STOP_PENDING
//   SERVICE_CONTINUE_PENDING
//   SERVICE_PAUSE_PENDING
// sMachine:
//   machine name, ie: \SERVER
//   empty = local machine
// sService
//   service name, ie: Alerter
function ServiceGetStatus(  sMachine, sService : string ) : DWord;
var
  // service control manager handle
  schm,
  // service handle
  schs   : SC_Handle;
  // service status
  ss     : TServiceStatus;
  // current service status
  dwStat : DWord;
begin
  dwStat := -1;

  // connect to the service control manager 
  schm := OpenSCManager( PChar(sMachine), Nil, SC_MANAGER_CONNECT);

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

    // if successful...
    if(schs > 0)then
    begin
      // retrieve the current status of the specified service    
      if (QueryServiceStatus( schs, ss)) then
      begin
        dwStat := ss.dwCurrentState;
      end;
      // close service handle
      CloseServiceHandle(schs);
    end;

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

  Result := dwStat;
end; 

Naahh…..
saat ini kita punya fungsi dasar — “ServiceGetStatus()” — untuk mendapat status dari service yang berjalan pada mesin tertentu, mari kita sederhanakan proses dengan menggunakan fungsi: “ServiceRunning()” and “ServiceStopped()”

function ServiceRunning( psMachine, psService : string ) : boolean;
begin
  Result := SERVICE_RUNNING = ServiceGetStatus( psMachine, psService );
end;

function ServiceStopped( psMachine, psService : string ) : boolean;
begin
  Result := SERVICE_STOPPED = ServiceGetStatus( psMachine, psService );
end;

Baik “ServiceRunning()” maupun “ServiceStopped()” membutuhkan nama mesin dan nama service. Nama Mesin dapat dikosongkan jika yang diuji adalah mesin lokal.
Contohnya:

if( ServiceRunning( '\ComputerName', 'alerter' ) ) then
begin
  // service "alerter" di \ComputerName sedang berjalan
  // tulikan apa yg akan dilakukan disini
end;

if ( ServiceRunning( '', 'alerter' ) ) then
begin
  // service "alerter" di lokal komputer sedang berjalan
end;

if( ServiceStopped( '', 'alerter' ) ) then
begin
  // service "alerter" di lokal komputer berhenti
end;
Iklan
Kategori:Delphi
  1. Agustus 25, 2017 pukul 1:53 am

    I do trust all the ideas you have introduced on your post.
    They are very convincing and can certainly work. Still, the posts are too short for newbies.
    May just you please lengthen them a bit from subsequent time?
    Thank you for the post.

  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: