Windows Service

This is the easiest way to create your own true NT service. Just look at the testService project. This has all the code you need.

Instructions: Before opening the testService project you first need to compile or register EVICT_Service.dll and NTSVC.ocx. NTSVC.ocx is created by Microsoft and written in Visual C. Remember that you always run the compiled version (the testService recognizes this). Otherwise the VB IDE will be registered as the service.

If you run the test service with desktop interaction then you can see the status in the modified title in the task manager. Running the executable will start up the service setup. Also look what happens if you start the executable twice. If you look at the services (in computer management) then you must be aware that it needs a refresh for showing an changed state.

Click here to download the complete source code, the compiled DLL and the demo.

NT Service

[sourcecode language=”vb”]
Public WithEvents service As EVICT_Service.service
Dim strSaveTitle As String

Private Sub Form_Load()
strSaveTitle = App.Title
‘ You probably do not want the IDE to become a service
If isExe = False Then
If MsgBox(“If you are running inside the VB IDE environment, then you are not able to run as a service otherwize you would make a service out of the IDE. But then you would probably like to debug the service Code. so if you like you can still run the code. Press OK to continue or Cancel to stop.”, vbOKCancel, “You are running in the VB IDE” ) = vbCancel Then End
Service_StartApp ‘ For debugging in the IDE
Exit Sub
End If

‘ Make a service out of this application
Set service = New EVICT_Service.service
service.ServiceName = “myTestService”
service.ServiceTitle = “This is my own test service”
service.ServiceInteractive = False
service.ServiceStartMode = svcStartAutomatic
service.ServiceControlsAccepted = svcCtrlPauseContinue
service.WriteLog = True
‘ You can use the following 2 variables if you like. You probably don’t need these
Debug.Print service.isServiceInstalled
Debug.Print service.ServiceState
End Sub

Private Sub Service_StartApp()

App.Title = strSaveTitle & ” (Started)”
‘ Put here your code for starting the service
End Sub

Private Sub service_StartSetup()
App.Title = strSaveTitle & ” (Setup)”
‘ Just show that we entered startup mode
End Sub

Private Sub Service_ContinueApp()
App.Title = strSaveTitle & ” (Restarted)”
‘ Put here your code for a restart of the service
End Sub

Private Sub service_Control(lngEventNum As Long)
App.Title = strSaveTitle & ” (Event=” & lngEventNum & “)”
‘ Put here your code for control events
End Sub

Private Sub Service_PauseApp()
App.Title = strSaveTitle & ” (Paused)”
‘ Put here your code for pausing a service
End Sub

Private Sub Service_StopApp()
‘ This one will not show because we will be unloade automaticaly
App.Title = strSaveTitle & ” (Stopping)”
‘ Put here your code for stopping the service (like unloading created objects)
End Sub

Private Sub service_ExitSetup()
‘ Since we did a setup and are now ready we can quit.
Unload Me
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set service = Nothing
End Sub

‘ Test if we are running in the IDE or in a EXE
Private Function isExe() As Boolean
On Error Resume Next
Debug.Print 1 / 0
If Err.Number = 0 Then
isExe = True
isExe = False
End If
End Function

Leave a Reply

Playground of Edwin Vermeer