?
Solved

windows services

Posted on 2004-11-18
3
Medium Priority
?
168 Views
Last Modified: 2010-04-24
Is there any articles on serviceController?

I am trying to create a program the starts and stops services in windows

I am looking for some code or some way to loop through services that are currently running on a either win2k or xp.

Once I have that I should be good.



0
Comment
Question by:malanois
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 2000 total points
ID: 12617199
'Ref to System.ServiceProcesses.dll

Public Class frmMain
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call ReadSvcs()
    End Sub

    Sub ReadSvcs()
        Dim ListSvcs() As ServiceProcess.ServiceController
        Dim SingleSvc As ServiceProcess.ServiceController
        Dim LVW As ListViewItem

        ListSvcs = SingleSvc.GetServices

        lvwServices.Items.Clear()
        Try
            For Each SingleSvc In ListSvcs
                LVW = lvwServices.Items.Add(SingleSvc.DisplayName)
                LVW.SubItems.Add(SingleSvc.ServiceName)
                LVW.SubItems.Add(SingleSvc.Status.ToString)
                LVW.SubItems.Add(SingleSvc.ServiceType.ToString)
            Next
        Catch e As Exception
            MessageBox.Show("Could not initialize Windows Service engine.  Restarting your computer may work", "Fatal Error: " & e.Source)
        End Try
    End Sub

    Private Sub lvwServices_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwServices.Click 'parental
        If lvwServices.SelectedItems(0).Text <> "" Then
            Select Case lvwServices.SelectedItems(0).SubItems(2).Text
                Case "Stopped"
                    cmdStop.Enabled = False
                    cmdStart.Enabled = True

                Case "Running"
                    cmdStop.Enabled = True
                    cmdStart.Enabled = False

                Case Else
                    cmdStop.Enabled = False
                    cmdStart.Enabled = False
            End Select
        End If
    End Sub

    Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
        If lvwServices.SelectedItems(0).Text <> "" Then
            Call StopService(lvwServices.SelectedItems(0).SubItems(1).Text)
        End If 'It has
    End Sub

    Sub StopService(ByVal ServiceName As String)
        Dim ListSvcs() As ServiceProcess.ServiceController
        Dim SingleSvc As ServiceProcess.ServiceController

        ListSvcs = SingleSvc.GetServices

        Try
            For Each SingleSvc In ListSvcs
                If UCase(SingleSvc.ServiceName) = UCase(ServiceName) Then
                    SingleSvc.Stop()
                    Call ReadSvcs()
                    Exit For
                End If
            Next
        Catch e As Exception
            MessageBox.Show("Could not stop service.  Ensure it is not disabled", "Fatal Error: " & e.Source)
        End Try
    End Sub

    Sub StartService(ByVal ServiceName As String)
        Dim ListSvcs() As ServiceProcess.ServiceController
        Dim SingleSvc As ServiceProcess.ServiceController

        ListSvcs = SingleSvc.GetServices

        Try
            For Each SingleSvc In ListSvcs
                If UCase(SingleSvc.ServiceName) = UCase(ServiceName) Then
                    SingleSvc.Start()
                    Call ReadSvcs()
                    Exit For
                End If
            Next
        Catch e As Exception
            MessageBox.Show("Could not start service.  Ensure it is not disabled", "Fatal Error: " & e.Source)
        End Try
    End Sub

    Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
        If lvwServices.SelectedItems(0).Text <> "" Then
            Call StartService(lvwServices.SelectedItems(0).SubItems(1).Text)
        End If
    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        End
    End Sub

    Private Sub cmdRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRefresh.Click
        Call ReadSvcs()
    End Sub

End Class
0
 

Author Comment

by:malanois
ID: 12617977
ok,

I have created a new project, copied and pasted the above code.
added button for , start, stop,refresh, and exit
added a listview

the first several tries I get an error for:
 lvwServices.Items.Clear()


I have rebooted several times and I get the same error everytime:

("Could not initialize Windows Service engine.  Restarting your computer may work"

XP srv Pk1

MJ
0
 

Author Comment

by:malanois
ID: 12627149
good now
 Thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

807 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