Solved

.net stop service command Access Denied error

Posted on 2010-09-13
11
888 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:CraigLazar
  • 6
  • 5
11 Comments
 
LVL 4

Expert Comment

by:MichaelMH
ID: 33661488
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

0
 
LVL 4

Author Comment

by:CraigLazar
ID: 33661988
hi,
thanks for the code, however i am getting an error on the
dim controller as new ServiceController

"type not defined"
0
 
LVL 4

Expert Comment

by:MichaelMH
ID: 33662094
Did you used the import statement: Imports System.ServiceProcess?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 4

Expert Comment

by:MichaelMH
ID: 33662146
Also be sure that you have System.ServiceProcess DLL in your project references.
0
 
LVL 4

Author Comment

by:CraigLazar
ID: 33662596
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.
0
 
LVL 4

Expert Comment

by:MichaelMH
ID: 33662727
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.
0
 
LVL 4

Author Comment

by:CraigLazar
ID: 33663756
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.
0
 
LVL 4

Expert Comment

by:MichaelMH
ID: 33663827
What OS do you have? Check if you need to run the program as administrator.
0
 
LVL 4

Author Comment

by:CraigLazar
ID: 33664917
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.
0
 
LVL 4

Accepted Solution

by:
MichaelMH earned 500 total points
ID: 33664964
So then here is your solution. If it's possible please mark the response as the right solution ;).
0
 
LVL 4

Author Closing Comment

by:CraigLazar
ID: 33668860
thanks for the help
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question