-
Change NT service config (disabled -> enabled)
Hi,
I am trying to change NT service config, for instance enable it when it's
disabled. I don't have any problems starting, stopping services.
If anyone could tell why the code below don't work I'd really appreciate
this.
ChangeServiceConf - doesn't seem to do anything,
API call returns 0
GetLastError return empty
ChangeServiceConfig(hService, SERVICE_WIN32, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
vbNullString, vbNullString, &H0, vbNullString, vbNullString, vbNullString,
"PaymentExpress")
------snap------------------
'Constants*************************************************************************
'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)
Public Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
Public Const SERVICE_ACTIVE = &H1
Public Const SERVICE_INACTIVE = &H2
Public Const SERVICE_STATE_ALL = (SERVICE_ACTIVE Or SERVICE_INACTIVE)
Public Const SERVICE_WIN32_OWN_PROCESS = &H10
Public Const SERVICE_WIN32_SHARE_PROCESS = &H20
Public Const SERVICE_WIN32 = SERVICE_WIN32_OWN_PROCESS + SERVICE_WIN32_SHARE_PROCESS
Public Const SERVICE_KERNEL_DRIVER = &H1
Public Const SERVICE_FILE_SYSTEM_DRIVER = &H2
Public Const SERVICE_RECOGNIZER_DRIVER = &H8
Public Const SERVICE_ADAPTER = &H4
Public Const SERVICE_INTERACTIVE_PROCESS = &H100
Public Const SERVICE_DRIVER = SERVICE_KERNEL_DRIVER Or SERVICE_FILE_SYSTEM_DRIVER
Or SERVICE_RECOGNIZER_DRIVER
'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)
'Error Constants
Private Const ERROR_ACCESS_DENIED = 5&
Private Const ERROR_INVALID_HANDLE = 6&
Private Const ERROR_INVALID_PARAMETER = 87
Private Const ERROR_MORE_DATA = 234
Private Const ERROR_DATABASE_DOES_NOT_EXIST = 1065&
Private Const ERROR_INSUFFICIENT_BUFFER = 122
Public Const ERROR_SERVICE_SPECIFIC_ERROR = 1066&
'Status Constants
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
'Accepted Controls Constants
Public Const SERVICE_ACCEPT_STOP = &H1
Public Const SERVICE_ACCEPT_PAUSE_CONTINUE = &H2
Public Const SERVICE_ACCEPT_SHUTDOWN = &H4
'Control Constants
Public Const SERVICE_CONTROL_STOP = &H1
Public Const SERVICE_CONTROL_PAUSE = &H2
Public Const SERVICE_CONTROL_CONTINUE = &H3
Public Const SERVICE_ERROR_IGNORE = &H0
Public Const SERVICE_ERROR_NORMAL = &H1
Public Const SERVICE_ERROR_SEVERE = &H2
Public Const SERVICE_ERROR_CRITICAL = &H3
'StartType Constants
Public Const SERVICE_BOOT_START = &H0
Public Const SERVICE_SYSTEM_START = &H1
Public Const SERVICE_AUTO_START = &H2
Public Const SERVICE_DEMAND_START = &H3
Public Const SERVICE_DISABLED = &H4
Public Const SC_GROUP_IDENTIFIER = "+"
'Type Declarations*****************************************************************
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
Private Type ENUM_SERVICE_STATUS
lpServiceName As Long
lpDisplayName As Long
ServiceStatus As SERVICE_STATUS
End Type
Type QUERY_SERVICE_CONFIG
dwServiceType As Long
dwStartType As Long
dwErrorControl As Long
lpBinaryPathName As Long
lpLoadOrderGroup As Long
dwTagId As Long
lpDependencies As Long
lpServiceStartName As Long
lpDisplayName As Long
End Type
Public Type ListOfServices
bInit As Boolean
lCount As Long
lLastErr As Long
sErrMessage As String
List() As ENUM_SERVICE_STATUS
End Type
'API Declarations******************************************************************
Private Declare Function EnumServicesStatus Lib "advapi32.dll" Alias "EnumServicesStatusA"
(ByVal hSCManager As Long, ByVal dwServiceType As Long, ByVal dwServiceState
As Long, lpServices As Any, ByVal cbBufSize As Long, pcbBytesNeeded As Long,
lpServicesReturned As Long, lpResumeHandle As Long) As Long
Private Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA"
(ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess
As Long) As Long
Private Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA"
(ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess
As Long) As Long
Private Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA"
(ByVal hService As Long, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors
As Long) As Long
Private Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject
As Long) As Long
Private Declare Function QueryServiceConfig Lib "advapi32.dll" Alias "QueryServiceConfigA"
(ByVal hService As Long, lpServiceConfig As Byte, ByVal cbBufSize As Long,
pcbBytesNeeded As Long) As Long
Private Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal hService
As Long, lpServiceStatus As SERVICE_STATUS) As Long
Private Declare Function EnumDependentServices Lib "advapi32.dll" Alias "EnumDependentServicesA"
(ByVal hService As Long, ByVal dwServiceState As Long, lpServices As Any,
ByVal cbBufSize As Long, pcbBytesNeeded As Long, lpServicesReturned As Long)
As Long
Private Declare Function ControlService Lib "advapi32.dll" (ByVal hService
As Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long
Private 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 String, ByVal
lpLoadOrderGroup As String, lpdwTagId As Long, ByVal lpDependencies As String,
ByVal lpServiceStartName As String, ByVal lpPassword As String, ByVal lpDisplayName
As String) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pTo As
Any, uFrom As Any, ByVal lSize As Long)
Public Declare Function lStringCopy Lib "kernel32" Alias "lstrcpyA" (ByVal
lpString1 As String, ByVal lpString2 As Long) As Long
Private Declare Function lStringLength Lib "kernel32" Alias "lstrlenW" (ByVal
lpString As Long) As Long
Public colDepServices As New Collection
Public colServices As New Collection
Public Function ChangeServiceConf(ComputerName As String, ServiceName As
String) As Boolean
Dim ServiceStatus As SERVICE_STATUS
Dim hSManager As Long
Dim hService As Long
Dim res As Long
Dim buffer() As Byte
ReDim buffer(512)
DoEvents
Call ServiceStop(ComputerName, ServiceName)
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
res = ChangeServiceConfig(hService, SERVICE_WIN32, SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL, vbNullString, vbNullString, &H0, vbNullString, vbNullString,
vbNullString, "PaymentExpress")
Debug.Print GetLastError
CloseServiceHandle hService
End If
CloseServiceHandle hSManager
End If
End Function
Public Sub ServiceStop(ComputerName As String, ServiceName As String)
Dim ServiceStatus As SERVICE_STATUS
Dim hSManager As Long
Dim hService As Long
Dim res As Long
DoEvents
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
res = ControlService(hService, SERVICE_CONTROL_STOP, ServiceStatus)
CloseServiceHandle hService
End If
CloseServiceHandle hSManager
End If
End Sub
------snap------------------
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks