Avatar of CraigLazar
CraigLazar
 asked on

.net stop service command Access Denied error

Hi i am using vb.net and i am battling to stop a service from an exe. (win7)
i get the following error Failed 5 access is denied.
i have tried the following to try and stop it
1. sc.exe stop cpda
2.Using WMI call
   Dim oWMI
   Dim colItems
   oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
   colItems = oWMI.ExecQuery("Select * from Win32_Service Where Name ='CPDA'")
   For Each oWMI In colItems
      oWMI.StopService()
   Next
3. net stop cpda

if i open services and stop it manually it works with no security warning.
Please if possible do not just post a web link
thanks
C#.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
CraigLazar

8/22/2022 - Mon
MichaelMH

Here is pseudo sample code about how you could start/stop a window service using VB.NET:

Imports System.ServiceProcess

dim controller as new ServiceController

controller.MachineName = "."
controller.ServiceName = "IISADMIN"
dim status as string = controller.Status.ToString

' Stop the service
controller.Stop()

' Start the service
controller.Start()

Open in new window

CraigLazar

ASKER
hi,
thanks for the code, however i am getting an error on the
dim controller as new ServiceController

"type not defined"
MichaelMH

Did you used the import statement: Imports System.ServiceProcess?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
MichaelMH

Also be sure that you have System.ServiceProcess DLL in your project references.
CraigLazar

ASKER
Hi,
Ok i added the reference to the project, and it is not working, i am getting an innerexception now with the same problem, Access Denied.
MichaelMH

Under what account are you running your application? If the Windows account which you are using has restricted rights, then this issue will persists. So, check the rights of your account.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
CraigLazar

ASKER
Hi,
i thought as much, my user has admin rights, the strange thing is that if i run the command sc.exe stop cpda from the command prompt it works no problem. with no errors. Its just when i try an run the code in my app it stops.
MichaelMH

What OS do you have? Check if you need to run the program as administrator.
CraigLazar

ASKER
Hi, yip that's it. I compiled a test app. Ran it with admin privileges and it worked. So it looks like my vs instance does not have admin writes when in run time.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
MichaelMH

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
CraigLazar

ASKER
thanks for the help