Solved

Check to see if service is running

Posted on 2002-05-12
7
156 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
[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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

689 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