|
| 1 | +Imports System.Runtime.InteropServices |
| 2 | + |
| 3 | +Public Class WinApi |
| 4 | + |
| 5 | + ' Everything needed to allow us to call the relevant Win32 APIs from .NET code. |
| 6 | + ' See links to native API definitions on each item for further details |
| 7 | + |
| 8 | + Public Const SERVICE_WIN32_OWN_PROCESS As UInteger = &H10 |
| 9 | + Public Const SERVICE_ERROR_NORMAL As UInteger = 1 |
| 10 | + Public Const SERVICE_AUTO_START As UInteger = 2 |
| 11 | + Public Const SERVICE_DEMAND_START As UInteger = 3 |
| 12 | + Public Const SERVICE_DISABLED As UInteger = 4 |
| 13 | + |
| 14 | + 'https://docs.microsoft.com/en-us/windows/win32/services/service-security-and-access-rights#access-rights-for-a-service |
| 15 | + <Flags()> _ |
| 16 | + Public Enum SERVICE_RIGHTS As UInteger |
| 17 | + SERVICE_QUERY_CONFIG = 1 |
| 18 | + SERVICE_CHANGE_CONFIG = 2 |
| 19 | + SERVICE_QUERY_STATUS = 4 |
| 20 | + SERVICE_ENUMERATE_DEPENDENTS = 8 |
| 21 | + SERVICE_START = &H10 |
| 22 | + SERVICE_STOP = &H20 |
| 23 | + SERVICE_INTERROGATE = &H80 |
| 24 | + SERVICE_USER_DEFINED_CONTROL = &H100 |
| 25 | + SERVICE_PAUSE_CONTINUE = &H40 |
| 26 | + SERVICE_ALL_ACCESS = &HF01FF |
| 27 | + ACCESS_SYSTEM_SECURITY = &H1000000 |
| 28 | + DELETE = &H10000 |
| 29 | + WRITE_DAC = &H40000 |
| 30 | + WRITE_OWNER = &H80000 |
| 31 | + SYNCHRONIZE = &H100000 |
| 32 | + End Enum |
| 33 | + |
| 34 | + 'https://docs.microsoft.com/en-us/windows/win32/services/service-security-and-access-rights |
| 35 | + <Flags()> _ |
| 36 | + Public Enum SCM_RIGHTS As UInteger |
| 37 | + SC_MANAGER_CONNECT = 1 |
| 38 | + SC_MANAGER_CREATE_SERVICE = 2 |
| 39 | + SC_MANAGER_ENUMERATE_SERVICE = 4 |
| 40 | + SC_MANAGER_LOCK = 8 |
| 41 | + SC_MANAGER_MODIFY_BOOT_CONFIG = &H20 |
| 42 | + SC_MANAGER_QUERY_LOCK_STATUS = &H10 |
| 43 | + SC_MANAGER_ALL_ACCESS = &HF003F |
| 44 | + DELETE = &H10000 |
| 45 | + WRITE_DAC = &H40000 |
| 46 | + WRITE_OWNER = &H80000 |
| 47 | + SYNCHRONIZE = &H100000 |
| 48 | + End Enum |
| 49 | + |
| 50 | + 'https://docs.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicew |
| 51 | + <DllImport("advapi32.dll", EntryPoint:="StartServiceW", SetLastError:=True)> _ |
| 52 | + Public Shared Function StartService(ByVal hService As IntPtr, ByVal dwNumServiceArgs As UInteger, _ |
| 53 | + ByVal lpServiceArgVectors As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean |
| 54 | + End Function |
| 55 | + |
| 56 | + 'https://docs.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-createservicew |
| 57 | + <DllImport("advapi32.dll", EntryPoint:="CreateServiceW", SetLastError:=True)> _ |
| 58 | + Public Shared Function CreateService(ByVal hSCManager As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpServiceName As String, _ |
| 59 | + <MarshalAs(UnmanagedType.LPWStr)> ByVal lpDisplayName As String, ByVal dwDesiredAccess As UInteger, ByVal dwServiceType As UInteger, ByVal dwStartType As UInteger, |
| 60 | + ByVal dwErrorControl As UInteger, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpBinaryPathName As String, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpLoadOrderGroup As String, _ |
| 61 | + ByVal lpdwTagId As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpDependencies As String, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpServiceStartName As String, _ |
| 62 | + <MarshalAs(UnmanagedType.LPWStr)> ByVal lpPassword As String) As IntPtr |
| 63 | + End Function |
| 64 | + |
| 65 | + 'https://docs.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-openscmanagerw |
| 66 | + <DllImport("advapi32.dll", EntryPoint:="OpenSCManagerW", SetLastError:=True)> _ |
| 67 | + Public Shared Function OpenSCManager(<MarshalAs(UnmanagedType.LPWStr)> ByVal lpMachineName As String, _ |
| 68 | + <MarshalAs(UnmanagedType.LPWStr)> ByVal lpDatabaseName As String, _ |
| 69 | + ByVal dwDesiredAccess As UInteger) As IntPtr |
| 70 | + End Function |
| 71 | + |
| 72 | + 'https://docs.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-closeservicehandle |
| 73 | + <DllImport("advapi32.dll", EntryPoint:="CloseServiceHandle", SetLastError:=True)> _ |
| 74 | + Public Shared Function CloseServiceHandle(ByVal hSCObject As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean |
| 75 | + End Function |
| 76 | + |
| 77 | + <DllImport("advapi32.dll", EntryPoint:="OpenServiceW", SetLastError:=True)> _ |
| 78 | + Public Shared Function OpenService(ByVal hSCManager As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpServiceName As String, _ |
| 79 | + ByVal dwDesiredAccess As UInteger) As IntPtr |
| 80 | + End Function |
| 81 | + |
| 82 | +End Class |
0 commit comments