Access denied when starting a service
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Access denied when starting a service

  1. #1
    Join Date
    Dec 2008
    Posts
    31

    Access denied when starting a service

    Hi,

    I want my code to automatically start a service if it's not already running when my program starts, then stop upon exit. I found the code to do this, but my program just gives me access denid errors when started. I believe I have to do some hockery pockery with tokens? although I cant find the code anywhere to do this, just wondering if anyone could help me out

    thanks

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Access denied when starting a service

    Could you give some more information. Where do you want to start the service, local or on a network device?

    Give some more details about the exception, (kind of exception, innerexception)

  3. #3
    Join Date
    Dec 2008
    Posts
    31

    Re: Access denied when starting a service

    Hi, the service is a local service and the exception it throws is :

    System.InvalidOperationException was unhandled
    Message="Cannot open screen-scraper server service on computer '.'."
    Source="System.ServiceProcess"
    StackTrace:
    at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
    at System.ServiceProcess.ServiceController.Stop()
    at WinScraper.Program.Main() in C:\WinScraper\WinScraper\WinScraper\Program.cs:line 34
    at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    InnerException: System.ComponentModel.Win32Exception
    Message="Access is denied"
    ErrorCode=-2147467259
    NativeErrorCode=5
    InnerException:

    as you can see it says InnerException: System.ComponentModel.Win32Exception
    Message="Access is denied", so I presumed that has something to do with windows permissions?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,485

    Re: Access denied when starting a service

    Please post the code you use to start the service (and use [code ] [/ code] tags).

  5. #5
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Access denied when starting a service

    Check this thread for a discussion of some of the permission type issues you will run into...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Dec 2008
    Posts
    31

    Re: Access denied when starting a service

    ServiceController sc = new ServiceController("screen scraper server");

    if(sc.Status.Equals(ServiceControllerStatus.Stopped)){
    sc.Start();
    }


    is the basic code I'm using to check if the service is running already, and if not then to start it. I know it's recognising the service etc, no problem getting status and things like that, just don't have the permissions to start/stop the service. Still can't find a way to do this..

  7. #7
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Access denied when starting a service

    Quote Originally Posted by Bobby_1234 View Post
    ServiceController sc = new ServiceController("screen scraper server");

    if(sc.Status.Equals(ServiceControllerStatus.Stopped)){
    sc.Start();
    }


    is the basic code I'm using to check if the service is running already, and if not then to start it. I know it's recognising the service etc, no problem getting status and things like that, just don't have the permissions to start/stop the service. Still can't find a way to do this..
    Did you follow the links in the next to last two replies in the thread I provided???
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center