|
-
Sep 24th, 2008, 06:50 AM
#1
VB6 - ChangeServiceConfig API - Enable/Disable service, change service properties
I've been poking around on the net and I haven't been able to find a single working example of the ChangeServiceConfig API function for VB.
I found several old threads on VBF but they were all unresolved. So I decided to work on it a bit and I managed to create a working code.
What the example does is find the service by name, loads the current configuration and sets the Startup Type to Automatic (it's what I needed) while keeping other parameters unchanged.
This is what I needed it to do, but you can easily change the code to change any aspect of the services configuration.
The code will come in handy to those who need to start a service that has been disabled, because just starting it won't work unless you first change the Startup Type.
Code:
Option Explicit
'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)
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
'Settings service mode
Public Const SERVICE_ERROR_IGNORE = 0
Public Const SERVICE_ERROR_NORMAL = 1
Public Const SERVICE_ERROR_SEVERE = 2
Public Const SERVICE_ERROR_CRITICAL = 3
Public Const SERVICE_BOOT_START = 0
Public Const SERVICE_SYSTEM_START = 1
Public Const SERVICE_AUTO_START = 2
Public Const SERVICE_DEMAND_START = 3
Public Const SERVICE_DISABLED = 4
Public Const SERVICE_WIN32_OWN_PROCESS = &H10
Public Const SERVICE_WIN32_SHARE_PROCESS = &H20
Public Const SERVICE_INTERACTIVE_PROCESS = &H100
Public Const SERVICE_NO_CHANGE As String = &HFFFF
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 String, _
lpdwTagId As Long, _
ByVal lpDependencies As Long, _
ByVal lpServiceStartName As Long, _
ByVal lpPassword As Long, _
ByVal lpDisplayName As Long) As Long
Public Declare Function QueryServiceConfig _
Lib "advapi32.dll" Alias "QueryServiceConfigA" _
(ByVal hService As Long, _
lpServiceConfig As Any, _
ByVal cbBufSize As Long, _
pcbBytesNeeded As Long) As Long
Public Type QUERY_SERVICE_CONFIG_PTR
dwServiceType As Long
dwStartType As Long
dwErrorControl As Long
psBinaryPathName As Long
psLoadOrderGroup As Long
dwTagId As Long
psDependencies As Long
psServiceStartName As Long
psDisplayName As Long
End Type
Public Declare Sub CopyMem _
Lib "kernel32" Alias "RtlMoveMemory" _
(pTo As Any, _
pFrom As Any, _
ByVal lCount As Long)
Public Function ServiceAutoStart(ComputerName As String, ServiceName As String) As Long
Dim hSManager As Long
Dim hService As Long
Dim res As Long
Dim ptypServiceConfig As QUERY_SERVICE_CONFIG_PTR
Dim plngBufSize As Long
Dim pabytQuery() As Byte
Dim plngBytesNeeded As Long
Dim plngRtn 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
' Get the number of bytes needed
plngBufSize = 0
ReDim pabytQuery(0) As Byte
plngRtn = QueryServiceConfig(hService, _
pabytQuery(0), _
plngBufSize, _
plngBytesNeeded)
' Now do it with the right size
plngBufSize = plngBytesNeeded
ReDim pabytQuery(0 To plngBytesNeeded - 1) As Byte
plngRtn = QueryServiceConfig(hService, _
pabytQuery(0), _
plngBufSize, _
plngBytesNeeded)
CopyMem ptypServiceConfig, pabytQuery(0), Len(ptypServiceConfig)
res = ChangeServiceConfig(hService, _
ptypServiceConfig.dwServiceType, _
SERVICE_AUTO_START, _
ptypServiceConfig.dwErrorControl, _
ptypServiceConfig.psBinaryPathName, _
ptypServiceConfig.psLoadOrderGroup, _
ByVal ptypServiceConfig.dwTagId, _
ptypServiceConfig.psDependencies, _
ptypServiceConfig.psServiceStartName, _
&O0, _
ptypServiceConfig.psDisplayName)
ServiceAutoStart = res
CloseServiceHandle hService
End If
CloseServiceHandle hSManager
End If
End Function
P.S: To Vista/W7 users, code needs Admin rights to work.
Last edited by baja_yu; May 15th, 2010 at 06:47 PM.
Reason: Thanks to Martin for moving the thread
-
Sep 24th, 2008, 07:03 AM
#2
Re: [RESOLVED] ChangeServiceConfig - Enable/Disable service
Sounds good, I'll have to take it for a spin when I get a chance, thanks for sharing!
-
May 15th, 2010, 06:40 PM
#3
Re: [RESOLVED] ChangeServiceConfig - Enable/Disable service
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|