Service class (.cls)

usetheforce2
06-01-2003, 05:51 PM
Hello all,

Here is a .cls that i put together that encapsulates all the basic service operation used.


Start Service
Stop Service
Resume Service
Pause Serivce
Enumerate Dependent services (based on the Service Name)
Enummerate service status for (Active, InActive, and All services)

also, it expose some of the basic service properties

Service Name
Service Display Name
Service State (Started, Stopped, Paused)
Service Type (The service is a device driver, The service is a file system driver ...)

with basic error control and messaging.

[API's used]

Public Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
Public Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal hService As Long, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long
Public Declare Function ControlService Lib "advapi32.dll" (ByVal hService As Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long
Public Declare Function EnumDependentServices Lib "advapi32.dll" Alias "EnumDependentServicesA" (ByVal hService As Long, ByVal dwServiceState As Long, lpServices As ENUM_SERVICE_STATUS, ByVal cbBufSize As Long, pcbBytesNeeded As Long, lpServicesReturned As Long) As Long

Public Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Public Declare Function EnumServicesStatus Lib "advapi32.dll" Alias "EnumServicesStatusA" (ByVal hSCManager As Long, ByVal dwServiceType As Long, ByVal dwServiceState As Long, lpServices As Any, ByVal cbBufSize As Long, pcbBytesNeeded As Long, lpServicesReturned As Long, lpResumeHandle As Long) As Long
Public Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
Public Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (szDest As String, szcSource As Long) As Long




[references:]
www.AllApi.net
MSDN Service API's (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/service_functions.asp)

if you have any questions or suggestions, let me know.
Regan

usetheforce2
06-01-2003, 06:33 PM
Enabling vbscripts with the services class...

either compile or download the Service class in this project and then simple user the CreateObject or GetObject VBscript functions to retrieve reference to this class

example code (list all acitive and inactive services):
' create a services.services object
' get the services (active) and itterate the services
Dim y
Dim strServiceList
Const SERVICE_ACTIVE = &H1
Const SERVICE_INACTIVE = &H2

' get reference to the object
Set objServices = createobject("serviceSVR.services")

' retrieve all services installed
objServices.GetServices SERVICE_ACTIVE Or SERVICE_INACTIVE

' enumerate the service list
For y = 0 To clng(objServices.ServiceCount) - 1
wscript.echo objServices.GetDisplayName(clng(y)) & " - "
Next


example code (START and STOP a service):

' create a services.services object
' get the services (active) and iterate the services

Set args = wscript.arguments

If args.count <> 2 Then
wscript.echo "services.vbs <start|stop> <service name>"
End If

'' get reference to the object
Set objServices = createobject("serviceSVR.services")

If args(0) = "start" Then
If objServices.Start_Service("netman") Then
wscript.echo "Service Started"
Else
wscript.echo objServices.LastErrorDescription
End If
ElseIf args(0) = "stop" Then
If objServices.Stop_Service("netman") Then
wscript.echo "Service Stopped"
Else
wscript.echo objServices.LastErrorDescription
End If
Else
wscript.echo "First parameter is incorrect"
End If

'[sample output]
'C:\Documents And Settings\UseTheForce>cscript C:\SCRIPTS\services.vbs
'
'services.vbs <start|stop> <service name>
'
'C:\>cscript C:\SCRIPTS\services.vbs start netman
'
'Service Started
'
'C:\>cscript C:\SCRIPTS\services.vbs start netman
'
'An instance of the service Is already running.
'
'C:\Documents And Settings\UseTheForce>cscript C:\SCRIPTS\services.vbs Stop netman
'
'Service Stopped
enjoy,
Regan

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum