ServiceCommand – Start, stop, pause, and continue a Windows NT service

ServiceCommand – Start, stop, pause, and continue a Windows NT service

Private Type SERVICE_STATUS       dwServiceType As Long    dwCurrentState As Long    dwControlsAccepted As Long    dwWin32ExitCode As Long    dwServiceSpecificExitCode As Long    dwCheckPoint As Long    dwWaitHint As LongEnd TypePrivate Declare Function OpenSCManager Lib "advapi32.dll" Alias _    "OpenSCManagerA" (ByVal lpMachineName As String, _    ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As LongPrivate Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject _    As Long) As LongPrivate Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" _    (ByVal hSCManager As Long, ByVal lpServiceName As String, _    ByVal dwDesiredAccess As Long) As LongPrivate Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" _    (ByVal hService As Long, ByVal dwNumServiceArgs As Long, _    ByVal lpServiceArgVectors As Long) As LongPrivate Declare Function ControlService Lib "advapi32.dll" (ByVal hService As _    Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As LongConst GENERIC_EXECUTE = &H20000000Const SERVICE_CONTROL_STOP = 1Const SERVICE_CONTROL_PAUSE = 2Const SERVICE_CONTROL_CONTINUE = 3' 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 informationFunction ServiceCommand(ByVal ServiceName As String, ByVal command As Long) As _    Boolean    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)        Case SERVICE_CONTROL_STOP, SERVICE_CONTROL_PAUSE, _            SERVICE_CONTROL_CONTINUE            ' 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 = TrueCleanUp:    If hService Then CloseServiceHandle hService    ' close the SCM    CloseServiceHandle hSCM    End Function

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes