Solved

.net stop service command Access Denied error

Posted on 2010-09-13
11
883 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
Comment Utility
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
Comment Utility
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
Comment Utility
Did you used the import statement: Imports System.ServiceProcess?
0
 
LVL 4

Expert Comment

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

Author Comment

by:CraigLazar
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Expert Comment

by:MichaelMH
Comment Utility
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
Comment Utility
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
Comment Utility
What OS do you have? Check if you need to run the program as administrator.
0
 
LVL 4

Author Comment

by:CraigLazar
Comment Utility
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
Comment Utility
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
Comment Utility
thanks for the help
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now