Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

.net stop service command Access Denied error

Posted on 2010-09-13
11
Medium Priority
?
938 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

604 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