Добрый день.
При необходимости создать сервис был написан тестовый код:
'#MODULE
Public Declare Function CreateService Lib "advapi32.dll" _
Alias "CreateServiceA" _
(ByVal hSCManager As Long, _
ByVal lpServiceName As String, _
ByVal lpDisplayName As String, _
ByVal dwDesiredAccess 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 lp As String, _
ByVal lpPassword As String) As Long
Public Declare Function GetLastError Lib "kernel32" () As Long
Public lngHSrv 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 Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
Public Const WRITE_OWNER = &H80000
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const SC_MANAGER_CONNECT = &H1
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_CREATE_SERVICE = &H2
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
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 ACCESS_SYSTEM_SECURITY = &H1000000
Public Const SERVICE_WIN32_OWN_PROCESS As Long = &H10
Public Const SERVICE_SYSTEM_START As Long = &H1
Public Const SERVICE_ERROR_NORMAL As Long = &H1
'#FORM
Private Sub Command1_Click()
Dim lngHSrv As Long
Dim lngRetCreSrv As Long
lngHsrc = OpenSCManager(vbNullString, vbNullString, _
SC_MANAGER_CREATE_SERVICE)
If lngHsrc <> 0 Then
lngRetCreSrv = CreateService(lngHsrc, _
"MySrv", _
"My first service", _
ACCESS_SYSTEM_SECURITY, _
SERVICE_WIN32_OWN_PROCESS, _
SERVICE_SYSTEM_START, _
SERVICE_ERROR_NORMAL, _
"c:\winnt\notepad.exe", _
vbNullString, _
vbNull, _
vbNullString, _
vbNullString, _
vbNullString)
End If
End Sub
Результатом выполнения функции CreateService стало нулевое значение, т.е. lngRetCreSrv = 0. Согласно MSDN это означает, что сервис не создался. GetLastError возвращает тоже 0.
Подскажите, пожалуйста, почему не создается сервис?