Solved

visual basic API for disable remote services?

Posted on 2004-09-02
3
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

717 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