Solved

Check to see if service is running

Posted on 2002-05-12
7
149 Views
Last Modified: 2010-05-02
Hi,

I'm writing an app that takes user input and generates text files based on that input.

The text files are processed by a service (that I had no hand in writing) running on Win2K.

In my app would like to give some indication that the service is running or not - green light / red light type of thing.

So....is there a way from VB to check to see is a service is running on a different machine?  I have no need to stop, start, or otherwise interact with the service.

Thanks in advance,

Mike
0
Comment
Question by:marscpa
7 Comments
 
LVL 44

Accepted Solution

by:
bruintje earned 200 total points
ID: 7004728
Hi Mike,

Picked this up some while ago from the net. It also includes subs to start, stop and pause the service. But you don't need them though they're included
Be sure to change "Computer Name" to the appropriate name, and also change "Service Name" to the appropriate short service name (ie its real name, not the name you see in service manager).


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

Private Sub SomeSub()
    Dim ServiceStatus as SERVICE_STATUS
    Dim lhSCManager As Long
    Dim lhService As Long
    Dim lhServiceStatus As Long

    lhSCManager = OpenSCManager("Computer Name", SERVICES_ACTIVE_DATABASE,
SC_MANAGER_ALL_ACCESS)
       
    If lhSCManager <> 0 Then
           
        lhService = OpenService(lhSCManager, "Service Name",
SERVICE_ALL_ACCESS)
       
        If lhService <> 0 Then
               
            lhServiceStatus = QueryServiceStatus(lhService, ServiceStatus)
               
            If lhServiceStatus <> 0 Then
                Select Case ServiceStatus.dwCurrentState
                    Case SERVICE_STOPPED
                    Case SERVICE_START_PENDING
                    Case SERVICE_STOP_PENDING
                    Case SERVICE_RUNNING
                    Case SERVICE_CONTINUE_PENDING
                    Case SERVICE_PAUSE_PENDING
                    Case SERVICE_PAUSED
                End Select
            End If
            CloseServiceHandle lhService
        End If
        CloseServiceHandle lhSCManager
    End If

End Sub


Public Sub vbPauseService()
    Dim ServiceStatus as SERVICE_STATUS
    Dim lhSCManager As Long
    Dim lhService As Long
   
    lhSCManager = OpenSCManager("Computer Name", SERVICES_ACTIVE_DATABASE,
SC_MANAGER_ALL_ACCESS)
   
    If lhSCManager <> 0 Then
        lhService = OpenService(lhSCManager, "Service Name",
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()
    Dim ServiceStatus as SERVICE_STATUS
    Dim lhSCManager As Long
    Dim lhService As Long
   
    lhSCManager = OpenSCManager("Computer Name", SERVICES_ACTIVE_DATABASE,
SC_MANAGER_ALL_ACCESS)
   
    If lhSCManager <> 0 Then
        lhService = OpenService(lhSCManager, "Service Name",
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()
    Dim ServiceStatus as SERVICE_STATUS
    Dim lhSCManager As Long
    Dim lhService As Long
   
    lhSCManager = OpenSCManager("Computer Name", SERVICES_ACTIVE_DATABASE,
SC_MANAGER_ALL_ACCESS)
   
    If lhSCManager <> 0 Then
        lhService = OpenService(lhSCManager, "Service Name",
SERVICE_ALL_ACCESS)
       
        If lhService <> 0 Then
            lResult = ControlService(lhService, SERVICE_CONTROL_STOP,
ServiceStatus)
            CloseServiceHandle lhService
        End If
        CloseServiceHandle lhSCManager
    End If
   
End Sub


it's mainly NT 4 but that's what i need till today :)

HTH:O)Bruintje
0
 
LVL 12

Expert Comment

by:roverm
ID: 7004744
<being impressed>
WOW BRIAN!
</being impressed>

listening...

D'Mzzl!
RoverM
0
 
LVL 44

Expert Comment

by:bruintje
ID: 7004754
picked up most of it, so it's not all from my own lazy brain......if it works i'll use it and don't think to long ;)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Expert Comment

by:raizon
ID: 7004807
listening
0
 

Author Comment

by:marscpa
ID: 7005191
Bruintje-

I'll try this out and award points accordingly (spelling?).  Thanks for the quick response.

Mike
0
 

Author Comment

by:marscpa
ID: 7011569
I have only had a little time to play with this and honestly have nto gotten it to work yet, BUT it's definelty going down the right path.

Thanks for the quick response...I didn't want the points to be so slow going back to you.

Mike
0
 
LVL 44

Expert Comment

by:bruintje
ID: 7012061
Well thanks but i really don't mind that much for VB points (Office points i do ;) ) but if you post some follow up i hope to get notifs that's really terrible because i get no notifs after a Q is a PAQ

but i'll put this on the list and check in now and then
0

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.

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

707 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

15 Experts available now in Live!

Get 1:1 Help Now