Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Knowledge Base > Code Library > Service class (.cls)


Reply
 
Thread Tools Display Modes
  #1  
Old 06-01-2003, 05:51 PM
usetheforce2's Avatar
usetheforce2 usetheforce2 is offline
Senior Contributor

Retired Moderator
* Expert *
 
Join Date: Jul 2000
Location: Toronto, Ontario, Canada
Posts: 1,410
Default Service class (.cls)


Hello all,

Here is a .cls that i put together that encapsulates all the basic service operation used.
  1. Start Service
  2. Stop Service
  3. Resume Service
  4. Pause Serivce
  5. Enumerate Dependent services (based on the Service Name)
  6. Enummerate service status for (Active, InActive, and All services)
also, it expose some of the basic service properties
  1. Service Name
  2. Service Display Name
  3. Service State (Started, Stopped, Paused)
  4. Service Type (The service is a device driver, The service is a file system driver ...)
with basic error control and messaging.

[API's used]
Code:
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

if you have any questions or suggestions, let me know.
Regan
Attached Files
File Type: zip Services.zip (7.2 KB, 626 views)
__________________
winsock siteHERE||EliteVBHERE||C++ & VB Markup UtilityHERE
Reply With Quote
  #2  
Old 06-01-2003, 06:33 PM
usetheforce2's Avatar
usetheforce2 usetheforce2 is offline
Senior Contributor

Retired Moderator
* Expert *
 
Join Date: Jul 2000
Location: Toronto, Ontario, Canada
Posts: 1,410
Default

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):
Code:
' 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):

Code:
' 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
Attached Files
File Type: zip ActiveX dll Services.zip (16.2 KB, 221 views)
__________________
winsock siteHERE||EliteVBHERE||C++ & VB Markup UtilityHERE

Last edited by usetheforce2; 06-02-2003 at 11:53 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
NT Service IN VB iexplore General 5 06-17-2003 02:59 PM
Class Structures Cogen Web Programming 4 05-30-2003 03:04 PM
OOP woes: Dynamic Class Creation (my rpg) Virgo Game Programming 7 03-12-2003 04:40 PM
Problem with class type bryan76 Miscellaneous Languages 2 05-23-2002 05:01 AM
Another InStr type question bradac General 4 09-01-2001 01:27 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->