How To : Using Win32::Packages with Perl 5

Win32::Service

Last update 23 june 97

(Checked with build 306)


An NT Service is a background process (daemon for Unix folks) which is loaded by the Service Control Manager of the NT kernel. They are often loaded at bootup, before any user logs in, and are often independent of any specific user being logged on at the time. If a service is not launched automatically by the system at boot time, as many services are, it can also be manually launched by a user at the console, via the NT Control Panel's Services applet, or by Perl ...

GetServices method

The GetServices method returns a list of services of a specified host.

Syntax
Win32::Service::GetServices($HostName,*List);
$HostName Name of the host, '' for local or '\\\\MyComputer'.
*List Reference of an array. The key is the Service name in the registry and the value is the Display Name.

Example

use Win32::Service;
my ($key, %service);

Win32::Service::GetServices('',\%services);

foreach $key (sort keys %services)
	{
	print "Display Name\t: $key, $services{$key}\n";
	}

GetStatus method

The GetStatus method returns hash with informations about a service

Syntax
Win32::Service::GetStatus($HostName,$Service,*StatusInfo);
$HostName Name of the host, '' for local or '\\\\MyComputer'.
$Service Service registry key name.
*Status_Info Reference to a hash, see table below.


StatusInfo
WaitHint Amount of milliseconds, that the service expects any operation to take.
CheckPoint Value that the service increments periodically to report its progress. Zero when the service does not have a any operation pending.
Win32ExitCode Win32 error code that the service uses when an error occurs.This value soubl be zero when it is running and on normal termination.
ServiceType Indicates the type of service.
ServiceSpecificExitCode Service specific error code.
CurrentState Indicates the current state of the service.
ControlsAccepted Specifies the control codes that the service will accept and process.

Example

use Win32::Service;
my ($key, %service, %status, $part);

Win32::Service::GetServices('',\%services);

foreach $key (sort keys %services)
	{
	print "Display Name\t: $key, $services{$key}\n";
	Win32::Service::GetStatus( '',$services{$key}, \%status);
	foreach $part (keys %status)
		{
		print "\t$part : $status{$part}\n";
		}
	}

StartService method

The StartService method starts the specified service

Syntax
Win32::Service::StartService($HostName,$ServiceName);
$HostName Name of the host, '' for local or '\\\\MyComputer'.
$ServiceName Service registry key name.

Example

use Win32::Service;
use Win32;
my %status;

Win32::Service::GetStatus( '','NetDDE', \%status);
die "service is arealdy started\n" if ($status{CurrentState} == 4);

Win32::Service::StartService(Win32::NodeName( ),'NetDDE') || die "Can't start service\n";
print "Service started\n";

StopService method

The StopService method stops a service.

Syntax
Win32::Service::GetServices($HostName,$ServiceName);
$HostName Name of the host, '' for local or '\\\\MyComputer'.
$ServiceName Service registry key name.

Example

use Win32::Service;
my %status;

Win32::Service::GetStatus( '','https-luke', \%status);
die "Service is not running\n" if ($status{CurrentState} == 1 )
Win32::Service::StopService('','https-luke') || die "can't stop service\n";
print "Service stopped\n";

PauseService method

The PauseService method pauses a service. Not all the services are able to pause.

Syntax
Win32::Service::PauseService($HostName,$ServiceName);
$HostName Name of the host, '' for local or '\\\\MyComputer'.
$ServiceName Service registry key name.

Example

use Win32::Service;

Win32::Service::PauseService('','NetDDE') || die "can't pause service\n";
print "Service paused\n";

ResumeService method

The ResumeService method resumes a service.

Syntax
Win32::Service::GetServices($HostName,$ServiceName);
$HostName Name of the host, '' for local or '\\\\MyComputer'.
$ServiceName Service registry key name.

Example

use Win32::Service;

Win32::Service::ResumeService('','NetDDE') || die "can't resume service\n";
print "Service resumed\n";

Copyright © 1997 Philippe Le Berre

Any rights not expressly granted herein are reserved.