Solved

Start/Stop NT Service

Posted on 2000-03-18
15
1,083 Views
Last Modified: 2008-02-01
How to start/stop a WinNT service, say, DNS, by VB code?
0
Comment
Question by:robbert
  • 5
  • 3
  • 3
  • +2
15 Comments
 
LVL 15

Expert Comment

by:cquinn
ID: 2630971
shell to "net stop DNS" might work!
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2631110
If will work, but only for local services (on the PC/Server the app runs)
If you want to do this on a remote PC/Server, you may use NETSVC.EXE instead of NET USE (included in NT resource kit), and call this within a shell.
In every case, check if permissions are given.
0
 
LVL 15

Author Comment

by:robbert
ID: 2631135
Thanks. Can one of you provide source code? The execute method should be in-process, that means, no extra window.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2631143
simply to in VB
SHELL "NET START DNS",vbHide
or
SHELL "NETSVC DNS \\servername /start", vbHide
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2631147
it won't be inprocess, but not extra window will be shown (vbHide)
0
 
LVL 15

Author Comment

by:robbert
ID: 2631431
I'm a dummy, not to tell exactly what I need. So:

Indeed, I want to stop and start the Domain Name Service of another server.

NETSVC shouldn't be so great because
- it is not free
- it does not return process info, so I might try to restart the service when it did not stop, yet. In the end, my app doesn't know if the action was successful.

Is there another tool, or any way to administer a service by API calls?
0
 
LVL 1

Accepted Solution

by:
pclement earned 50 total points
ID: 2631514
Take a look at the Services section at

http://www.netfokus.dk/vbadmincode/

I've not used the code or even looked at it for that matter, but it may be of some help.

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:pclement
ID: 2631615
Some additional code I happened to run across:

http://home.earthlink.net/~butlerbob/vb/winnt/srvctrl.htm

0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2631619
I posted my comment to this similiar question. You can see my answer from
http://www.experts-exchange.com/bin/Q.10278003



From the web:
Put the following code in the module:

'API Constants
Public Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
' Service Control
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 Function ServiceStatus(ComputerName As String, ServiceName As String) As String
    Dim ServiceStat As SERVICE_STATUS
    Dim hSManager As Long
    Dim hService As Long
    Dim hServiceStatus As Long

    ServiceStatus = ""
    hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE,
SC_MANAGER_ALL_ACCESS)
    If hSManager <> 0 Then
        hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
        If hService <> 0 Then
            hServiceStatus = QueryServiceStatus(hService, ServiceStat)
            If hServiceStatus <> 0 Then
                Select Case ServiceStat.dwCurrentState
                Case SERVICE_STOPPED
                    ServiceStatus = "Stopped"
                Case SERVICE_START_PENDING
                    ServiceStatus = "Start Pending"
                Case SERVICE_STOP_PENDING
                    ServiceStatus = "Stop Pending"
                Case SERVICE_RUNNING
                    ServiceStatus = "Running"
                Case SERVICE_CONTINUE_PENDING
                    ServiceStatus = "Coninue Pending"
                Case SERVICE_PAUSE_PENDING
                    ServiceStatus = "Pause Pending"
                Case SERVICE_PAUSED
                    ServiceStatus = "Paused"
                End Select
            End If
            CloseServiceHandle hService
        End If
        CloseServiceHandle hSManager
    End If
End Function

Public Sub ServicePause(ComputerName As String, ServiceName As String)
    Dim ServiceStatus As SERVICE_STATUS
    Dim hSManager As Long
    Dim hService As Long
    Dim res As Long

    hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE,
SC_MANAGER_ALL_ACCESS)
    If hSManager <> 0 Then
        hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
        If hService <> 0 Then
            res = ControlService(hService, SERVICE_CONTROL_PAUSE, ServiceStatus)
            CloseServiceHandle hService
        End If
        CloseServiceHandle hSManager
    End If
End Sub

Public Sub ServiceStart(ComputerName As String, ServiceName As String)
    Dim ServiceStatus As SERVICE_STATUS
    Dim hSManager As Long
    Dim hService As Long
    Dim res As Long

    hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE,
SC_MANAGER_ALL_ACCESS)
    If hSManager <> 0 Then
        hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
        If hService <> 0 Then
            res = StartService(hService, 0, 0)
            CloseServiceHandle hService
        End If
        CloseServiceHandle hSManager
    End If
End Sub

Public Sub ServiceStop(ComputerName As String, ServiceName As String)
    Dim ServiceStatus As SERVICE_STATUS
    Dim hSManager As Long
    Dim hService As Long
    Dim res As Long

    hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE,
SC_MANAGER_ALL_ACCESS)
    If hSManager <> 0 Then
        hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
        If hService <> 0 Then
            res = ControlService(hService, SERVICE_CONTROL_STOP, ServiceStatus)
            CloseServiceHandle hService
        End If
        CloseServiceHandle hSManager
    End If
End Sub



Put the following code in the form:

'Usage: the first parameter is the remote computer name, if it is "" the computer is the local
computer, the second is the service name.
Private Sub Command1_Click()
    ServiceStop "", "Schedule"
End Sub

Private Sub Command2_Click()
    ServiceStart "", "Schedule"
End Sub

Private Sub Command3_Click()
    ServicePause "", "Schedule"
End Sub

Private Sub Command4_Click()
    MsgBox ServiceStatus("", "Schedule")
End Sub
0
 
LVL 15

Author Comment

by:robbert
ID: 2631693
Thanks, cquinn and especially angelIII and, sorry that I didn't tell what I need exactly, initially.

pclement, this is a nice little source. I found a VB project with lots of API calls, resulting in 4 VB functions: start, stop, pause, status. One can specify the machine name, too. That's exactly what I wanted.

Found a task scheduler, too and, other code samples. N'ice!
That's the way I like it!
0
 
LVL 15

Author Comment

by:robbert
ID: 2631710
Ruchi, thank you very much. Your code is much like the one pclement linked me to. Saw your answer (the same as this one - whatfor the 10 points?), the development of comments was the same as in this question: Net Start..., then API. Thanks.
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2631847
No problem, Robbert. ;-)
0
 
LVL 15

Author Comment

by:robbert
ID: 2631870
Tip: Use :-) for being happy and, ;-) for being ironical.
:-)
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2631892
Okay.. Thanks. :-)
0
 
LVL 1

Expert Comment

by:pclement
ID: 2632131
robbert:

Excellent! Glad it works for you.

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 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

17 Experts available now in Live!

Get 1:1 Help Now