Arsip: Windows services - Checking status


by LuriDarmawan in Articles more 20 years ago 1810
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;

AI Forward

๐Ÿš€ We're thrilled to partner with Alibaba Cloud for "AI Forward - Alibaba Cloud Global Developer Summit 2025" in Jakarta! Join us and explore the future of AI. Register now: https://int.alibabacloud.com/m/1000400772/
#AlibabaCloud #DeveloperSummit #Jakarta #AIFORWARD

Local Business Directory, Search Engine Submission & SEO Tools FreeWebSubmission.com SonicRun.com