Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.basic.visual.misc > #903

Re: How do I start/stop a system service

From "Jeff Johnson" <I.get@enough.spam>
Newsgroups comp.lang.basic.visual.misc, microsoft.public.vb.general.discussion
Subject Re: How do I start/stop a system service
Date 2012-03-09 17:41 -0500
Organization A noiseless patient Spider
Message-ID <jje0v6$o47$1@dont-email.me> (permalink)
References <jjdm45$kv9$1@dont-email.me>

Cross-posted to 2 groups.

Show all headers | View raw


"GS" <gs@somewhere.net> wrote in message news:jjdm45$kv9$1@dont-email.me...

> My apps rely on WMI for gathering hardware info. How would I start WMI if 
> not running, then stop it when I'm done using it?

Wow, I tried finding this with a Google search and I got nothin'. Here's the 
contents of a module I have. Copy and paste what is below into a text editor 
and save it as "Services.bas" (or "basServices.bas" if you still do that 
goofy Hungarian type-dangler thing). It will allow you to deal with the SCM. 
(If you don't know what the SCM is, you have no business fiddling with 
services...!)

--- Copy from the next line onwards ---
Attribute VB_Name = "basServices"
Option Explicit

' Service API stuff

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 CloseServiceHandle Lib "advapi32.dll" (ByVal 
hSCObject As Long) As Long
Public Declare Function SetServiceStatus Lib "advapi32.dll" (ByVal 
ServiceStatus As Long, lpServiceStatus As SERVICE_STATUS) As Long
Public Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal 
hService As Long, lpServiceStatus As SERVICE_STATUS) As Long
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 ControlService Lib "advapi32.dll" (ByVal hService As 
Long, ByVal controlcommand As ServiceControl, lpServiceStatus As 
SERVICE_STATUS) As Long
Public Declare Function StartService Lib "advapi32.dll" Alias 
"StartServiceA" (ByVal hService As Long, ByVal ArgCount As Long, ByVal 
lpArgVectors As Long) As Long
Public Declare Function GetServiceKeyName Lib "advapi32.dll" Alias 
"GetServiceKeyNameA" (ByVal hSCManager As Long, ByVal DisplayName As String, 
ByVal ServiceName As String, BuffSize As Long) As Long
Public Declare Function GetServiceDisplayName Lib "advapi32.dll" Alias 
"GetServiceDisplayNameA" (ByVal hSCManager As Long, ByVal ServiceName As 
String, ByVal DisplayName As String, BuffSize As Long) As Long
Public Declare Function ChangeServiceConfig Lib "advapi32.dll" Alias 
"ChangeServiceConfigA" (ByVal hService As Long, ByVal dwServiceType As Long, 
ByVal dwStartType As Long, ByVal dwErrorControl As Long, ByVal 
lpBinaryPathName As Long, ByVal lpLoadOrderGroup As Long, ByVal lpdwTagId As 
Long, ByVal lpDependencies As Long, ByVal lpServiceStartName As Long, ByVal 
lpPassword As Long, ByVal lpDisplayName As Long) As Long
Public Declare Function DeleteService Lib "advapi32.dll" (ByVal hService As 
Long) As Long

' Control manager access
Public Const SC_MANAGER_CONNECT = &H1
Public Const SC_MANAGER_CREATE_SERVICE = &H2
Public Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Public Const SC_MANAGER_LOCK = &H8
Public Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
Public Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SC_MANAGER_ALL_ACCESS = SC_MANAGER_CONNECT Or _
   SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVICE Or _
   SC_MANAGER_LOCK Or SC_MANAGER_QUERY_LOCK_STATUS Or _
   SC_MANAGER_MODIFY_BOOT_CONFIG Or STANDARD_RIGHTS_REQUIRED
' Service access
Public Const SERVICE_QUERY_CONFIG = &H1
Public Const SERVICE_CHANGE_CONFIG = &H2
Public Const SERVICE_QUERY_STATUS = &H4
Public Const SERVICE_ENUMERATE_DEPENDENTS = &H8
Public Const SERVICE_START = &H10
Public Const SERVICE_STOP = &H20
Public Const SERVICE_PAUSE_CONTINUE = &H40
Public Const SERVICE_INTERROGATE = &H80
Public Const SERVICE_USER_DEFINED_CONTROL = &H100
Public Const SERVICE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED Or _
   SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG Or _
   SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDENTS Or _
   SERVICE_START Or SERVICE_STOP Or SERVICE_PAUSE_CONTINUE Or _
   SERVICE_INTERROGATE Or SERVICE_USER_DEFINED_CONTROL

Public Const SERVICE_NO_CHANGE = &HFFFFFFFF

Public Enum ServiceStartModes
   stmAtBoot = 0
   stmOnSystemInit = 1
   stmAutomatic = 2
   stmManual = 3
   stmDisabled = 4
End Enum

Public Enum ServiceStates
   sstUnknown = 0 ' not a real status code
   sstStopped = 1
   sstStartPending = 2
   sstStopPending = 3
   sstRunning = 4
   sstContinuePending = 5
   sstPausePending = 6
   sstPaused = 7
End Enum

Public Enum ServiceControl
   scStop = 1
   scPause = 2
   scContinue = 3
   scInterrogate = 4
   scShutdown = 5
End Enum

Public Type SERVICE_STATUS
   dwServiceType As Long
   dwCurrentState As ServiceStates
   dwControlsAccepted As Long
   dwWin32ExitCode As Long
   dwServiceSpecificExitCode As Long
   dwCheckPoint As Long
   dwWaitHint As Long
End Type

Public Function ChangeServiceStartMode(ByVal ServiceName As String, ByVal 
StartMode As ServiceStartModes)
   Dim hSCManager As Long, hService As Long, lResult As Long
   hSCManager = OpenSCManager(vbNullString, vbNullString, 
SC_MANAGER_CONNECT)
   If hSCManager <> 0 Then
      hService = OpenService(hSCManager, ByVal ServiceName, 
SERVICE_CHANGE_CONFIG)
      If hService <> 0 Then
         lResult = ChangeServiceConfig(hService, SERVICE_NO_CHANGE, 
StartMode, SERVICE_NO_CHANGE, _
            0&, 0&, 0&, 0&, 0&, 0&, 0&)
         CloseServiceHandle hService
      End If
      CloseServiceHandle hSCManager
   End If
End Function

Public Function GetDisplayName(ByVal ServiceName As String) As String
   ' Function: Get the name that displays in the Services applet based
   '           on the registry name
   Dim strOut As String, hSCManager As Long, x As Long
   GetDisplayName = ServiceName ' default
   hSCManager = OpenSCManager(vbNullString, vbNullString, 
SC_MANAGER_CONNECT)
   If hSCManager <> 0 Then
      strOut = Space$(256)
      x = Len(strOut)
      If GetServiceDisplayName(hSCManager, ByVal ServiceName, strOut, x) = 1 
Then
         x = InStr(strOut, vbNullChar)
         If x > 0 Then GetDisplayName = Left$(strOut, x - 1)
      End If
      CloseServiceHandle hSCManager
   End If
End Function

Public Function GetServiceName(ByVal DisplayName As String) As String
   ' Function: Get the registry name of the service based on the name that
   '           displays in the Services applet
   Dim strOut As String, hSCManager As Long, x As Long
   GetServiceName = DisplayName ' default
   hSCManager = OpenSCManager(vbNullString, vbNullString, 
SC_MANAGER_CONNECT)
   If hSCManager <> 0 Then
      strOut = Space$(256)
      x = Len(strOut)
      If GetServiceKeyName(hSCManager, ByVal DisplayName, strOut, x) = 1 
Then
         x = InStr(strOut, vbNullChar)
         If x > 0 Then GetServiceName = Left$(strOut, x - 1)
      End If
      CloseServiceHandle hSCManager
   End If
End Function


Public Function GetServiceState(ByVal ServiceName As String) As 
ServiceStates
   Dim x As Long, hSCManager As Long, hService As Long
   Dim ssStatus As SERVICE_STATUS
   GetServiceState = sstUnknown
   hSCManager = OpenSCManager(vbNullString, vbNullString, 
SC_MANAGER_CONNECT)
   If hSCManager <> 0 Then
      hService = OpenService(hSCManager, ByVal ServiceName, 
SERVICE_QUERY_STATUS)
      If hService <> 0 Then
         If QueryServiceStatus(hService, ssStatus) = 1 Then
            GetServiceState = ssStatus.dwCurrentState
         End If
         CloseServiceHandle hService
      End If
      CloseServiceHandle hSCManager
   End If
End Function

Public Function SetServiceContinued(ByVal ServiceName As String) As 
ServiceStates
   Dim ssStatus As SERVICE_STATUS, hSCManager As Long, hService As Long
   SetServiceContinued = sstUnknown
   hSCManager = OpenSCManager(vbNullString, vbNullString, 
SC_MANAGER_CONNECT)
   If hSCManager <> 0 Then
      hService = OpenService(hSCManager, ByVal ServiceName, 
SERVICE_PAUSE_CONTINUE)
      If hService <> 0 Then
         If ControlService(hService, scContinue, ssStatus) = 1 Then
            SetServiceContinued = ssStatus.dwCurrentState
         End If
         CloseServiceHandle hService
      End If
      CloseServiceHandle hSCManager
   End If
End Function

Public Function SetServicePaused(ByVal ServiceName As String) As 
ServiceStates
   Dim ssStatus As SERVICE_STATUS, hSCManager As Long, hService As Long
   SetServicePaused = sstUnknown
   hSCManager = OpenSCManager(vbNullString, vbNullString, 
SC_MANAGER_CONNECT)
   If hSCManager <> 0 Then
      hService = OpenService(hSCManager, ByVal ServiceName, 
SERVICE_PAUSE_CONTINUE)
      If hService <> 0 Then
         If ControlService(hService, scPause, ssStatus) = 1 Then
            SetServicePaused = ssStatus.dwCurrentState
         End If
         CloseServiceHandle hService
      End If
      CloseServiceHandle hSCManager
   End If
End Function

Public Function SetServiceStarted(ByVal ServiceName As String) As 
ServiceStates
   Dim hSCManager As Long, hService As Long
   SetServiceStarted = sstUnknown
   hSCManager = OpenSCManager(vbNullString, vbNullString, 
SC_MANAGER_ALL_ACCESS)
   If hSCManager <> 0 Then
      hService = OpenService(hSCManager, ByVal ServiceName, 
SERVICE_ALL_ACCESS)
      If hService <> 0 Then
         Err.Clear
         If StartService(hService, 0, 0) = 1 Then
            SetServiceStarted = sstRunning
         End If
         CloseServiceHandle hService
      End If
      CloseServiceHandle hSCManager
   End If
End Function

Public Function SetServiceStopped(ByVal ServiceName As String) As 
ServiceStates
   Dim ssStatus As SERVICE_STATUS, hSCManager As Long, hService As Long
   SetServiceStopped = sstUnknown
   hSCManager = OpenSCManager(vbNullString, vbNullString, 
SC_MANAGER_CONNECT)
   If hSCManager <> 0 Then
      hService = OpenService(hSCManager, ByVal ServiceName, SERVICE_STOP)
      If hService <> 0 Then
         If ControlService(hService, scStop, ssStatus) = 1 Then
            SetServiceStopped = ssStatus.dwCurrentState
         End If
         CloseServiceHandle hService
      End If
      CloseServiceHandle hSCManager
   End If
End Function


Back to comp.lang.basic.visual.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-09 14:36 -0500
  Re: How do I start/stop a system service "MikeD" <nobody@nowhere.edu> - 2012-03-09 15:45 -0500
    Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-09 17:38 -0500
      Re: How do I start/stop a system service "MikeD" <nobody@nowhere.edu> - 2012-03-10 07:27 -0500
        Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-10 13:16 -0500
          Re: How do I start/stop a system service "MikeD" <nobody@nowhere.edu> - 2012-03-10 17:57 -0500
            Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-10 21:08 -0500
              Re: How do I start/stop a system service "MikeD" <nobody@nowhere.edu> - 2012-03-10 22:57 -0500
                Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-10 23:58 -0500
                Re: How do I start/stop a system service Jason Keats <jkeats@melbpcDeleteThis.org.au> - 2012-03-11 20:22 +1100
                Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-11 15:54 -0400
                Re: How do I start/stop a system service Jason Keats <jkeats@melbpcDeleteThis.org.au> - 2012-03-12 15:19 +1100
                Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-12 01:14 -0400
                Re: How do I start/stop a system service "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-03-11 09:20 +0000
                Re: How do I start/stop a system service "MikeD" <nobody@nowhere.edu> - 2012-03-11 19:11 -0400
              Re: How do I start/stop a system service "Mike Williams" <Mike@WhiskyAndCoke.com> - 2012-03-11 09:13 +0000
                Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-11 16:19 -0400
                Re: How do I start/stop a system service "MikeD" <nobody@nowhere.edu> - 2012-03-11 19:23 -0400
                Re: How do I start/stop a system service "Mayayana" <mayayana@invalid.nospam> - 2012-03-11 20:54 -0500
                Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-11 22:12 -0400
                Re: How do I start/stop a system service "Mayayana" <mayayana@invalid.nospam> - 2012-03-12 09:57 -0500
                Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-12 13:24 -0400
                Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-12 13:59 -0400
                Re: How do I start/stop a system service "Henning" <computer_hero@coldmail.com> - 2012-03-12 22:46 +0100
                Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-12 18:59 -0400
  Re: How do I start/stop a system service "Farnsworth" <nospam@nospam.com> - 2012-03-09 16:01 -0500
    Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-09 17:36 -0500
      Re: How do I start/stop a system service "Farnsworth" <nospam@nospam.com> - 2012-03-09 18:55 -0500
        Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-09 21:30 -0500
          Re: How do I start/stop a system service "Farnsworth" <nospam@nospam.com> - 2012-03-11 11:14 -0500
  Re: How do I start/stop a system service "Jeff Johnson" <I.get@enough.spam> - 2012-03-09 17:41 -0500
    Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-09 18:23 -0500
      Re: How do I start/stop a system service "Mayayana" <mayayana@invalid.nospam> - 2012-03-10 09:24 -0500
        Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-10 13:06 -0500
          Re: How do I start/stop a system service "Mayayana" <mayayana@invalid.nospam> - 2012-03-10 14:11 -0500
            Re: How do I start/stop a system service GS <gs@somewhere.net> - 2012-03-10 15:25 -0500

csiph-web