Solved

visual basic API for disable remote services?

Posted on 2004-09-02
3
317 Views
Last Modified: 2013-11-13
I am looking for an API that will disable remote services (or any other way of doing it)  OTHER than through the registry as this is unreliable due to multiple controlset's in the registry.  Thanks!
0
Comment
Question by:bluedragon99
  • 2
3 Comments
 
LVL 1

Author Comment

by:bluedragon99
ID: 11969762
remember DISABLE not stop.
0
 
LVL 3

Accepted Solution

by:
Validor earned 500 total points
ID: 11978309
Below is a module that will do this for you.  Here is an example call.

'-----------example to disable a service---------------'
'This will disable the IIS webserver service on a machine named "mywebserver"
'To configure a service on the local machine, leave the machine name as an empty string "".
    If SetServiceStartupType("mywebserver", "w3svc", stDisabled) Then
        MsgBox "Successful"
    Else
        MsgBox "Failed"
    End If



'-----------begin ServicesModules.BAS---------------'
Option Explicit

Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SERVICE_QUERY_CONFIG = &H1
Public Const SERVICE_QUERY_STATUS = &H4
Public Const SERVICE_CHANGE_CONFIG = &H2
Public Const SERVICE_ENUMERATE_DEPENDENTS = &H8
Public Const SERVICE_START = &H10
Public Const SERVICE_STOP = &H20
Public Const SERVICE_PAUSE_CONTINUE = &H40
Public Const SERVICE_INTERROGATE = &H80
Public Const SERVICE_USER_DEFINED_CONTROL = &H100
Public Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG Or SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDENTS Or SERVICE_START Or SERVICE_STOP Or SERVICE_PAUSE_CONTINUE Or SERVICE_INTERROGATE Or SERVICE_USER_DEFINED_CONTROL)

Public Const SC_MANAGER_CONNECT = 1
Public Const SC_MANAGER_CREATE_SERVICE = &H2
Public Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Public Const SC_MANAGER_LOCK = &H8
Public Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
Public Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20

Public Const SC_MANAGER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SC_MANAGER_CONNECT Or SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVICE Or SC_MANAGER_LOCK Or SC_MANAGER_QUERY_LOCK_STATUS Or SC_MANAGER_MODIFY_BOOT_CONFIG)

Public Const SERVICE_ERROR_IGNORE = &H0
Public Const SERVICE_ERROR_NORMAL = &H1
Public Const SERVICE_ERROR_SEVERE = &H2
Public Const SERVICE_ERROR_CRITICAL = &H3



Public 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

Public Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Public Declare Function setservicestatus Lib "advapi32.dll" Alias "SetServiceStatus" (ByVal hServiceStatus As Long, lpServiceStatus As SERVICE_STATUS) As Long
Public Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
Public Declare Function ChangeServiceConfig Lib "advapi32.dll" Alias "ChangeServiceConfigA" (ByVal hservice As Long, ByVal dwServiceType As Long, ByVal dwStartType As Long, ByVal dwErrorControl As Long, ByVal lpBinaryPathName As String, ByVal lpLoadOrderGroup As String, lpdwTagId As Long, ByVal lpDependencies As String, ByVal lpServiceStartName As String, ByVal lpPassword As String, ByVal lpDisplayName As String) As Long
Public Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long


Public Const SERVICE_NO_CHANGE = &HFFFF

Public Const SERVICE_BOOT_START = 0
Public Const SERVICE_SYSTEM_START = 1
Public Const SERVICE_AUTO_START = 2
Public Const SERVICE_DEMAND_START = 3
Public Const SERVICE_DISABLED = 4

Public Const stAutomatic = SERVICE_AUTO_START
Public Const stManual = SERVICE_DEMAND_START
Public Const stDisabled = SERVICE_DISABLED




Public Function SetServiceStartupType( _
    amachinename As String, _
    aservicename As String, _
    astartuptype As Integer _
) As Boolean

    Dim FuncResult As Boolean
    Dim hservicemanager As Long
    Dim hservice As Long
   
    'set default to successful
    FuncResult = True
   
    'open the service control manager
    hservicemanager = OpenSCManager( _
        amachinename + Chr$(0), _
        "ServicesActive" + Chr$(0), _
        SC_MANAGER_ALL_ACCESS _
    )
   
    If (hservicemanager <> 0) Then
        'open the service
        hservice = OpenService( _
            hservicemanager, _
            aservicename + Chr$(0), _
            SERVICE_ALL_ACCESS _
        )
       
        If (hservice <> 0) Then
            'change the configuration options for the service
            Dim rc As Long
            rc = ChangeServiceConfig( _
                hservice, _
                SERVICE_NO_CHANGE, _
                astartuptype, _
                SERVICE_NO_CHANGE, _
                ByVal vbNullString, _
                ByVal vbNullString, _
                ByVal 0&, _
                ByVal vbNullString, _
                ByVal vbNullString, _
                ByVal vbNullString, _
                ByVal vbNullString _
            )
            If (rc = 0) Then FuncResult = False
           
            'close the service
            CloseServiceHandle hservice
        Else
            FuncResult = False
        End If
       
        'close the service control manager
        CloseServiceHandle hservicemanager
    Else
        FuncResult = False
    End If
   
    'return the result
    SetServiceStartupType = FuncResult
   
End Function


0
 
LVL 1

Author Comment

by:bluedragon99
ID: 11980830
Excellent work, thanks!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
squareUp  challenge 22 106
Batch file output 20 77
userCompare  challenge 3 61
Java Loop 4 3
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

19 Experts available now in Live!

Get 1:1 Help Now