Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Starting a remote service...

Status
Not open for further replies.

devilman0

IS-IT--Management
Nov 14, 2002
257
US
I havn't been able to find any resources on how to start a remote service. I can enume. it's status, but can't get it to start. Thanks in advance.

Thanks,
James
[afro][mad]
"Make it idiot-proof and someone will make a better idiot." ~bumper sticker
 
Bit rusty as to details but I recall you can use sc.exe but its part of the sdk and not distributable.

sc.exe \\machinename STOP servicename
sc.exe \\machinename START servicename
 
Thanks for your quick answer!

I have been googleing (and exporeing) and have found the api startservice api. Code follows.....
<code>
Public Function StartMTSService(ComputerName As String, ServiceName As String) As Variant
Dim hSManager As Long
Dim hService As Long
hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
If hSManager <> 0 Then
hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
If hService <> 0 Then
StartMTSService = StartService(hService, SERVICE_START, SERVICE_INTERROGATE)
CloseServiceHandle hService
End If
CloseServiceHandle hSManager
End If
End Function
</code>

I can't get this to work, the part i think is incorrect is where it says "SERVICE_INTERROGATE".
Thanks in advance.

Thanks,
James
[afro][mad]
&quot;Make it idiot-proof and someone will make a better idiot.&quot; ~bumper sticker
 
Well, i figured it out after much googling, searching, and meditation.
Declares are overkill, this is because i also check the status of the service after i try to kill it, if it is still in the "running" state, then i try again. I found this code quickly to check status.
Here's what i have, for thoes who would like to do it later:
<code>
'API Constants
Public Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
' Service Control
Public Const SERVICE_CONTROL_STOP = &H1
Public Const SERVICE_CONTROL_PAUSE = &H2
' Service State - for CurrentState
Public Const SERVICE_STOPPED = &H1
Public Const SERVICE_START_PENDING = &H2
Public Const SERVICE_STOP_PENDING = &H3
Public Const SERVICE_RUNNING = &H4
Public Const SERVICE_CONTINUE_PENDING = &H5
Public Const SERVICE_PAUSE_PENDING = &H6
Public Const SERVICE_PAUSED = &H7
'Service Control Manager object specific access types
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
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 SC_MANAGER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or 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)
'Service object specific access types
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)

Const GENERIC_EXECUTE = &H20000000

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

Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) As Long
Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal hService As Long, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long 'found this myself

Public Function StartMTSService(ComputerName As String, ServiceName As String) As Variant
Dim hSManager As Long
Dim hService As Long
hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, GENERIC_EXECUTE)
If hSManager <> 0 Then
hService = OpenService(hSManager, ServiceName, GENERIC_EXECUTE)
If hService <> 0 Then
StartMTSService = StartService(hService, 0, 0)
CloseServiceHandle hService
End If
CloseServiceHandle hSManager
End If
End Function

</code>
Simple, No?

Have fun coding all, I am ;-D



Thanks,
James
[afro][mad]
&quot;Make it idiot-proof and someone will make a better idiot.&quot; ~bumper sticker
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top