Catch Win32 Services

The following code will allow you to catch running Win32Services

The Code:
Option Explicit On

Imports System
Imports System.Management

Public Class Form1

Private MgClass As New Management.ManagementClass("Win32_Service")

Private Sub ButtonLoadServices_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLoadServices.Click

Try
Me.Cursor = Cursors.WaitCursor

ListView1.Items.Clear()

Label3.Text = ""
Label5.Text = ""
Label7.Text = ""
Label9.Text = ""

ButtonLoadServices.Enabled = False
ButtonLoadServices.Refresh()

Label1.Text = "0% Complete."
Label1.Refresh()

With ProgressBar1
.Value = 0
.Minimum = 0
.Maximum = MgClass.GetInstances().Count
.Refresh()
End With

For Each obj As Management.ManagementObject In MgClass.GetInstances()
Application.DoEvents()

Dim MHeader As New ListViewItem()
Dim SHeader As New ListViewItem.ListViewSubItem()

MHeader.Text = obj.GetPropertyValue("Caption").ToString

If obj.GetPropertyValue("Description") <> "" Then
SHeader.Text = obj.GetPropertyValue("Description").ToString()
End If

ListView1.Items.Add(MHeader).SubItems.Add(SHeader)

ProgressBar1.Value += 1
ProgressBar1.Refresh()

Label1.Text = ((ProgressBar1.Value / ProgressBar1.Maximum) * 100).ToString("0.00") & "% Complete."
Label1.Refresh()
Next

ButtonLoadServices.Enabled = True
ButtonLoadServices.Refresh()
Me.Cursor = Cursors.Default
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
ButtonLoadServices.Enabled = True
ButtonLoadServices.Refresh()
Me.Cursor = Cursors.Default
End Try

End Sub

Private Sub ListView1_AfterLabelEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LabelEditEventArgs) Handles ListView1.AfterLabelEdit

e.CancelEdit = True

End Sub

Private Sub ListView1_BeforeLabelEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LabelEditEventArgs) Handles ListView1.BeforeLabelEdit

e.CancelEdit = True

End Sub

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

Label3.Text = ""
Label5.Text = ""
Label7.Text = ""
Label9.Text = ""

End Sub

Private Sub ListView1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick

Dim SelectItem As String = ""
Dim SelRow As Integer = 0

Try
Me.Cursor = Cursors.WaitCursor

SelRow = ListView1.SelectedItems.Item(0).Index

SelectItem += """"
SelectItem += ListView1.Items.Item(SelRow).Text.ToString()
SelectItem += """"

Dim SelQuery As New SelectQuery("Win32_Service", "Caption=" & SelectItem & "")
Dim ObjectSearcher As New ManagementObjectSearcher(SelQuery)

For Each service As ManagementObject In ObjectSearcher.Get()
Label3.Text = service.GetPropertyValue("PathName")
Label5.Text = service.GetPropertyValue("ServiceType")
Label7.Text = service.GetPropertyValue("StartMode")
Label9.Text = service.GetPropertyValue("State")
Exit For
Next

SelQuery = Nothing
ObjectSearcher.Dispose()
Me.Cursor = Cursors.Default
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.Cursor = Cursors.Default
End Try

End Sub

End Class

0 comments:

Post a Comment