aluiken
asked on
NT Services
Hey all,
I want to get the state from a service under nt (eg running, stopped) Could anybody tell me how i could do this ?
Thankx,
Arijan
I want to get the state from a service under nt (eg running, stopped) Could anybody tell me how i could do this ?
Thankx,
Arijan
aluiken, here is the full code for this, paste the following into a module.
Then in your form code use the function call:
strResponse = QueryService("ServiceName" ,"ServerNa me")
msgbox "ServiceName Status Is: " & strResponse
This will in fact check the status on the local machine or any machine on the network. Leave the second parameter as "" for the local machine.
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 OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Public Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) 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 StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal hService As Long, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long
Public Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
Const STANDARD_RIGHTS_REQUIRED = &HF0000
Const STANDARD_RIGHTS_ALL = &H1F0000
Const SPECIFIC_RIGHTS_ALL = &HFFFF
' Service database names
Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
Const SERVICES_FAILED_DATABASE = "ServicesFailed"
' Value to indicate no change to an optional parameter
Const SERVICE_NO_CHANGE = &HFFFF
' Service State -- for Enum Requests (Bit Mask)
Const SERVICE_ACTIVE = &H1
Const SERVICE_INACTIVE = &H2
Const SERVICE_STATE_ALL = (SERVICE_ACTIVE Or SERVICE_INACTIVE)
' Controls
Const SERVICE_CONTROL_STOP = &H1
Const SERVICE_CONTROL_PAUSE = &H2
Const SERVICE_CONTROL_CONTINUE = &H3
Const SERVICE_CONTROL_INTERROGAT E = &H4
Const SERVICE_CONTROL_SHUTDOWN = &H5
' Service State -- for CurrentState
Const SERVICE_STOPPED = &H1
Const SERVICE_START_PENDING = &H2
Const SERVICE_STOP_PENDING = &H3
Const SERVICE_RUNNING = &H4
Const SERVICE_CONTINUE_PENDING = &H5
Const SERVICE_PAUSE_PENDING = &H6
Const SERVICE_PAUSED = &H7
' Controls Accepted (Bit Mask)
Const SERVICE_ACCEPT_STOP = &H1
Const SERVICE_ACCEPT_PAUSE_CONTI NUE = &H2
Const SERVICE_ACCEPT_SHUTDOWN = &H4
' Service Control Manager object specific access types
Const SC_MANAGER_CONNECT = &H1
Const SC_MANAGER_CREATE_SERVICE = &H2
Const SC_MANAGER_ENUMERATE_SERVI CE = &H4
Const SC_MANAGER_LOCK = &H8
Const SC_MANAGER_QUERY_LOCK_STAT US = &H10
Const SC_MANAGER_MODIFY_BOOT_CON FIG = &H20
Const SC_MANAGER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SC_MANAGER_CONNECT Or SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVI CE Or SC_MANAGER_LOCK Or SC_MANAGER_QUERY_LOCK_STAT US Or SC_MANAGER_MODIFY_BOOT_CON FIG)
' Service object specific access type
Const SERVICE_QUERY_CONFIG = &H1
Const SERVICE_CHANGE_CONFIG = &H2
Const SERVICE_QUERY_STATUS = &H4
Const SERVICE_ENUMERATE_DEPENDEN TS = &H8
Const SERVICE_START = &H10
Const SERVICE_STOP = &H20
Const SERVICE_PAUSE_CONTINUE = &H40
Const SERVICE_INTERROGATE = &H80
Const SERVICE_USER_DEFINED_CONTR OL = &H100
Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG Or SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDEN TS Or SERVICE_START Or SERVICE_STOP Or SERVICE_PAUSE_CONTINUE Or SERVICE_INTERROGATE Or SERVICE_USER_DEFINED_CONTR OL)
Public Type SERVICE_STATUS
dwServiceType As Long
dwCurrentState As Long
dwControlsAccepted As Long
dwWin32ExitCode As Long
dwServiceSpecificExitCode As Long
dwCheckPoint As Long
dwWaitHint As Long
End Type
Public Function QueryService(strServiceNam e As String, strServerName As String) As String
LogMsg "QueryService : " & strServiceName
Dim lngSMHandle As Long
Dim lngSvcHandle As Long
Dim ssStatus As SERVICE_STATUS
lngSMHandle = OpenSCManager(strServerNam e, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT)
lngSvcHandle = OpenService(lngSMHandle, strServiceName, SERVICE_ALL_ACCESS)
ret = QueryServiceStatus(lngSvcH andle, ssStatus)
If lngSvcHandle = 0& Then QueryService = "Service Not Found"
Select Case ssStatus.dwCurrentState
Case SERVICE_STOPPED
QueryService = "Service Is Stopped"
Case SERVICE_START_PENDING
QueryService = "Service Start Pending"
Case SERVICE_STOP_PENDING
QueryService = "Service Stop Pending"
Case SERVICE_RUNNING
QueryService = "Service Is Running"
Case SERVICE_CONTINUE_PENDING
QueryService = "Service Continue Pending"
Case SERVICE_PAUSE_PENDING
QueryService = "Service Pause Pending"
Case SERVICE_PAUSED
QueryService = "Service Paused"
End Select
CloseServiceHandle lngSvcHandle
CloseServiceHandle lngSMHandle
End Function
Public Sub StopSvc(strServiceName As String, strServerName As String)
LogMsg "Stop Service : " & strServiceName
Dim lngSMHandle As Long
Dim lngSvcHandle As Long
Dim ssStatus As SERVICE_STATUS
lngSMHandle = OpenSCManager(strServerNam e, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT)
lngSvcHandle = OpenService(lngSMHandle, strServiceName, SERVICE_ALL_ACCESS)
ret = QueryServiceStatus(lngSvcH andle, ssStatus)
If lngSvcHandle <> 0& Then
ControlService lngSvcHandle, SERVICE_CONTROL_STOP, ssStatus
End If
CloseServiceHandle lngSvcHandle
CloseServiceHandle lngSMHandle
End Sub
Public Sub StartSvc(strServiceName As String, strServerName As String)
LogMsg "Start Service : " & strServiceName
Dim lngSMHandle As Long
Dim lngSvcHandle As Long
Dim ssStatus As SERVICE_STATUS
lngSMHandle = OpenSCManager(strServerNam e, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT)
lngSvcHandle = OpenService(lngSMHandle, strServiceName, SERVICE_ALL_ACCESS)
ret = QueryServiceStatus(lngSvcH andle, ssStatus)
If lngSvcHandle <> 0& Then
StartService lngSvcHandle, 0&, 0&
End If
CloseServiceHandle lngSvcHandle
CloseServiceHandle lngSMHandle
End Sub
Then in your form code use the function call:
strResponse = QueryService("ServiceName"
msgbox "ServiceName Status Is: " & strResponse
This will in fact check the status on the local machine or any machine on the network. Leave the second parameter as "" for the local machine.
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 OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Public Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) 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 StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal hService As Long, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long
Public Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
Const STANDARD_RIGHTS_REQUIRED = &HF0000
Const STANDARD_RIGHTS_ALL = &H1F0000
Const SPECIFIC_RIGHTS_ALL = &HFFFF
' Service database names
Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
Const SERVICES_FAILED_DATABASE = "ServicesFailed"
' Value to indicate no change to an optional parameter
Const SERVICE_NO_CHANGE = &HFFFF
' Service State -- for Enum Requests (Bit Mask)
Const SERVICE_ACTIVE = &H1
Const SERVICE_INACTIVE = &H2
Const SERVICE_STATE_ALL = (SERVICE_ACTIVE Or SERVICE_INACTIVE)
' Controls
Const SERVICE_CONTROL_STOP = &H1
Const SERVICE_CONTROL_PAUSE = &H2
Const SERVICE_CONTROL_CONTINUE = &H3
Const SERVICE_CONTROL_INTERROGAT
Const SERVICE_CONTROL_SHUTDOWN = &H5
' Service State -- for CurrentState
Const SERVICE_STOPPED = &H1
Const SERVICE_START_PENDING = &H2
Const SERVICE_STOP_PENDING = &H3
Const SERVICE_RUNNING = &H4
Const SERVICE_CONTINUE_PENDING = &H5
Const SERVICE_PAUSE_PENDING = &H6
Const SERVICE_PAUSED = &H7
' Controls Accepted (Bit Mask)
Const SERVICE_ACCEPT_STOP = &H1
Const SERVICE_ACCEPT_PAUSE_CONTI
Const SERVICE_ACCEPT_SHUTDOWN = &H4
' Service Control Manager object specific access types
Const SC_MANAGER_CONNECT = &H1
Const SC_MANAGER_CREATE_SERVICE = &H2
Const SC_MANAGER_ENUMERATE_SERVI
Const SC_MANAGER_LOCK = &H8
Const SC_MANAGER_QUERY_LOCK_STAT
Const SC_MANAGER_MODIFY_BOOT_CON
Const SC_MANAGER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SC_MANAGER_CONNECT Or SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVI
' Service object specific access type
Const SERVICE_QUERY_CONFIG = &H1
Const SERVICE_CHANGE_CONFIG = &H2
Const SERVICE_QUERY_STATUS = &H4
Const SERVICE_ENUMERATE_DEPENDEN
Const SERVICE_START = &H10
Const SERVICE_STOP = &H20
Const SERVICE_PAUSE_CONTINUE = &H40
Const SERVICE_INTERROGATE = &H80
Const SERVICE_USER_DEFINED_CONTR
Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG Or SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDEN
Public Type SERVICE_STATUS
dwServiceType As Long
dwCurrentState As Long
dwControlsAccepted As Long
dwWin32ExitCode As Long
dwServiceSpecificExitCode As Long
dwCheckPoint As Long
dwWaitHint As Long
End Type
Public Function QueryService(strServiceNam
LogMsg "QueryService : " & strServiceName
Dim lngSMHandle As Long
Dim lngSvcHandle As Long
Dim ssStatus As SERVICE_STATUS
lngSMHandle = OpenSCManager(strServerNam
lngSvcHandle = OpenService(lngSMHandle, strServiceName, SERVICE_ALL_ACCESS)
ret = QueryServiceStatus(lngSvcH
If lngSvcHandle = 0& Then QueryService = "Service Not Found"
Select Case ssStatus.dwCurrentState
Case SERVICE_STOPPED
QueryService = "Service Is Stopped"
Case SERVICE_START_PENDING
QueryService = "Service Start Pending"
Case SERVICE_STOP_PENDING
QueryService = "Service Stop Pending"
Case SERVICE_RUNNING
QueryService = "Service Is Running"
Case SERVICE_CONTINUE_PENDING
QueryService = "Service Continue Pending"
Case SERVICE_PAUSE_PENDING
QueryService = "Service Pause Pending"
Case SERVICE_PAUSED
QueryService = "Service Paused"
End Select
CloseServiceHandle lngSvcHandle
CloseServiceHandle lngSMHandle
End Function
Public Sub StopSvc(strServiceName As String, strServerName As String)
LogMsg "Stop Service : " & strServiceName
Dim lngSMHandle As Long
Dim lngSvcHandle As Long
Dim ssStatus As SERVICE_STATUS
lngSMHandle = OpenSCManager(strServerNam
lngSvcHandle = OpenService(lngSMHandle, strServiceName, SERVICE_ALL_ACCESS)
ret = QueryServiceStatus(lngSvcH
If lngSvcHandle <> 0& Then
ControlService lngSvcHandle, SERVICE_CONTROL_STOP, ssStatus
End If
CloseServiceHandle lngSvcHandle
CloseServiceHandle lngSMHandle
End Sub
Public Sub StartSvc(strServiceName As String, strServerName As String)
LogMsg "Start Service : " & strServiceName
Dim lngSMHandle As Long
Dim lngSvcHandle As Long
Dim ssStatus As SERVICE_STATUS
lngSMHandle = OpenSCManager(strServerNam
lngSvcHandle = OpenService(lngSMHandle, strServiceName, SERVICE_ALL_ACCESS)
ret = QueryServiceStatus(lngSvcH
If lngSvcHandle <> 0& Then
StartService lngSvcHandle, 0&, 0&
End If
CloseServiceHandle lngSvcHandle
CloseServiceHandle lngSMHandle
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Use NetSVC.EXE (NT Resource Kit)
netsvc \\servername /query "ServiceKey"
netsvc \\servername /query "ServiceKey"
ASKER
No sorry it all has to be without calling other programs, just plain vb
ASKER
thx
HTH :)
Paul