Click here to Skip to main content
15,298,916 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ı try write Windows service controller in vb
but ı have some problem in compile code
if ı compile code and start service process after process not working because
this service process need permisison and ı didnt solve this problem
how can ı fix this permisison problem ?
my working system is:windows 10
my ide is:visual studio 2015

What I have tried:

VB
Dim parts() As String = Split(My.User.Name, "\")
Dim username As String = parts(1)
Dim servicename As String = "Spooler"
Dim comm As Integer = "128"
Dim Testservicechk As ServiceControllerPermission = New ServiceControllerPermission(ServiceControllerPermissionAccess.Control, username, servicename)
Testservicechk.Assert()
Dim Testservice As System.ServiceProcess.ServiceController = New System.ServiceProcess.ServiceController(servicename, username)
Testservice.ExecuteCommand(comm)
Posted
Updated 19-Feb-21 18:10pm
v3

1 solution

You have to run your code as an administrator. Normal users cannot stop/start services.

How to force a C# based WinForms applications to run with administrator rights on any environment? | Our Code World[^]
   
Comments
Dark Frost 20-Feb-21 10:01am
   
i am already using admin request,but i still get Permission Access error
Dave Kreskowiak 20-Feb-21 11:37am
   
Is this happening while running the code under the debugger? Did you launch Visual Studio as an admin (right-click the shortcut, "Run as administrator"?
Dave Kreskowiak 20-Feb-21 11:59am
   
Looking at the code, You've got some strange things in there.
Dim comm As Integer = "128"

Remove the quotes around 128.
...= New System.ServiceProcess.ServiceController(servicename, username)

The first string you pass to ServiceController should be name of the service you want to work with. In your case, servicename is good. However, the second paramter you are passing is a username, instead of what it expects, a machine name. Remove the ", username" portion of the code. It should be
= New System.ServiceProcess.ServiceController(servicename)
Dark Frost 20-Feb-21 16:53pm
   
username needed because
this function should be called like this
System.ServiceProcess.ServiceController(name As String,machineName As String)
name = service name
nachineName = your machine name
sorry Fuction not working again :(
Dave Kreskowiak 20-Feb-21 17:52pm
   
Yeah but your not supplying a machine name as the second parameter. You're supplying a username. A ServiceController constructor never accepts a username as an argument.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900