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

x
?
Solved

Shell & Service Issues

Posted on 1999-07-20
5
Medium Priority
?
691 Views
Last Modified: 2012-06-21
First, I'm not a programmer - I dabble and know a bit (DLL calls I do NOT know).

Trying to write a program that monitors services under NT.
In order to verify that the service exists and/or is running, I was going to use a shell command and redirect the output to a file - open that file and parse the information.  Problem is, the NET START <service> command will not redirect to a file.  So I need an alternate way of doing this - simple DLL calls (well detailed in any perspective answer), another way of reading the result of information from the shell, whatever.  The following is more or less how I was intending to do this, assuming the redirection worked:

shell "net start " + ServiceName + " > ~svcchk.tmp"
open "~svcchk.tmp" for input as #1
line input #1, StartServiceResult
If instr(StartServiceResult, <string>) then
else
end if
0
Comment
Question by:Lee W, MVP
[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
  • 3
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
Mirkwood earned 600 total points
ID: 1526175
Public Const MachineName = "s1"
Public Const ServiceName = "Schedule"



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
 
LVL 96

Author Comment

by:Lee W, MVP
ID: 1526176
I'll grade this tonight - I'll make it an "A" if you provide the source of your code (Technet, URL, etc).  Otherwise, it'll probably be a B (or C if it turns out its too cumbersome/time consuming for me to "decode" (it seems to do what I want it to, but it also seems to have a lot of things I'm not looking for - and I'd have preferred more comments to explain what exactly is being done everywhere).
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1526177
I'm afraid the human brain is the source.

But how difficult can it be:

vbStartService
ServiceStatus
vbPauseService
vbStopService

The parameters are always computername, servicename

0
 
LVL 96

Author Comment

by:Lee W, MVP
ID: 1526178
Ok... acceptible... though I did have some initial problems (vb6) having the constants as public - I had to remove the public portion of those statements and it worked.  Though I suppose it could just have something to do with how I had things.  I got it working though (or so it would seem) - but it doesn't seem to work with the SERVER or WORKSTATION services (I'll need to do a bit of further testing - I'm also running vb6 on win2k).  I'm going to delay answering the question just yet - you'll get the points at either an A or B grade, depending on how easily I figure out the rest of it (I think it'll be ok, but just want to check).
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1526179
That's because it's a module not a form. Forms don't allow public constants and declarations.
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

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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

661 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