Enabling/Disable a service on MSCONFIG using VB 6.0 , 500 Points!

Posted on 2007-10-07
Last Modified: 2013-12-26
Hi, take a moment to my example , --> Click the Start button , then click "run" and open the file "msconfig" , then click on Services, there you should see many applications , now what i need to do is ... to find a way so i can Disable/Enable a service using visual basic 6.0 .

Question by:KingSencat
    LVL 8

    Expert Comment

    Question was asked before, here is an answer:
    LVL 6

    Expert Comment

    I'd start looking at the ControlService API:

    Not used this myself, but I've heard it should allow you to stop and start services and this should help you:

    Author Comment

    Anthony2000: Doesn't help too much , can you paste me here a sample code ?

    Author Comment

    I just need a code so i can STOP A SERVICE and RUN A SERVICE , this is all what i need , please help me
    LVL 8

    Expert Comment

    KingSencat, I found an example on the web that uses the service control manager api:
    I would test if for you, but I don't have VB6 on this machine. Its on my notebook at work.

    In summary:

    this is the function that you will call (from below):

    dim result as boolean

    result = ServiceCommand("service name",  SERVICE_CONTROL_STOP) ' disable service or stop it

    result = ServiceCommand("service name",  SERVICE_CONTROL_START) ' enable service or start it

    Let me know if this worked.

    Private Type SERVICE_STATUS
    dwServiceType As Long
    dwCurrentState As Long
    dwControlsAccepted As Long
    dwWin32ExitCode As Long
    dwServiceSpecificExitCode As Long
    dwCheckPoint As Long
    dwWaitHint As Long
    End Type

    Private Declare Function OpenSCManager Lib "advapi32.dll" Alias _
    "OpenSCManagerA" (ByVal lpMachineName As String, _
    ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
    Private Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject _
    As Long) As Long
    Private Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" _
    (ByVal hSCManager As Long, ByVal lpServiceName As String, _
    ByVal dwDesiredAccess As Long) As Long
    Private Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" _
    (ByVal hService As Long, ByVal dwNumServiceArgs As Long, _
    ByVal lpServiceArgVectors As Long) As Long
    Private Declare Function ControlService Lib "advapi32.dll" (ByVal hService As _
    Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long

    Const GENERIC_EXECUTE = &H20000000

    ' start/stop/pause/continue a service
    ' SERVICENAME is the internal name of the service
    ' COMMAND can be 0=Start, 1=Stop, 2=Pause, 3=Continue
    ' returns True if successful, False otherwise
    ' if any error, call Err.LastDLLError for more information

    Function ServiceCommand(ByVal ServiceName As String, ByVal command As Long) As _
    Dim hSCM As Long
    Dim hService As Long
    Dim res As Long
    Dim lpServiceStatus As SERVICE_STATUS

    ' first, check the command
    If command < 0 Or command > 3 Then Err.Raise 5

    ' open the connection to Service Control Manager, exit if error
    hSCM = OpenSCManager(vbNullString, vbNullString, GENERIC_EXECUTE)
    If hSCM = 0 Then Exit Function

    ' open the given service, exit if error
    hService = OpenService(hSCM, ServiceName, GENERIC_EXECUTE)
    If hService = 0 Then GoTo CleanUp

    ' start the service
    Select Case command
    Case 0
    ' to start a service you must use StartService
    res = StartService(hService, 0, 0)
    ' these commands use ControlService API
    ' (pass a NULL pointer because no result is expected)
    res = ControlService(hService, command, lpServiceStatus)
    End Select
    If res = 0 Then GoTo CleanUp

    ' return success
    ServiceCommand = True

    If hService Then CloseServiceHandle hService
    ' close the SCM
    CloseServiceHandle hSCM

    End Function
    LVL 33

    Accepted Solution

    perhaps this works also in VB

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\cimv2")
    Set colListOfServices = objWMIService.ExecQuery _
     ("Select * from Win32_Service Where Name ='Alerter'") 'Enter here the service Name
    For Each objService in colListOfServices
        Wscript.Echo "Started Alerter service"

    In addition to stop a service use instead of StartService()

    I hope this works for you
    LVL 65

    Expert Comment

    @roos01 and KingSencat,

    that code will work (and is probably the simplest), as long as you have a project reference to the Microsoft Scripting Runtime Library added.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now