Starting Services in VB

I am looking for sample code that shows how to start Windows Services from VB by using the Win32 API or other means.
Thanks...
GeekManAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
0
RanaHossainCommented:
talk about a coincidence - I spent the whole day yesterday trying to get my application as a service.

first tried using servany.exe - which you can download from the net - it also comes with 2 other files that allows you to run a service...

now that did create the service, but was not running.

so found some api.. and same deal.

finally I figured it out - services needs to be event driven - so downloaded a copy of NTSVC.OCX, and with events you can install, start, stop, pause, uninstall the service.

0
RainUKCommented:
If you want to avoid the agro and grief of using pointers in structures, copymemory etc etc to Win32 API, then try Windows Management Instrumentation, much easier and less grief here are some useful links for you to check out:


WMI Win32_Service
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_service.asp

And some easy code examples on info above:

http://www.microsoft.com/technet/treeview/default.asp?url=/technet/scriptcenter/scrguide/sas_ser_bwdq.asp


I wrote my own Service Control Program using just WMI, it does pretty much what the Win32 APIs do, except for a few things. But I am sure you'll find the WMI Win32_Service class does all what you need.
0
Glen A.IT Project ManagerCommented:
Hey there, here's some code you can use:

in your form you can call the start/stop/pause service subs as follows:

vbStartService strServer, strService
vbStopService strServer, strService
vbPauseService strServer, strService

you can check the status with

ServiceStatus strServer, strService



code for your vb module:


Option Explicit

Global strServer As String   'Name of computer to check
Global strService As String  'Name of service to check

Public Const SERVICES_ACTIVE_DATABASE = "ServicesActive"

' Controls
Public Const SERVICE_CONTROL_STOP = &H1
Public Const SERVICE_CONTROL_PAUSE = &H2

' Service State -- for CurrentState
Public Const SERVICE_STOPPED = &H1
Public Const SERVICE_START_PENDING = &H2
Public Const SERVICE_STOP_PENDING = &H3
Public Const SERVICE_RUNNING = &H4
Public Const SERVICE_CONTINUE_PENDING = &H5
Public Const SERVICE_PAUSE_PENDING = &H6
Public Const SERVICE_PAUSED = &H7

' Service Control Manager object specific access types
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SC_MANAGER_CONNECT = &H1
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)
'Service object specific access types
Public Const SERVICE_QUERY_CONFIG = &H1
Public Const SERVICE_CHANGE_CONFIG = &H2
Public Const SERVICE_QUERY_STATUS = &H4
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)

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

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

Public Sub ServiceStatus(Computer As String, Service As String)
    Dim ServiceStatus As SERVICE_STATUS
    Dim lhSCManager As Long
    Dim lhService As Long
    Dim lhServiceStatus As Long

    lhSCManager = OpenSCManager(Computer, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
       
    If lhSCManager <> 0 Then
           
        lhService = OpenService(lhSCManager, Service, SERVICE_ALL_ACCESS)
       
        If lhService <> 0 Then
               
            lhServiceStatus = QueryServiceStatus(lhService, ServiceStatus)
               
            If lhServiceStatus <> 0 Then
                Select Case ServiceStatus.dwCurrentState
                    Case SERVICE_STOPPED
                        MsgBox "Stopped"
                    Case SERVICE_START_PENDING
                        MsgBox " Start Pending"
                    Case SERVICE_STOP_PENDING
                        MsgBox "Stop Pending"
                    Case SERVICE_RUNNING
                        MsgBox "Running"
                    Case SERVICE_CONTINUE_PENDING
                        MsgBox "Coninue Pending"
                    Case SERVICE_PAUSE_PENDING
                        MsgBox "Pause Pending"
                    Case SERVICE_PAUSED
                        MsgBox "Paused"
                End Select
            End If
            CloseServiceHandle lhService
        End If
        CloseServiceHandle lhSCManager
    End If

End Sub


Public Sub vbPauseService(Computer As String, Service As String)
    Dim ServiceStatus As SERVICE_STATUS
    Dim lhSCManager As Long
    Dim lhService As Long
    Dim lresult As Long
   
    lhSCManager = OpenSCManager(Computer, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
    If lhSCManager <> 0 Then
        lhService = OpenService(lhSCManager, Service, SERVICE_ALL_ACCESS)
       
        If lhService <> 0 Then
            lresult = ControlService(lhService, SERVICE_CONTROL_PAUSE, ServiceStatus)
            CloseServiceHandle lhService
        End If
        CloseServiceHandle lhSCManager
    End If
       
End Sub


Public Sub vbStartService(Computer As String, Service As String)
    Dim ServiceStatus As SERVICE_STATUS
    Dim lhSCManager As Long
    Dim lhService As Long
    Dim lresult As Long
   
    lhSCManager = OpenSCManager(Computer, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
   
    If lhSCManager <> 0 Then
        lhService = OpenService(lhSCManager, Service, SERVICE_ALL_ACCESS)
       
        If lhService <> 0 Then
            lresult = StartService(lhService, 0, 0)
            CloseServiceHandle lhService
        End If
        CloseServiceHandle lhSCManager
    End If
       
End Sub


Public Sub vbStopService(Computer As String, Service As String)
    Dim ServiceStatus As SERVICE_STATUS
    Dim lhSCManager As Long
    Dim lhService As Long
    Dim lresult As Long
   
    lhSCManager = OpenSCManager(Computer, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
   
    If lhSCManager <> 0 Then
        lhService = OpenService(lhSCManager, Service, SERVICE_ALL_ACCESS)
       
        If lhService <> 0 Then
            lresult = ControlService(lhService, SERVICE_CONTROL_STOP, ServiceStatus)
            CloseServiceHandle lhService
        End If
        CloseServiceHandle lhSCManager
    End If
End Sub

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.