Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Managing services
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Managing services
Versions des environnements
Environment:
VB 9.0
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01622337
Message ID:
01622337
Vues:
39
As the code to manage services is in .NET, I created a new thread in this forum for you. Here is my class. You may take whatever you want and adjust accordingly in VFP.
Public Class WindowsService

    Public cDomain As String = ""
    Public cIP As String = ""
    Public cMessage As String = ""
    Public cOutput As String = ""
    Public cPassword As String = ""
    Public cPath As String = ""
    Public cService As String = ""
    Public cStatus As String = ""
    Public cUsername As String = ""
    Private cIPIsMandatory As String = ""
    Private cPasswordIsMandatory As String = ""
    Private cServiceNameIsMandatory As String = ""
    Private cUsernameIsMandatory As String = ""
    Private cUnableToCreateService As String = ""
    Private cUnableToDeleteService As String = ""
    Private cUnableToStartService As String = ""
    Private cUnableToStopService As String = ""
    Private nLanguage As Integer = 1
    Private oApp As App = Nothing
    Private oProcess As LXProcess = Nothing
    Private oTaskGroup As Collection = New Collection

    ' This is when we access the data provider in desktop mode
    Sub New(ByVal toApplication As App)
        oApp = toApplication
        nLanguage = oApp.nLanguage
        Init()
    End Sub

    ' This is when we access the data provider in Web or Web service mode
    Sub New(ByVal toProcess As LXProcess)
        oProcess = toProcess
        oApp = oProcess.oApp
        nLanguage = oProcess.nLanguage
        Init()
    End Sub

    ' Initialization
    Private Sub Init()

        ' Based on the language
        Select Case nLanguage

            ' English
            Case 1
                cIPIsMandatory = "The IP address is mandatory."
                cPasswordIsMandatory = "The RunAs password is mandatory."
                cServiceNameIsMandatory = "The service name is mandatory."
                cUsernameIsMandatory = "The RunAs username is mandatory."
                cUnableToCreateService = "Unable to create ##Service## service."
                cUnableToDeleteService = "Unable to delete ##Service## service."
                cUnableToStartService = "Unable to start ##Service## service."
                cUnableToStopService = "Unable to stop ##Service## service."

                ' French
            Case 2
                cIPIsMandatory = "L'adresse IP de l'ordinateur est obligatoire."
                cPasswordIsMandatory = "Le RunAs mot de passe est obligatoire."
                cServiceNameIsMandatory = "Le nom du service est obligatoire."
                cUsernameIsMandatory = "Le RunAs code d'usager est obligatoire"
                cUnableToCreateService = "Impossible de créer le service ##Service#."
                cUnableToDeleteService = "Impossible d'enlever le service ##Service#."
                cUnableToStartService = "Impossible de créer le service ##Service##."
                cUnableToStopService = "Impossible d'arrêter le service ##Service##."

                ' Spanish
            Case 3
                cIPIsMandatory = "The IP address is mandatory."
                cPasswordIsMandatory = "The RunAs password is mandatory."
                cServiceNameIsMandatory = "The service name is mandatory."
                cUsernameIsMandatory = "The RunAs username is mandatory."
                cUnableToCreateService = "Unable to create ##Service## service."
                cUnableToDeleteService = "Unable to delete ##Service## service."
                cUnableToStartService = "Unable to start ##Service## service."
                cUnableToStopService = "Unable to stop ##Service## service."

                ' Portuguese
            Case 4
                cIPIsMandatory = "The IP address is mandatory."
                cPasswordIsMandatory = "The RunAs password is mandatory."
                cServiceNameIsMandatory = "The service name is mandatory."
                cUsernameIsMandatory = "The RunAs username is mandatory."
                cUnableToCreateService = "Unable to create ##Service## service."
                cUnableToDeleteService = "Unable to delete ##Service## service."
                cUnableToStartService = "Unable to start ##Service## service."
                cUnableToStopService = "Unable to stop ##Service## service."

        End Select

    End Sub

    ' Start a service
    Public Function StartService() As Boolean
        Dim lcIP As String = ""
        Dim lcService As String = ""
        Dim loLogData As LogData = Nothing
        Dim loProcessFile As ProcessFile = Nothing

        ' Get the proper definition as per the current scope
        If oProcess Is Nothing Then
            loLogData = New LogData(oApp)
            loProcessFile = New ProcessFile(oApp)
        Else
            loLogData = New LogData(oProcess)
            loProcessFile = New ProcessFile(oProcess)
        End If

        ' Reset the values
        cMessage = ""
        cOutput = ""

        ' Initialization
        lcIP = Trim(cIP)
        lcService = Trim(cService)

        ' If we do not have a service
        If lcService.Length = 0 Then
            cMessage = cServiceNameIsMandatory
            Return False
        End If

        ' If we do not have an IP
        If lcIP.Length = 0 Then
            cMessage = cIPIsMandatory
            Return False
        End If

        ' If we are logging
        If oProcess.lLogData Then

            ' Start the log
            loLogData.cTitle = "c:\Windows\System32\sc.exe \\" + lcIP + " start """ + lcService + """"
            If Not loLogData.StartLog() Then
                Return False
            End If

        End If

        ' Enable the task
        loProcessFile.cWorkingDirectory = "c:\Windows\System32"
        loProcessFile.cFileName = "c:\Windows\System32\sc.exe"
        loProcessFile.cArguments = "\\" + lcIP + " start """ + lcService + """"
        If Not loProcessFile.Process() Then
            cMessage = loProcessFile.cMessage
            Return False
        End If

        ' If we are logging
        If oProcess.lLogData Then

            ' Stop the log
            loLogData.cExtra = loProcessFile.cLog
            If Not loLogData.StopLog() Then
                Return False
            End If

        End If

        ' If we have an error
        If loProcessFile.cError.Length > 0 Then
            cMessage = oApp.StrTran(cUnableToStartService, "##Service##", lcService)
            cMessage = cMessage + oApp.cCR + oApp.cCR + oApp.StrTran(loProcessFile.cError, "ERROR: ", "") + _
             "sc \\" + lcIP + " start """ + lcService + """"
            Return False
        End If

        ' Output
        cOutput = loProcessFile.cOutput

        ' If we have the word FAILED
        If oApp.At("FAILED", cOutput) Then
            cMessage = oApp.StrTran(cUnableToStartService, "##Service##", lcService)
            cMessage = cMessage + oApp.cCR + oApp.cCR + cOutput + oApp.cCR + oApp.cCR + _
             "sc \\" + lcIP + " start """ + lcService + """"
            Return False
        End If

        ' Reset the values
        cDomain = ""
        cIP = ""
        cPassword = ""
        cPath = ""
        cService = ""
        cUsername = ""

        Return True
    End Function

    ' Stop a service
    Public Function StopService() As Boolean
        Dim lcIP As String = ""
        Dim lcService As String = ""
        Dim loLogData As LogData = Nothing
        Dim loProcessFile As ProcessFile = Nothing

        ' Get the proper definition as per the current scope
        If oProcess Is Nothing Then
            loLogData = New LogData(oApp)
            loProcessFile = New ProcessFile(oApp)
        Else
            loLogData = New LogData(oProcess)
            loProcessFile = New ProcessFile(oProcess)
        End If

        ' Reset the values
        cMessage = ""
        cOutput = ""

        ' Initialization
        lcIP = Trim(cIP)
        lcService = Trim(cService)

        ' If we do not have a service
        If lcService.Length = 0 Then
            cMessage = cServiceNameIsMandatory
            Return False
        End If

        ' If we do not have an IP
        If lcIP.Length = 0 Then
            cMessage = cIPIsMandatory
            Return False
        End If

        ' If we are logging
        If oProcess.lLogData Then

            ' Start the log
            loLogData.cTitle = "c:\Windows\System32\sc.exe \\" + lcIP + " stop """ + lcService + """"
            If Not loLogData.StartLog() Then
                Return False
            End If

        End If

        ' Stop the task
        loProcessFile.cWorkingDirectory = "c:\Windows\System32"
        loProcessFile.cFileName = "c:\Windows\System32\sc.exe"
        loProcessFile.cArguments = "\\" + lcIP + " stop """ + lcService + """"
        If Not loProcessFile.Process() Then
            cMessage = loProcessFile.cMessage
            Return False
        End If

        ' If we are logging
        If oProcess.lLogData Then

            ' Stop the log
            loLogData.cExtra = loProcessFile.cLog
            If Not loLogData.StopLog() Then
                Return False
            End If

        End If

        ' If we have an error
        If loProcessFile.cError.Length > 0 Then
            cMessage = oApp.StrTran(cUnableToStopService, "##Service##", lcService)
            cMessage = cMessage + oApp.cCR + oApp.cCR + oApp.StrTran(loProcessFile.cError, "ERROR: ", "") + _
             "sc \\" + lcIP + " stop """ + lcService + """"
            Return False
        End If

        ' Output
        cOutput = loProcessFile.cOutput

        ' If we have the word FAILED
        If oApp.At("FAILED", cOutput) Then
            cMessage = oApp.StrTran(cUnableToStopService, "##Service##", lcService)
            cMessage = cMessage + oApp.cCR + oApp.cCR + cOutput + oApp.cCR + oApp.cCR + _
             "sc \\" + lcIP + " stop """ + lcService + """"
            Return False
        End If

        ' Reset the values
        cDomain = ""
        cIP = ""
        cPassword = ""
        cPath = ""
        cService = ""
        cUsername = ""

        Return True
    End Function

    ' Create service
    Public Function CreateService() As Boolean
        Dim lcAccount As String = ""
        Dim lcDomain As String = ""
        Dim lcIP As String = ""
        Dim lcPassword As String = ""
        Dim lcPath As String = ""
        Dim lcService As String = ""
        Dim lcUsername As String = ""
        Dim loLogData As LogData = Nothing
        Dim loProcessFile As ProcessFile = Nothing

        ' Get the proper definition as per the current scope
        If oProcess Is Nothing Then
            loLogData = New LogData(oApp)
            loProcessFile = New ProcessFile(oApp)
        Else
            loLogData = New LogData(oProcess)
            loProcessFile = New ProcessFile(oProcess)
        End If

        ' Reset the values
        cMessage = ""
        cOutput = ""

        ' Initialization
        lcDomain = Trim(cDomain)
        lcIP = Trim(cIP)
        lcPassword = Trim(cPassword)
        lcPath = Trim(cPath)
        lcService = Trim(cService)
        lcUsername = Trim(cUsername)

        ' If we have a domain
        If lcDomain.Length > 0 Then
            lcAccount = lcDomain + "\" + lcUsername
        Else
            lcAccount = lcUsername
        End If

        ' If we do not have a service
        If lcService.Length = 0 Then
            cMessage = cServiceNameIsMandatory
            Return False
        End If

        ' If we do not have an IP
        If lcIP.Length = 0 Then
            cMessage = cIPIsMandatory
            Return False
        End If

        ' If we do not have a username
        If lcUsername.Length = 0 Then
            cMessage = cUsernameIsMandatory
            Return False
        End If

        ' If we do not have a password
        If lcPassword.Length = 0 Then
            cMessage = cPasswordIsMandatory
            Return False
        End If

        ' Create the service
        loProcessFile.cWorkingDirectory = "c:\Windows\System32"
        loProcessFile.cFileName = "c:\Windows\System32\sc.exe"
        loProcessFile.cArguments = "\\" + lcIP + " create """ + lcService + """ binPath= """ + lcPath + """ " + _
         "start= demand obj= """ + lcAccount + """ password= " + lcPassword
        If Not loProcessFile.Process() Then
            cMessage = loProcessFile.cMessage
            Return False
        End If

        ' If we have an error
        If loProcessFile.cError.Length > 0 Then
            cMessage = oApp.StrTran(cUnableToCreateService, "##Service##", lcService)
            cMessage = cMessage + oApp.cCR + oApp.cCR + oApp.StrTran(loProcessFile.cError, "ERROR: ", "") + _
             "sc \\" + lcIP + " create """ + lcService + """ binPath= """ + lcPath + """ " + "start= demand obj= """ + lcAccount + """"
            Return False
        End If

        ' Output
        cOutput = loProcessFile.cOutput

        ' If we have the word FAILED
        If oApp.At("FAILED", cOutput) Then
            cMessage = oApp.StrTran(cUnableToCreateService, "##Service##", lcService)
            cMessage = cMessage + oApp.cCR + oApp.cCR + cOutput + oApp.cCR + oApp.cCR + _
             "sc \\" + lcIP + " create """ + lcService + """ binPath= """ + lcPath + """ start= demand obj= """ + lcAccount + """"
            Return False
        End If

        ' Reset the values
        cDomain = ""
        cIP = ""
        cPassword = ""
        cPath = ""
        cService = ""
        cUsername = ""

        Return True
    End Function

    ' Delete a service
    Public Function DeleteService() As Boolean
        Dim lcIP As String = ""
        Dim lcService As String = ""
        Dim loLogData As LogData = Nothing
        Dim loProcessFile As ProcessFile = Nothing

        ' Get the proper definition as per the current scope
        If oProcess Is Nothing Then
            loLogData = New LogData(oApp)
            loProcessFile = New ProcessFile(oApp)
        Else
            loLogData = New LogData(oProcess)
            loProcessFile = New ProcessFile(oProcess)
        End If

        ' Reset the values
        cMessage = ""
        cOutput = ""

        ' Initialization
        lcIP = Trim(cIP)
        lcService = Trim(cService)

        ' If we do not have a service
        If lcService.Length = 0 Then
            cMessage = cServiceNameIsMandatory
            Return False
        End If

        ' If we do not have an IP
        If lcIP.Length = 0 Then
            cMessage = cIPIsMandatory
            Return False
        End If

        ' Delete the service
        loProcessFile.cWorkingDirectory = "c:\Windows\System32"
        loProcessFile.cFileName = "c:\Windows\System32\sc.exe"
        loProcessFile.cArguments = "\\" + lcIP + " delete """ + lcService + """"
        If Not loProcessFile.Process() Then
            cMessage = loProcessFile.cMessage
            Return False
        End If

        ' If we have an error
        If loProcessFile.cError.Length > 0 Then
            cMessage = oApp.StrTran(cUnableToDeleteService, "##Service##", lcService)
            cMessage = cMessage + oApp.cCR + oApp.cCR + oApp.StrTran(loProcessFile.cError, "ERROR: ", "") + _
             "sc \\" + lcIP + " delete """ + lcService + """"
            Return False
        End If

        ' Output
        cOutput = loProcessFile.cOutput

        ' If we have the word FAILED
        If oApp.At("FAILED", cOutput) Then
            cMessage = oApp.StrTran(cUnableToDeleteService, "##Service##", lcService)
            cMessage = cMessage + oApp.cCR + oApp.cCR + cOutput + oApp.cCR + oApp.cCR + _
             "sc \\" + lcIP + " delete """ + lcService + """"
            Return False
        End If

        ' Reset the values
        cIP = ""
        cService = ""

        Return True
    End Function

End Class
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform