[VB][C#] Question on stopping/disabling services

Discussion in 'Mixed Languages' started by QuantumBug, Feb 16, 2013.

  1. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,488
    1,324
    60
    #1 QuantumBug, Feb 16, 2013
    Last edited by a moderator: Apr 20, 2017
    Well, just a small issue, I'm using the code below to start and stop a particular service, but after a restart it will return to it's original state. Is it best I edit the Registry also, when stopping or starting the service...

    Surely when you stop a service it's going to stay disabled/stopped until the user starts it again?

    Code:
    Public Sub sscertprop()
            Dim cp As ServiceController = New ServiceController("CertPropSvc")
            If (cp.Status.Equals(ServiceControllerStatus.Stopped)) Or
                (cp.Status.Equals(ServiceControllerStatus.StopPending)) Then
                cp.Start()
            Else
                cp.Stop()
            End If
        End Sub
    I use the code by simply calling it.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. l33tissw00t

    l33tissw00t MDL Addicted

    Dec 6, 2012
    543
    315
    30
    #2 l33tissw00t, Feb 16, 2013
    Last edited by a moderator: Apr 20, 2017
    Not understanding the question.
    Do you wish to disable it from starting after a restart?

    Take a look at:
    Code:
    technet.microsoft.com/en-us/library/cc990290(v=ws.10).aspx
    using start=disabled
     
  3. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,873
    20,139
    180
    #3 Alphawaves, Feb 17, 2013
    Last edited by a moderator: Apr 20, 2017
    Dave your question is some what confusing, you don't need to touch registry if stopping or starting a service..:eek:
    Do you want to disable the service from starting ?
    If so i would use registry, maybe something like:

    Code:
    //2 = Automatic
    //3 = Manual
    //4 = Disabled 
    
    RegistryKey ss = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\ServiceName", true);
                ss.SetValue("Start", 4);
                ss.Close(); 
    :eek:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,488
    1,324
    60
    #4 QuantumBug, Feb 17, 2013
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Sorry about not being clear =/

    Basically, I want to completely stop/disable a service until the user starts it again in the application, but am also giving the user the option to enable it at a later date. Say the user removes their wireless card and use LAN only, they'd want to disable the WLAN AutoConfig service (Wlansvc) and the code in the OP would stop a service but it would start after a computer reboot, or they'd start a service and it would stop after a reboot.

    Anyway this code seems to have solved it...

    Code:
        Public Sub sscertprop()
    
            'Select the particular service
            Dim cp As ServiceController = New ServiceController("CertPropSvc")
            If (cp.Status.Equals(ServiceControllerStatus.Stopped)) Or
                (cp.Status.Equals(ServiceControllerStatus.StopPending)) Then
                'Set startup to automatic
                My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\CertPropSvc", "Start", "2", RegistryValueKind.DWord)
                'Start the service
                cp.Start()
            Else
                'Set the startup to manual
                My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\CertPropSvc", "Start", "3", RegistryValueKind.DWord)
                'Stop the service
                cp.Stop()
            End If
    
        End Sub
    Unless anyone can think of a better option :D this has worked perfectly so far.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,873
    20,139
    180
    Well I wouldn't use ServiceController :tooth: :laie:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,488
    1,324
    60
    It's only there to eliminate a restart =/
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,873
    20,139
    180
    Come on David, think about it ? :eek::D
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,488
    1,324
    60
    Will it already be disabled after the Registry is edited?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,462
    419
    60
    then what would you use? ServiceController provides better error handling than shellexec lol. I use SC.exe just to install my services then ServiceController to do the rest.
     
  10. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,873
    20,139
    180
    Yeh I get like that from time to time Sarcy.. I do use a combination of both depending on what im doing..:tooth:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,462
    419
    60
    #11 PAYMYRENT, Feb 19, 2013
    Last edited by a moderator: Apr 20, 2017
    lol Alpha...

    I think that WMI could stop and disable it not sure though.. i will look into it

    EDIT: wmi would do it better.

    Code:
    ' Lock to Your service - Mine is Reboot-To Updater service
                    Dim classInstance As New ManagementObject( _
                        "root\CIMV2", _
                        "Win32_Service.Name='SF-Reboot-To'", _
                        Nothing)
    
    Dim outParams As ManagementBaseObject = _
                        classInstance.InvokeMethod("StopService", Nothing, Nothing)
    
                    ' List outParams
                    Console.WriteLine("Out parameters:")
                    Console.WriteLine("ReturnValue: {0}", outParams("ReturnValue"))
    
                    ' Obtain [in] parameters for the method
                    Dim DisabledParam As ManagementBaseObject = _
                        classInstance.GetMethodParameters("ChangeStartMode")
    
                    ' Add the input parameters.
                    DisabledParam("StartMode") =  "Disabled"
    
                    ' Execute the method and obtain the return values.
                    Dim outDisable As ManagementBaseObject = _
                        classInstance.InvokeMethod("ChangeStartMode", DisabledParam, Nothing)
    
                    ' List outParams
                    Console.WriteLine("Out parameters:")
                    Console.WriteLine("ReturnValue: {0}", outDisable("ReturnValue"))
    
    
     
  12. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,488
    1,324
    60
    WMI can kiss my ass :)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  13. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,462
    419
    60
    still for the ease of coding lol :p
     
  14. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,488
    1,324
    60
    Well it is good for listing startup items, damn things are placed everywhere in the registry.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  15. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,873
    20,139
    180
    Yep i got some WMI functions for this also..:tooth::yeah:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  16. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,462
    419
    60
  17. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,488
    1,324
    60
    Anything to disable startup items? I can list them ok, having a hard time finding a good way to disable them.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  18. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,462
    419
    60
    dump the reg item that you are disabling then delete the reg value.

    for services make a list of them and disable the ones that you want and store a list in the registry or a file on the hdd.
     
  19. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,488
    1,324
    60
    Actually, good idea, right now I'm reading application location and displaying information via the registry key.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  20. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,873
    20,139
    180
    #20 Alphawaves, Feb 19, 2013
    Last edited by a moderator: Apr 20, 2017
    PMR already done it for you:
    Code:
    Dim classInstance As New ManagementObject("root\CIMV2", "Win32_Service.Name='ServiceName'", Nothing)
            Dim outParams As ManagementBaseObject = classInstance.InvokeMethod("StopService", Nothing, Nothing)
            Dim DisabledParam As ManagementBaseObject = classInstance.GetMethodParameters("ChangeStartMode")
            DisabledParam("StartMode") = "Disabled"
            Dim outDisable As ManagementBaseObject = classInstance.InvokeMethod("ChangeStartMode", DisabledParam, Nothing)
    Another crappy way:
    Code:
    For Each obj In GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select State from Win32_Service where Name = 'ServiceName'")
                If obj.State = "Running" Then
                    obj.StopService()
                End If
                obj.ChangeStartMode("Disabled")
            Next
    :tooth::tooth::tooth:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...