1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
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 |