TUT: self checking service 06-20-2011, 12:39 PM
#1
Let's create a service in the background. To make a bit more fun, we'll let the service check if it has been found and killed by the user, if so, it will restart automatically after a random period
Ok, to be able to install a service you'll probably need admin rights.. that part i'll leave out of this tut, up to you to figure it out
The code for the service: (i've named it 'iehost' for this example)
So, compile as a service and install... whenever you manually kill the process, it will check if the registry value still exists... if so, it will relaunch after a random period.. hard to kill unless you know which registry value has to be changed
Happy coding
If anyone needs an example of above service, PM me. I have a working example checking for certain opened internet pages; if found they will be auto replaced by another page.
Ok, to be able to install a service you'll probably need admin rights.. that part i'll leave out of this tut, up to you to figure it out
The code for the service: (i've named it 'iehost' for this example)
Code:
Imports System
Imports System.IO
Imports Microsoft.Win32
Public Class iehost
Inherits System.ServiceProcess.ServiceBase
Private WithEvents timer2 As System.Timers.Timer
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things in motion so your service can do its work.
' Initialise a timer...
Me.timer2 = New System.Timers.Timer(10000)
Me.timer2.Enabled = True
timer2.Start()
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
'don't just stop, prompt for a psw first or in this case:
'check for reg key
If Me.checkRegistration = False Then
'have more fun, use rnd value... :)
Dim waitTime As Integer = 10000
Dim rndAss As New Random
waitTime = rndAss.Next(10000, 60000)
'resend command line arg
Try
Process.Start(My.Application.Info.DirectoryPath & "\iehost.exe", "/i /t " & waitTime)
Exit Sub
Catch ex As Exception
End Try
End If
timer2.Stop()
End Sub
Public Function checkRegistration() As Boolean
'first check if registrationKey exists
Dim returnKey As String
Dim deKey As RegistryKey
Try
deKey = Registry.LocalMachine.OpenSubKey("Software\iehost\4y6eds2fn", False)
returnKey = DirectCast(deKey.GetValue("version", "1.0.0"), String)
Catch ex As Exception
Return False
End Try
If returnKey = "1.0.1" Then
Return True
Else
Return False
End If
End Function
Protected Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer2.Elapsed
me.MethodToBeExecuted
End Sub
Protected Sub MethodToBeExecuted()
'here you can write whatever you want to be done (f.e. launch program...)
End SubSo, compile as a service and install... whenever you manually kill the process, it will check if the registry value still exists... if so, it will relaunch after a random period.. hard to kill unless you know which registry value has to be changed
Happy coding
If anyone needs an example of above service, PM me. I have a working example checking for certain opened internet pages; if found they will be auto replaced by another page.
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)

![[Image: rytwG00.png]](http://i.imgur.com/rytwG00.png)