Thursday, July 14, 2005

vbCity/DevCity.NET Forums :: Visual Basic :: VB General :: FAQ :: Creating WinNT Services in VB:

This is a demonstration of creating a NT Service in VB6. The same methode works in VB4 and VB5. Your OS must be NT4.0, W2000 or XP

An application that runs as a service is controlled by the system's Service Manager and runs in the background. Examples are mail servers and database servers. A service application needs to accept commands from the system like Start, Stop, Pause and Resume.

Since VB cannot communicate with the systems Service Manager (VB.NET can do that), we use a contol called NTSVC.OCX. NTSVC.OCX used to be on MSDN as unsupported software. It is included in the zip file.

First you need to register NTSVC.OCX with REGSVR32. After registering, you can add the Microsoft NT Service Control component to your project.

Our project will need one form to host the OCX and a Timer control , and one global module. The module takes care of installing and uninstalling the service. It is not much code, because NTSVC.OCX does all the work.

Code:
Option Explicit
Public Const SVCLOGFILE = "\servicelog.txt"

Public Sub subLogCommand(strCommand As String)
'*** log all commands to a text file
'*** if you like to log to the system event log,
'*** use frmService.NTService1.LogEvent svcEventInformation, svcMessageInfo, strCommand

Dim lngFileNum As Long

lngFileNum = FreeFile()
Open App.Path & SVCLOGFILE For Append As #lngFileNum
Print #lngFileNum, Format$(Date$, "YYYY/MM/DD"), Format$(Time$, "HH:MM:SS"), strCommand
Close #lngFileNum
End Sub

Public Sub Main()

'*** Write to the log
subLogCommand Command$

'*** select the right action to take
Select Case Trim$(Command$)

'*** tell the OCX to install the service and quit
Case "/install"
'*** set all defaults for the service here
With frmService.NTService1

'*** True if the service needs to interact with the user
.Interactive = False

'*** use these to read / write to the registry at
'*** HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\[SERVICE NAME]
'.SaveSetting
'.GetSetting
'.DeleteSetting
'.GetAllSettings

'*** for example write the Description shown in the Services window.
.SaveSetting "", "Description", "This is a Service written in VB6."

'*** set the startmode to manual by default
.StartMode = svcStartManual

'*** now install the service
.Install

'*** write a line to the system log
.LogEvent svcEventInformation, svcMessageInfo, "VB Service installed."
End With

Unload frmService

'*** tell the OCX to uninstall the service and quit
Case "/uninstall"
frmService.NTService1.Uninstall
Unload frmService

'*** start the service. simply keep the form loaded
Case ""
Load frmService
frmService.NTService1.StartService

'*** show a messagebox to show the commands.
'*** for your final application, consider removing this.
'*** better write to the NT event log.
Case Else
MsgBox "use " & App.EXEName & " /uninstall or /install..." & vbNewLine & _
"use 'Net Start " & frmService.NTService1.ServiceName & "' to START the service" & vbNewLine & _
"use 'Net Stop " & frmService.NTService1.ServiceName & "' to STOP the service" & vbNewLine & _
"use the Windows Services to set the Startup Type to AUTOMATIC."
Unload frmService

End Select

End Sub

Now we need the code to handle the commands. The commands are events fired by the OCX. The code of our form looks like this:

Code:
Option Explicit

Private Sub NTService1_Continue(Success As Boolean)
subLogCommand "continue"

'*** resume
timApplication.Interval = 60000

Success = True
End Sub

Private Sub NTService1_Pause(Success As Boolean)
subLogCommand "pause"

'*** pause the timer
timApplication.Interval = 0

Success = True
End Sub

Private Sub NTService1_Start(Success As Boolean)
subLogCommand "start"

'*** tell the servicemanager which control commands we accept
NTService1.ControlsAccepted = svcCtrlPauseContinue
NTService1.StartService

'*** run every minute
timApplication.Interval = 60000

Success = True
End Sub

Private Sub NTService1_Stop()
subLogCommand "stopped"

'*** cleanup time
timApplication.Interval = 0

End Sub

Private Sub timApplication_Timer()
'***
'*** YOUR CODE HERE
'***
subLogCommand "[TIMER] ..just to demonstrate that the service is running..."
End Sub

Note that the Timer_Event should start your Application.

To view your service running, run services.msc and look for VB NT Service.

I added four batch files to the zip to make it easier to work with the service during development.

Martijn

0 Comments:

Post a Comment

<< Home