Install Windows NT Service

By | 2002-06-01

Install a Windows NT service on a local or remote server. Configure how the service is installed. Requires Windows NT and administrator rights.

Original Author: Matthew Ruffell

Inputs

ServiceFileName [string] = binary service file path and name, ServiceName [string] = name of service, DisplayName [string] = unofficial name of service, Interactive [boolean] = communicates with desktop, AutoStart [boolean] = run when system starts, MachineName [optional string] = target server name

Assumptions

Requires Windows NT and administrator rights.

Returns

Returns True if serive was successfully installed.

API Declarations

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
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
Private Declare Function CloseServiceHandle Lib “advapi32.dll” (ByVal hSCObject As Long) As Long

Code

Public Function Install_SVC(strServiceFileName As String, strServiceName As String, strDisplayName As String, bolInteractive As Boolean, bolAutoStart As Boolean, Optional strMachineName As Variant, Optional strAccount As Variant, Optional strAccountPassword As Variant) As Boolean
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)

'// Open the service manager
hSCM = OpenSCManager(strMachineName, vbNullString, &H2)
If hSCM = 0 Then Exit Function '// error opening
'// Install the service
hSVC = CreateService(hSCM, _
strServiceName, _
strDisplayName, _
983551, _
lngInteractive, _
lngAutoStart, _
0, _
strServiceFileName, _
vbNull, _
vbNull, _
vbNullString, _
strAccount, _
strAccountPassword)

If hSVC <> 0 Then Install_SVC = True

Call CloseServiceHandle(hSVC)
Call CloseServiceHandle(hSCM)
End Function

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.