Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

NT Services monitoring with VB

Posted on 1999-07-14
8
Medium Priority
?
210 Views
Last Modified: 2010-05-02
I would like to use a VB app that monitors particular services in windows NT 4.0. If the service dies, then I would like to be able to automatically restart it. Is this possible?
0
Comment
Question by:GeekMan
[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
  • 4
  • 3
8 Comments
 
LVL 1

Expert Comment

by:ndb
ID: 1524705
Let me know if someone answered. I asked almost the same question.
0
 

Author Comment

by:GeekMan
ID: 1524706
If I cannot do that, I would at least like to be able to detect the service's status (started or stopped).
0
 
LVL 2

Accepted Solution

by:
Torus earned 400 total points
ID: 1524707
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 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)

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)


Public Const SERVICE_STOPPED = &H1

Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (lpMachineName As Any, lpDatabaseName As Any, 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

Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long


Sub StartQueryService()
    Dim SCHandler As Long
    Dim SHandler As Long
    Dim SS As SERVICE_STATUS
    Dim RC As Long
   

    SCHandler = OpenSCManager(ByVal 0, ByVal 0,     SC_MANAGER_ALL_ACCESS)
    If SCHandler <> 0 Then
        SHandler = OpenService(SCHandler,"YourServiceName", SERVICE_ALL_ACCESS)
        If SHandler <> 0 Then
            RC = QueryServiceStatus(SHandler, SS)
            If RC <> 0 Then
                 If SS.dwCurrentState = SERVICE_STOPPED Then
                      call StartService(SHandler, 0, 0)
                 end if
            End If
        end if
     end if
    CloseServiceHandle SHandler
    CloseServiceHandle SCHandler
end sub
0
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!

 

Author Comment

by:GeekMan
ID: 1524708
Wow...This is great. Thank you very much..
0
 

Author Comment

by:GeekMan
ID: 1524709
One question, don't I have to declare "STANDARD_RIGHTS_REQUIRED" as a constant? What should it's Hex value be, or does it matter??
0
 
LVL 2

Expert Comment

by:Torus
ID: 1524710
Sorry, missing. Here you are!

Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
0
 

Author Comment

by:GeekMan
ID: 1524711
I've got it working..I've noticed something interesting points in doing so. Unless I'm doing something wrong, the service names must not have any blanks in them when passed to the OpenService function(ie. a Service that displays as "Directory Replicator" must be passed to the function as: "DirectoryReplicator") I also found that not all Services are created equal. Some are displayed differently(particularly 3rd party services) than how they are recognized internally.
In one particular case I had a Service displayed as: "DmNet Listener Server" what worked internally was: "DmNetListenerService"...a little strange I thought..
0
 
LVL 2

Expert Comment

by:Torus
ID: 1524712
oh! Thank you for telling your experience here. Actually, I don't know the service name passed to the function should not have space.



0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

715 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