Solved

.net stop service command Access Denied error

Posted on 2010-09-13
11
895 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Open dialog with server side controls in it 3 47
linq .concat equivalent with expression syntax 4 26
ModalPopup  question 22 39
Stop Git from being my repository 1 26
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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