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

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

  1. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,855
    2,102
    60
    #1 Muerto, Feb 16, 2013
    Last edited: Jan 12, 2021
    ...
     
  2. l33tissw00t

    l33tissw00t MDL Addicted

    Dec 6, 2012
    819
    520
    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
    6,222
    22,279
    210
    #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:
     
  4. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,855
    2,102
    60
    #4 Muerto, Feb 17, 2013
    Last edited: Jan 12, 2021
    (OP)
    ...
     
  5. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,222
    22,279
    210
    Well I wouldn't use ServiceController :tooth: :laie:
     
  6. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,855
    2,102
    60
    It's only there to eliminate a restart =/
     
  7. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,222
    22,279
    210
    Come on David, think about it ? :eek::D
     
  8. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,855
    2,102
    60
    #8 Muerto, Feb 17, 2013
    Last edited: Jan 12, 2021
    (OP)
    ...
     
  9. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,460
    420
    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
    6,222
    22,279
    210
    Yeh I get like that from time to time Sarcy.. I do use a combination of both depending on what im doing..:tooth:
     
  11. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,460
    420
    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. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,855
    2,102
    60
    #12 Muerto, Feb 19, 2013
    Last edited: Jan 12, 2021
    (OP)
    ...
     
  13. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,460
    420
    60
    still for the ease of coding lol :p
     
  14. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,855
    2,102
    60
    #14 Muerto, Feb 19, 2013
    Last edited: Jan 12, 2021
    (OP)
    ...
     
  15. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,222
    22,279
    210
    Yep i got some WMI functions for this also..:tooth::yeah:
     
  16. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,460
    420
    60
  17. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,855
    2,102
    60
    #17 Muerto, Feb 19, 2013
    Last edited: Jan 12, 2021
    (OP)
    ...
     
  18. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,460
    420
    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. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,855
    2,102
    60
    #19 Muerto, Feb 19, 2013
    Last edited: Jan 12, 2021
    (OP)
    ...
     
  20. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,222
    22,279
    210
    #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: