Imports System.Runtime.InteropServices
Namespace Tools
Public Class ServiceWrapper
<DllImport("advapi32.dll", EntryPoint:="OpenSCManagerW", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function OpenSCManager(ByVal lpMachingName As String, _
ByVal lpDatabaseName As String, _
ByVal dwDesiredAccess As SCM_ACCESS) As IntPtr
End Function
<DllImport("advapi32.dll", EntryPoint:="OpenServiceW", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function OpenService(ByVal SC_HANDLE As IntPtr, _
ByVal strServiceName As String, _
ByVal dwDesiredAccess As SERVICE_ACCESS) As IntPtr
End Function
<DllImport("advapi32.dll", EntryPoint:="CloseServiceHandle", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function CloseServiceHandle(ByVal SC_HANDLE As IntPtr) As Boolean
End Function
<DllImport("advapi32.dll", EntryPoint:="QueryServiceConfigW", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function QueryServiceConfig(ByVal SC_HANDLE As IntPtr, _
ByVal pBuffer As IntPtr, _
ByVal cbBufSize As Int32, _
ByRef pcbBytesNeeded As Int32) As Boolean
End Function
<DllImport("advapi32.dll", EntryPoint:="QueryServiceConfig2W", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function QueryServiceConfig2(ByVal SC_HANDLE As IntPtr, _
ByVal dwInfoLevel As Int32, _
ByVal pBuffer As IntPtr, _
ByVal cbBufSize As Int32, _
ByRef pcbBytesNeeded As Int32) As Boolean
End Function
<DllImport("advapi32.dll", EntryPoint:="CreateServiceW", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function CreateService(ByVal SC_HANDLE As IntPtr, _
ByVal lpServiceName As String, _
ByVal lpDisplayName As String, _
ByVal dwDesiredAccess As SERVICE_ACCESS, _
ByVal dwServiceType As SERVICE_TYPE, _
ByVal dwStartType As SERVICE_START_TYPE, _
ByVal dwErrorControl As SERVICE_ERROR_CONTROL, _
ByVal lpBinaryPathName As String, _
ByVal lpLoadOrderGroup As String, _
ByVal lpdwTagId As Int32, _
ByVal lpDependencies As String, _
ByVal lpServiceUserName As String, _
ByVal lpPassword As String) As IntPtr
End Function
<DllImport("advapi32.dll", EntryPoint:="DeleteService", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function DeleteService(ByVal SC_HANDLE As IntPtr) As Boolean
End Function
<DllImport("advapi32.dll", entryPoint:="ChangeServiceConfigW", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function ChangeServiceConfig(ByVal SC_HANDLE As IntPtr, _
ByVal dwServiceType As SERVICE_TYPE, _
ByVal dwStartType As SERVICE_START_TYPE, _
ByVal dwErrorControl As SERVICE_ERROR_CONTROL, _
ByVal lpBinaryPathName As String, _
ByVal lpLoadOrderGroup As String, _
ByVal lpdwTagId As Int32, _
ByVal lpDependencies As String, _
ByVal lpServiceStartName As String, _
ByVal lpPassword As String, _
ByVal lpDisplayName As String) As Boolean
End Function
<DllImport("advapi32.dll", entryPoint:="ChangeServiceConfig2W", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function ChangeServiceConfig2(ByVal SC_HANDLE As IntPtr, _
ByVal dwInfoLevel As Int32, _
ByVal lpInfo As IntPtr) As Boolean
End Function
Private Enum PVT_WIN32_ERRORS As Int32
ERROR_SUCCESS = &H0
ERROR_ACCESS_DENIED = &H5
ERROR_INVALID_HANDLE = &H6
ERROR_INVALID_PARAMETER = &H57
ERROR_INSUFFICIENT_BUFFER = &H7A
ERROR_INVALID_NAME = &H7B
ERROR_INVALID_SERVICE_ACCOUNT = &H421
ERROR_CIRCULAR_DEPENDENCY = &H423
ERROR_SERVICE_DOES_NOT_EXIST = &H424
ERROR_DATABASE_DOES_NOT_EXIST = &H429
ERROR_SERVICE_EXISTS = &H431
ERROR_DUPLICATE_SERVICE_NAME = &H436
ERROR_SERVICE_MARKED_FOR_DELETE = &H430
End Enum
Private Enum SCM_ACCESS As Int32
SC_MANAGER_CONNECT = &H1
SC_MANAGER_CREATE_SERVICE = &H2
SC_MANAGER_ENUMERATE_SERVICE = &H4
SC_MANAGER_LOCK = &H8
SC_MANAGER_QUERY_LOCK_STATUS = &H10
SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
STANDARD_RIGHTS_REQUIRED = &HF000
GENERIC_WRITE = &H40000000
SC_MANAGER_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED And SC_MANAGER_CONNECT And SC_MANAGER_CREATE_SERVICE And SC_MANAGER_ENUMERATE_SERVICE And SC_MANAGER_LOCK And SC_MANAGER_QUERY_LOCK_STATUS And SC_MANAGER_MODIFY_BOOT_CONFIG
End Enum
Private Enum SERVICE_ACCESS As Int32
SERVICE_QUERY_CONFIG = &H1
SERVICE_CHANGE_CONFIG = &H2
SERVICE_QUERY_STATUS = &H4
SERVICE_ENUMERATE_DEPENDENTS = &H8
SERVICE_START = &H10
SERVICE_STOP = &H20
SERVICE_PAUSE_CONTINUE = &H40
SERVICE_INTERROGATE = &H80
SERVICE_USER_DEFINED_CONTROL = &H100
SERVICE_DELETE = &H10000
SERVICE_ALL_RIGHTS = &HF01FF
End Enum
Public Enum SERVICE_TYPE As Int32
SERVICE_KERNEL_DRIVER = &H1
SERVICE_FILE_SYSTEM_DRIVER = &H2
SERVICE_WIN32_OWN_PROCESS = &H10
SERVICE_WIN32_SHARE_PROCESS = &H20
SERVICE_INTERACTIVE_PROCESS = &H100
SERVICE_NO_CHANGE = &HFFFFFFFF
End Enum
Public Enum SERVICE_START_TYPE As Int32
SERVICE_BOOT_START = &H0
SERVICE_SYSTEM_START = &H1
SERVICE_AUTO_START = &H2
SERVICE_DEMAND_START = &H3
SERVICE_DISABLED = &H4
SERVICE_NO_CHANGE = &HFFFFFFFF
End Enum
Public Enum SERVICE_ERROR_CONTROL As Int32
SERVICE_ERROR_IGNORE = &H0
SERVICE_ERROR_NORMAL = &H1
SERVICE_ERROR_SEVERS = &H2
SERVICE_ERROR_CRITICAL = &H3
SERVICE_NO_CHANGE = &HFFFFFFFF
End Enum
<StructLayout(LayoutKind.Sequential)> _
Private Structure SERVICE_DESCRIPTION
<MarshalAs(UnmanagedType.LPTStr)> Dim lpDescription As String
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure QUERY_SERVICE_CONFIG
Dim dwServiceType As SERVICE_TYPE
Dim dwStartType As SERVICE_START_TYPE
Dim dwErrorControl As SERVICE_ERROR_CONTROL
<MarshalAs(UnmanagedType.LPTStr)> Dim lpBinaryPathName As String
<MarshalAs(UnmanagedType.LPTStr)> Dim lpLoadOrderGroup As String
Dim dwTagId As Int32
<MarshalAs(UnmanagedType.LPTStr)> Dim lpDependencies As String
<MarshalAs(UnmanagedType.LPTStr)> Dim lpServiceStartName As String
<MarshalAs(UnmanagedType.LPTStr)> Dim lpDisplayName As String
End Structure
Private Const SERVICE_CONFIG_DESCRIPTION As Int32 = 1
Public Function InstallService(ByVal ServiceName As String, ByVal DisplayName As String, ByVal CommandLine As String, Optional ByVal ServiceType As SERVICE_TYPE = SERVICE_TYPE.SERVICE_WIN32_OWN_PROCESS, Optional ByVal StartType As SERVICE_START_TYPE = SERVICE_START_TYPE.SERVICE_DEMAND_START, Optional ByVal Username As String = Nothing, Optional ByVal Password As String = Nothing) As Boolean
Debug.Print("InstallService")
Dim retval As Boolean = True
Dim hSCManager As IntPtr = OpenSCManager(Nothing, Nothing, SCM_ACCESS.SC_MANAGER_CREATE_SERVICE)
If hSCManager = 0 Then
Debug.WriteLine(" OpenSCManager returned " & Marshal.GetLastWin32Error.ToString)
retval = False
Else
Dim hService As IntPtr = CreateService(hSCManager, ServiceName, DisplayName, SERVICE_ACCESS.SERVICE_ALL_RIGHTS, ServiceType, StartType, SERVICE_ERROR_CONTROL.SERVICE_ERROR_NORMAL, CommandLine, Nothing, Nothing, Nothing, Username, Password)
If hService = 0 Then
Debug.WriteLine(" CreateService returned " & Marshal.GetLastWin32Error.ToString)
retval = False
Else
CloseServiceHandle(hService)
End If
CloseServiceHandle(hSCManager)
End If
Return retval
End Function
Public Function UninstallService(ByVal ServiceName As String) As Boolean
Debug.WriteLine("UninstallService")
Dim retval As Boolean = True
Dim hSCManager As IntPtr = OpenSCManager(Nothing, Nothing, SCM_ACCESS.SC_MANAGER_ALL_ACCESS)
If hSCManager = 0 Then
Debug.WriteLine(" OpenSCManager returned " & Marshal.GetLastWin32Error.ToString)
retval = False
Else
Dim hService As IntPtr = OpenService(hSCManager, ServiceName, SERVICE_ACCESS.SERVICE_ALL_RIGHTS)
If hService = 0 Then
Debug.WriteLine(" OpenService returned " & Marshal.GetLastWin32Error.ToString)
retval = False
Else
If DeleteService(hService) = False Then
Debug.WriteLine(" DeleteService returned " & Marshal.GetLastWin32Error.ToString)
retval = False
End If
CloseServiceHandle(hService)
End If
CloseServiceHandle(hSCManager)
End If
Return retval
End Function
Public Function GetServiceDescription(ByVal ServiceName As String) As String
Debug.WriteLine("GetServiceDescription")
Dim retval As String = ""
Dim hSCManager As IntPtr = OpenSCManager(Nothing, Nothing, SCM_ACCESS.SC_MANAGER_ALL_ACCESS)
Dim hService As IntPtr = OpenService(hSCManager, ServiceName, SERVICE_ACCESS.SERVICE_QUERY_CONFIG)
Dim hBuffer As IntPtr
Dim iBytesNeeded As Integer = 0
QueryServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, hBuffer, iBytesNeeded, iBytesNeeded)
hBuffer = Marshal.AllocHGlobal(iBytesNeeded)
QueryServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, hBuffer, iBytesNeeded, iBytesNeeded)
Dim sdInfo As SERVICE_DESCRIPTION = Marshal.PtrToStructure(hBuffer, GetType(SERVICE_DESCRIPTION))
retval = sdInfo.lpDescription
Marshal.FreeHGlobal(hBuffer)
CloseServiceHandle(hService)
CloseServiceHandle(hSCManager)
Return retval
End Function
Public Function GetServiceConfig(ByVal ServiceName As String) As QUERY_SERVICE_CONFIG
Dim retval As QUERY_SERVICE_CONFIG
Dim hSCManager As IntPtr = OpenSCManager(Nothing, Nothing, SCM_ACCESS.SC_MANAGER_ALL_ACCESS)
Dim hService As IntPtr = OpenService(hSCManager, ServiceName, SERVICE_ACCESS.SERVICE_CHANGE_CONFIG)
Dim hBuffer As IntPtr
Dim iBytesNeeded As Integer = 0
QueryServiceConfig(hService, hBuffer, iBytesNeeded, iBytesNeeded)
hBuffer = Marshal.AllocHGlobal(iBytesNeeded)
QueryServiceConfig(hService, hBuffer, iBytesNeeded, iBytesNeeded)
retval = Marshal.PtrToStructure(hBuffer, GetType(QUERY_SERVICE_CONFIG))
Marshal.FreeHGlobal(hBuffer)
CloseServiceHandle(hService)
CloseServiceHandle(hSCManager)
Return retval
End Function
Public Sub SetServiceDescription(ByVal ServiceName As String, ByVal SetDescription As String)
Dim hSCManager As IntPtr = OpenSCManager(Nothing, Nothing, SCM_ACCESS.SC_MANAGER_ALL_ACCESS)
Dim hService As IntPtr = OpenService(hSCManager, ServiceName, SERVICE_ACCESS.SERVICE_CHANGE_CONFIG)
Dim sdInfo As SERVICE_DESCRIPTION
sdInfo.lpDescription = SetDescription
Dim hBuffer As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(sdInfo))
Marshal.StructureToPtr(sdInfo, hBuffer, False)
ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, hBuffer)
Marshal.FreeHGlobal(hBuffer)
CloseServiceHandle(hService)
CloseServiceHandle(hSCManager)
End Sub
Public Sub SetServiceLogon(ByVal ServiceName As String, Optional ByVal NewUsername As String = Nothing, Optional ByVal NewPassword As String = Nothing)
Dim hSCManager As IntPtr = OpenSCManager(Nothing, Nothing, SCM_ACCESS.SC_MANAGER_ALL_ACCESS)
Dim hService As IntPtr = OpenService(hSCManager, ServiceName, SERVICE_ACCESS.SERVICE_CHANGE_CONFIG)
ChangeServiceConfig(hService, SERVICE_TYPE.SERVICE_NO_CHANGE, SERVICE_START_TYPE.SERVICE_NO_CHANGE, SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE, Nothing, Nothing, Nothing, Nothing, NewUsername, NewPassword, Nothing)
CloseServiceHandle(hService)
CloseServiceHandle(hSCManager)
End Sub
Public Sub SetServiceDisplayName(ByVal ServiceName As String, ByVal NewDisplayName As String)
Dim hSCManager As IntPtr = OpenSCManager(Nothing, Nothing, SCM_ACCESS.SC_MANAGER_ALL_ACCESS)
Dim hService As IntPtr = OpenService(hSCManager, ServiceName, SERVICE_ACCESS.SERVICE_CHANGE_CONFIG)
ChangeServiceConfig(hService, SERVICE_TYPE.SERVICE_NO_CHANGE, SERVICE_START_TYPE.SERVICE_NO_CHANGE, SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, NewDisplayName)
CloseServiceHandle(hService)
CloseServiceHandle(hSCManager)
End Sub
Public Sub SetServiceType(ByVal ServiceName As String, ByVal NewType As SERVICE_TYPE)
Dim hSCManager As IntPtr = OpenSCManager(Nothing, Nothing, SCM_ACCESS.SC_MANAGER_ALL_ACCESS)
Dim hService As IntPtr = OpenService(hSCManager, ServiceName, SERVICE_ACCESS.SERVICE_CHANGE_CONFIG)
ChangeServiceConfig(hService, NewType, SERVICE_START_TYPE.SERVICE_NO_CHANGE, SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
CloseServiceHandle(hService)
CloseServiceHandle(hSCManager)
End Sub
Public Sub SetServiceStartType(ByVal ServiceName As String, ByVal NewStartType As SERVICE_START_TYPE)
Dim hSCManager As IntPtr = OpenSCManager(Nothing, Nothing, SCM_ACCESS.SC_MANAGER_ALL_ACCESS)
Dim hService As IntPtr = OpenService(hSCManager, ServiceName, SERVICE_ACCESS.SERVICE_CHANGE_CONFIG)
ChangeServiceConfig(hService, SERVICE_TYPE.SERVICE_NO_CHANGE, NewStartType, SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
CloseServiceHandle(hService)
CloseServiceHandle(hSCManager)
End Sub
Public Sub SetServiceCommandLine(ByVal ServiceName As String, ByVal NewCommandline As String)
Dim hSCManager As IntPtr = OpenSCManager(Nothing, Nothing, SCM_ACCESS.SC_MANAGER_ALL_ACCESS)
Dim hService As IntPtr = OpenService(hSCManager, ServiceName, SERVICE_ACCESS.SERVICE_CHANGE_CONFIG)
ChangeServiceConfig(hService, SERVICE_TYPE.SERVICE_NO_CHANGE, SERVICE_START_TYPE.SERVICE_NO_CHANGE, SERVICE_ERROR_CONTROL.SERVICE_NO_CHANGE, NewCommandLine, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
CloseServiceHandle(hService)
CloseServiceHandle(hSCManager)
End Sub
End Class
End Namespace