Jeff Johnson [MVP: VB]
3/9/2012 10:42:00 PM
"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