Kilitli permissions to start a service

  • 09 Mart 2012 Cuma 14:08
     
     

    Hi.

    I'm developing a project where I'm looking after a service: Smart Card.

    I'm checking if it is closed. If it is closed then I'm checking if it is Disabled. If it is Disabled then I'm changing its status to Automatic and I'm starting the service. If the service is closed but the status is disabled I'm just open it.

    The problem is that on my computer everything is working good, but when I'm trying to use it on other computer, if the service is disabled then everything is fine, but for example if the service is automatic and closed then I'm trying to start it, but I receive the error which is telling that I don't have permissions.

    My questions are:

    1.How it is possible to have permissions to change the status from disable to auto and to start the service, but not to start it if the status is already automatic?

    2. How can I give the permissions just for that using the code?

    Here is the code what I'm using:

       Dim controller As New ServiceController("SCardSvr")

                        If controller.Status.Equals(ServiceControllerStatus.Stopped) Then
                            '' getServicesForDomainComputer(Environment.MachineName.ToString())
                            '' EnsureTransactionManagerStarted(False)

                            Dim obj As ManagementObject
                            Dim inParams, outParams As ManagementBaseObject
                            Dim Result As Integer


                            obj = New ManagementObject("\\.\root\cimv2:Win32_Service.Name='SCardSvr'")

                            'Change the Start Mode to Automatic

                            If obj("StartMode").ToString = "Disabled" Then
                                'Get an input parameters object for this method
                                inParams = obj.GetMethodParameters("ChangeStartMode")
                                inParams("StartMode") = "Automatic"

                                'do it!
                                outParams = obj.InvokeMethod("ChangeStartMode", inParams, Nothing)
                                Result = Convert.ToInt32(outParams("returnValue"))

                                If Result <> 0 Then
                                    Throw New Exception("ChangeStartMode method error code " & Result)
                                End If
                            End If

                            controller.Start()
                        End If

Tüm Yanıtlar

  • 12 Mart 2012 Pazartesi 08:14
     
     

    HI

    So which is error or error information about this code ?


    No code, No fact.

  • 12 Mart 2012 Pazartesi 08:39
     
     

    the error is in dutch because the application was installed on a dutch computer. so, the error is:

    Error number: 5 - Error message: Kan de service SCardSvr op de computer . niet openen

    It is saying that the service SCardSvr can not be opened.

  • 12 Mart 2012 Pazartesi 09:33
    Moderatör
     
     Yanıt

    Hi Andrei,

    Could you please let us know why would you want to get the permission to change the Windows services property which the account does not have orginally?  I think it may have security issues of some malicious codes get the permission to change system services properties or behaviors.  

    If the target Windows service is running with certain normal account, you can just running any codes through this account and change the target Windows service's behavior.   Otherwise, you need to use some system admin account to change other Windows services' properties or bahaviors. 

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us

  • 12 Mart 2012 Pazartesi 09:38
     
     

    I want to do this, because some of the customers don't have high skills in IT to know how to open a service etc.

    The problem is that my app is using this service and usually on some win XP this service is closed. And I want to give this permissions temporal because I want to avoid the error and to open the service and like this the user can use the application.

    If you have 100 customers and you need to help 50 of them with the installation, you will see it is really time consuming...

  • 13 Mart 2012 Salı 02:59
    Moderatör
     
     Yanıt

    Hi Andrei,

    I can understand your scenario, but as I explained in my last post, if the service is running under system account, only system admin account can change its properties or behaviors.   Normal users cannot achieve it even it's temporary. 

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us