Using the service controller in VB.Net

Using the service controller is really very easy.

All of my applications come about from something I’m working on at the time. This time I needed a way for my mom to temporarily stop the Microsoft Security Essentials  service which is named “MsMpSvc”, there is no built in way that I can find.  As with all AV/Antimalware programs they some times hog system resources and on a single core processor it is really bad.

The normal way a person would stop a service is,  thru the admin services utility. And depending on how a system is set up would allow for different ways to get there.

While looking to see if something was already made to do the job that I wanted to do, I ran across a help page that suggested to use the the Service Controller for there project.

The service controller  is  located in the (I’m using 2008 standard edition) Visual Studio Tools under the components section. It has been a while since I first used one of these so I had to relearn how to use it.

Here Is what the completed Program Looks Like

MSAVstoper

This program is really bare minimum and took about 3 hours to build and test(a couple more later to update). As you can see I have the start and stop buttons for the service . I could have stopped there but I also added the message at the top to tell the user If the service was actually installed or not. I used the list of installed services as a quick way to check for the service name to see if it was actually installed or not. I probably could have used a collection of Services then checked that but I didn’t think of it at the time. So we get a list of installed services as a bonus.

Here is what the program looks like inside of Visual Studio.

MsstoppinVS

We need to to add 5 labels, 2 buttons , and 1 list box to the form giving everything a name . Next We add the Import Statements to the code section for  “Imports System” , “Imports System.Management” then add the reference for the System . I like to add a label and a About box to the project so I can verify my versions as I’m building and testing , and can be handy later when an end user has a question you can have them give you the version they are using. Next we add the Service Control Module and set the property ServiceName: to  MsMpSvc  as you see below:

ServiceContollerProp

The Code :

Below Is the code used for this program:

 

Imports System Imports System.Management Imports System.ServiceProcess Imports System.Diagnostics Public Class Form1 Private SvcStatus As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load IsServicePresent() End Sub Private Sub IsServicePresent() Try Dim svcClass As ManagementClass = New ManagementClass("Win32_Service") For Each svcObj As ManagementObject In svcClass.GetInstances ListBoxInstalledServices.Items.Add(svcObj.Item("Name")) Next If ListBoxInstalledServices.Items.Contains("MsMpSvc") = True Then lblServiceExist.Text = "The Service MsMpSvc Exist" lblServiceExist.ForeColor = Color.DarkSeaGreen ElseIf ListBoxInstalledServices.Items.Contains("MsMpSvc") = False Then lblServiceExist.Text = "The Service MsMpSvcDoes Not Exist " & vbNewLine & "You Can Not Use This Program To Control The Service MsMpSvc" lblServiceExist.ForeColor = Color.DarkRed btnStartService.Enabled = False btnStopService.Enabled = False End If Catch ex As Exception MsgBox("Error" & vbNewLine & ex.Message.ToString, MsgBoxStyle.Information, "Error Service Verification") End Try End Sub Private Sub btnStopService_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopService.Click Try If IsServiceRunning() = "Stopped" Then MsgBox("The Service MsMpSvc Is Already Stopped", MsgBoxStyle.Information, "Service Status") ElseIf IsServiceRunning() = "Running" Then ServiceController1.Stop() End If ' the Service is already named in the propertys of the ServiceControl module Catch ex As Exception MsgBox("An Error Occured While Trying to Stop the Service" & vbNewLine & ex.Message.ToString, MsgBoxStyle.Information, "Stop Service Error") End Try End Sub Private Sub btnStartService_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartService.Click Try If IsServiceRunning() = "Running" Then MsgBox("The Service MsMpSvc Is Already Running", MsgBoxStyle.Information, "Service Status") ElseIf IsServiceRunning() = "Stopped" Then ServiceController1.Start() End If Catch ex As Exception MsgBox("Error While Trying to Start The Service" & vbNewLine & ex.Message.ToString, MsgBoxStyle.Information, "Start Service Error") End Try End Sub Private Sub lblAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblAbout.Click AboutBox1.Show() End Sub Private Function IsServiceRunning() ServiceController1.Refresh() SvcStatus = ServiceController1.Status().ToString Return SvcStatus End Function End Class

In the Form Load section we pass off to a Sub called IsServicePresent() . We then fill the list box and check if the service we want is listed, If Yes then we set the label text and the color to 1 message  , “ElseIf” , No then we set the label text and color to another message. All before the form  gets loaded. I also updated the code to disable the buttons if the service is not present. I use ElseIf To be sure the Argument gets evaluated to True or False.

Once the Form is loaded then it is just a matter for the user to click start or stop. Note that the service could take up to around 20 seconds to start or stop the service depending on the system. The user will Know when this happens by the alert in the notification area will pop up and turn red with a warning and then turn green after the service is restarted.

That’s pretty much it for the program as it stands.

It does need more Code to handle:

1: One thing I haven’t  figured out how to do, is to check if a status is  pending for starting or stopping. I was able to add for if the program was started or stopped already though. Everything I tried broke the program when trying to check for pending status. Possibly add a timer to keep checking every few seconds what the status is.

FYI : This service does not show up on the Windows 8 Developers preview.I tested this and a few other programs on it.

About these ads

About pcsxcetrasupport3

My part time Business, I mainly do system building and system repair. Over the last several years I have been building system utility's in vb script , HTA applications and VB.Net to be able to better find the information I need to better understand the systems problems in order to get the systems repaired and back to my customers quicker.
This entry was posted in Anti-virus, CodeProject, VB.net and tagged , . Bookmark the permalink.

2 Responses to Using the service controller in VB.Net

  1. you have to add the Reference for it.
    Right click on the projet name >Left click “Add Reference” > Scroll down the .”Net” list till you find “System.Management” select that then click ok.
    If you don’t already have the imports added then add it at the top “Imports System.Management”
    Then it should be ok.

  2. DaveH says:

    hi
    i m new in programming
    i m testing this code but he said that managementClass is not defined ?
    thanks

Comments are closed.