Skip to content

Commit e145e0c

Browse files
authored
Add files via upload
1 parent e37d6dd commit e145e0c

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

Program.vb

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Imports System.Runtime.InteropServices
2+
Imports System.ComponentModel
3+
4+
Module Program
5+
6+
Sub Main()
7+
8+
If My.Application.CommandLineArgs.Count < 2 Then
9+
Console.WriteLine("Usage:" & Environment.NewLine &
10+
"ServiceInstallTest.exe ServiceName ExePath")
11+
Exit Sub
12+
End If
13+
' Get command line args
14+
Dim ServiceName As String = My.Application.CommandLineArgs(0)
15+
Dim ExePath As String = My.Application.CommandLineArgs(1)
16+
Dim ScmHandle As IntPtr = IntPtr.Zero
17+
Dim ServiceHandle As IntPtr = IntPtr.Zero
18+
19+
Try
20+
' Connect to SCM (Service Control Manager) and get a handle that allows us to create services by specifying SC_MANAGER_CREATE_SERVICE.
21+
' Would fail with access denied here if we don't have permission to create services)
22+
ScmHandle = WinApi.OpenSCManager(Nothing, Nothing, WinApi.SCM_RIGHTS.SC_MANAGER_CREATE_SERVICE)
23+
If ScmHandle = IntPtr.Zero Then
24+
Throw New Win32Exception()
25+
End If
26+
27+
Try
28+
' Create a new service and get back a handle to the service (that can then be used with StartService function and other service functions).
29+
' We specify SERVICE_ALL_ACCESS so that this handle allows us full control over the service (to start it, stop it, delete it, etc) and for some
30+
' reason this works. If we try to request this level of access afterwards by using the OpenService function, it fails with access denied
31+
ServiceHandle = WinApi.CreateService(ScmHandle,
32+
ServiceName,
33+
Nothing,
34+
WinApi.SERVICE_RIGHTS.SERVICE_ALL_ACCESS,
35+
WinApi.SERVICE_WIN32_OWN_PROCESS,
36+
WinApi.SERVICE_DEMAND_START,
37+
WinApi.SERVICE_ERROR_NORMAL,
38+
ExePath,
39+
Nothing, Nothing, Nothing, "NT Authority\System", Nothing)
40+
41+
' If we successfully created the service, attempt to start it using the handle we got back from CreateService.
42+
If Not ServiceHandle = IntPtr.Zero Then
43+
If WinApi.StartService(ServiceHandle, 0, IntPtr.Zero) Then
44+
Console.Write("Service created and started successfully")
45+
Else
46+
Throw New Exception("Service has been created but was unable to start due to error: " & New System.ComponentModel.Win32Exception().Message)
47+
End If
48+
Else
49+
Throw New Exception("Unable to create service due to error: " & New System.ComponentModel.Win32Exception().Message)
50+
End If
51+
52+
Finally
53+
If Not ServiceHandle = IntPtr.Zero Then
54+
WinApi.CloseServiceHandle(ServiceHandle)
55+
End If
56+
End Try
57+
Catch ex As Exception
58+
Console.WriteLine("Error: " & ex.Message)
59+
Finally
60+
If Not ScmHandle = IntPtr.Zero Then
61+
WinApi.CloseServiceHandle(ScmHandle)
62+
End If
63+
End Try
64+
End Sub
65+
66+
67+
End Module

WinApi.vb

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)