Arrêter/démarrer un service NT externe au programe

DavidT Messages postés 43 Date d'inscription lundi 31 décembre 2001 Statut Membre Dernière intervention 5 février 2005 - 14 mai 2002 à 19:58
ProblemeVB Messages postés 1 Date d'inscription mercredi 26 janvier 2005 Statut Membre Dernière intervention 26 janvier 2005 - 26 janv. 2005 à 17:54
Je souhaitepouvoir arrêter un service NT spécifique
Comment faire à partir de VB pour arrêter ou démarrer un service listé dans la liste services
Le service Explorateur d'Ordianteurs par exemple

Merci de vos réponses.

4 réponses

DavidT Messages postés 43 Date d'inscription lundi 31 décembre 2001 Statut Membre Dernière intervention 5 février 2005
15 mai 2002 à 08:56
C'est bon, je me suis trouvé moi même la réponse.
Bon... pour enrichir ce site qui est très bien fait :
la réponse à Comment arrêter, démarrer un service externe est sur ce site..

Testé sur NT4, Windows 2000, XP Ok

http://www.andreavb.com/tip060002.html

Merci pour ceux qui ont essayé de chercher...
0
phtribaudeau Messages postés 14 Date d'inscription mercredi 15 janvier 2003 Statut Membre Dernière intervention 16 novembre 2004
15 juil. 2003 à 12:16
Salut DavidT,
Ton message date depuis mai 2002, mais je serais interesse de savoir comment tu as fait pour arreter et relancer un service NT4 en VB.
Peux-tu m'envoyer des sources qui font cela ?

D'avance, je te remercie.

PhTribaudeau
0
DavidT Messages postés 43 Date d'inscription lundi 31 décembre 2001 Statut Membre Dernière intervention 5 février 2005
15 juil. 2003 à 12:39
Option Explicit
'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
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize 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

Public Function StopOfceNT() As Boolean
Dim MyStatus As String, sProcess As Long

sProcess = FindProcessID("PccNTMon.exe")

Call logs("StopOfceNT: sProcess PccNTMon.exe=" & sProcess)
Call logs("StopOfceNT: Kill PccNTMon.exe process")
Call NTProcess.ProcessTerminate(sProcess)

sProcess = FindProcessID("PccNTUpd.exe")

Call logs("StopOfceNT: sProcess PccNTUpd.exe=" & sProcess)
Call logs("StopOfceNT: Kill PccNTUpd.exe process")
Call NTProcess.ProcessTerminate(sProcess)

MyStatus = ServiceStatus("", "NTRTSCAN")

Call logs("StopOfceNT:NTRTSCAN status = " & MyStatus)

Do Until MyStatus = "Stopped"

If MyStatus = "Running" Then
Call logs("StopOfceNT:NTRTSCAN status = " & MyStatus & "=> send event to Stopping")
ServiceStop "", "NTRTSCAN"
End If

If MyStatus = "Stop Pending" Then
Call logs("StopOfceNT:NTRTSCAN status = " & MyStatus & "=> Waiting")
Sleep 50
End If

If MyStatus = "Stopped" Then Exit Function

MyStatus = ServiceStatus("", "NTRTSCAN")
Loop

MyStatus = ServiceStatus("", "TMLISTEN")

Do Until MyStatus = "Stopped"
MyStatus = ServiceStatus("", "TMLISTEN")
Call logs("StopOfceNT:NTRTSCAN status = " & MyStatus)
If MyStatus = "Running" Then
ServiceStop "", "TMLISTEN"
End If

If MyStatus = "Stop Pending" Then
Sleep 50
End If
Loop
End Function
Public Function StartOfceNT() As Boolean
Dim MyStatus As String, sCount As Integer
Dim sl As New OfficeScanValues

If FindProcessID("PccNTMon.exe") = 0 Then

If Dir(sl.OfficeScan_ApplicationPath & "\PccNTMon.exe") <> vbNullString Then Shell sl.OfficeScan_ApplicationPath & "\PccNTMon.exe", vbNormalFocus
Set sl = Nothing
End If


MyStatus = ServiceStatus("", "NTRTSCAN")

If MyStatus = "Running" Then Exit Function

Do Until MyStatus = "Running"
MyStatus = ServiceStatus("", "NTRTSCAN")

If MyStatus = "Stopped" Then
Call logs("StartOfceNT:NTRTSCAN status = " & MyStatus & "=>Reload")
ServiceStart "", "NTRTSCAN"
sCount = sCount + 1


End If

If MyStatus = "Start Pending" Then
Call logs("StartOfceNT:NTRTSCAN status = " & MyStatus & "=>Starting")
Sleep 50
End If
If sCount > 200 Then Exit Function
Loop

sCount = 0
MyStatus = ServiceStatus("", "TMLISTEN")

If MyStatus = "Running" Then Exit Function

Do Until MyStatus = "Running"
MyStatus = ServiceStatus("", "TMLISTEN")

If MyStatus = "Stopped" Then
ServiceStart "", "TMLISTEN"
sCount = sCount + 1
End If

If MyStatus = "Start Pending" Then
Sleep 50
End If

If sCount > 200 Then Exit Function
Loop

End Function

Public Function ReloadOfceNT()

Dim MyOs As String
MyOs = PlateForme

Select Case MyOs

Case "NT"

Call StopOfceNT
Call StartOfceNT

Case "9x"
Call StopOfce95
Call StartOfficeScan95
End Select

End Function
Public Function StopOfficescan()

Dim MyOs As String
MyOs = PlateForme

Call logs("StopOfficescan: OS=" & MyOs)

Select Case MyOs

Case "NT"
Call StopOfceNT
Case "9x"
Call StopOfce95
End Select

End Function
Public Function StartOfficeScan()

Dim MyOs As String
MyOs = PlateForme

Select Case MyOs

Case "NT"
Call StartOfceNT
Case "9x"
Call StartOfficeScan95
End Select

End Function
Public Function MyComputerName() As String
Dim a As String * 256, X As String
X = GetComputerName(a, 256)
MyComputerName = Left(a, InStr(a, Chr(0)) - 1)
End Function
0
ProblemeVB Messages postés 1 Date d'inscription mercredi 26 janvier 2005 Statut Membre Dernière intervention 26 janvier 2005
26 janv. 2005 à 17:54
Salut DavidT,
Je suis bien allée sur le lien comme indiqué : http://www.andreavb.com/tip060002.html.
J'ai fais un copier-coller du code mais cela ne marche pas.
Comment je peux faire pour arrêter un service NT sur ma machine local et sur une autre machine remote?

Merci pour ta réponse
0
Rejoignez-nous