Jalankan Program Dengan Services

Option Explicit
Private 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 Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" _
(ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal _
dwDesiredAccess As Long) As Long

Declare Function ControlService Lib "advapi32.dll" (ByVal hService As Long, _
ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long

Private 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 lpServiceStartName As String, ByVal lpPassword _
As String) 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

Private Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal _
hSCObject As Long) As Long

Public Function Install_SVC(strServiceFileName As String, strServiceName As _
String, strDisplayName As String, bolInteractive As Boolean, bolAutoStart As _
Boolean, Optional GroupName As String = "", Optional strMachineName As _
Variant, Optional strAccount As Variant, Optional strAccountPassword As _
Variant) As Boolean

On Error Resume Next
Dim hSCM As Long
Dim hSVC As Long
Dim lngInteractive As Long
Dim lngAutoStart As Long
Dim pSTATUS As SERVICE_STATUS
If bolInteractive = True Then _
lngInteractive = (&H100 Or &H10) Else lngInteractive = &H10
If bolAutoStart = True Then lngAutoStart = &H2 Else lngAutoStart = &H3
If IsMissing(strMachineName) = True Then _
strMachineName = vbNullString Else strMachineName = CStr(strMachineName)
If IsMissing(strAccount) = True Then _
strAccount = vbNullString Else strAccount = CStr(strAccount)
If IsMissing(strAccountPassword) = True Then _
strAccountPassword = vbNullString Else _
strAccountPassword = CStr(strAccountPassword)

hSCM = OpenSCManager(strMachineName, vbNullString, &H2)
If hSCM = 0 Then Exit Function '// gagal membuat
'// Install service
hSVC = CreateService(hSCM, strServiceName, strDisplayName, 983551, _
lngInteractive, lngAutoStart, 0, strServiceFileName, GroupName, _
vbNull, vbNullString, strAccount, strAccountPassword)
If hSVC <> 0 Then Install_SVC = True
Call CloseServiceHandle(hSVC)
Call CloseServiceHandle(hSCM)
'StartService
End Function

No comments:

Post a Comment