PDA

Click to See Complete Forum and Search --> : NT Service


swine
Nov 24th, 1999, 04:39 AM
How can I make a VB app run as an NT Service? I am aware of the Srvany utility in the NT resource kit but I would rather not use Srvany. Is there another way to make a VB app run as an NT Service? Thanks in advance for the help.

johnweidauer
Apr 1st, 2004, 04:13 PM
need to add the "Microsoft NT Service" control to the form


Option Explicit

Dim StopService As Boolean

Private Sub Command1_Click()
Timer1.Enabled = True
End Sub

Private Sub Command2_Click()
Timer1.Enabled = False
End Sub

Private Sub Form_Load()
On Error GoTo ServiceError
App.TaskVisible = True
StopService = False

'Install the service
If Command = "/i" Then
' enable interaction with desktop
NTService.Interactive = True
'Install the program as an NT service
If NTService.Install Then
'Save the TimerInterval Parameter in the Registry
NTService.SaveSetting "Parameters", "TimerInterval", "5000"
MsgBox NTService.DisplayName & ": installed successfully"
Else
MsgBox NTService.DisplayName & ": failed to install"
End If
End
'Remove the Service Registry Keys and uninstall the service
ElseIf Command = "/u" Then
If NTService.Uninstall Then
MsgBox NTService.DisplayName & ": uninstalled successfully"
Else
MsgBox NTService.DisplayName & ": failed to uninstall"
End If
End
'Invalid parameter
ElseIf Command <> "" Then
MsgBox "Invalid Parameter"
End
End If
'Retrive the stored value for the timer interval
Timer1.Interval = CInt(NTService.GetSetting("Parameters", "TimerInterval", "5000"))
' enable Pause/Continue. Must be set before StartService
' is called or in design mode
NTService.ControlsAccepted = svcCtrlPauseContinue
' connect service to Windows NT services controller
NTService.StartService
Form_Resize
Exit Sub

ServiceError:
Call NTService.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description)

End Sub

Private Sub Form_Resize()
'on error resume next
Dim Ws As Integer

Ws = Me.WindowState

Select Case Ws
Case 0
SetWindowSize
Case 2
Me.WindowState = 0
SetWindowSize
Case Else
End Select

End Sub

Private Sub SetWindowSize()
Me.Height = 1155
Me.Width = 3030
End Sub

Private Sub Form_Unload(Cancel As Integer)
If Not StopService Then
'If MsgBox("Are you sure you want to unload the service?..." & vbCrLf & "the service will be stopped", vbQuestion + vbYesNo, "Stop Service") = vbYes Then
' NTService.StopService
' Cancel = True
'Else
Cancel = True
'End If
End If
End Sub

Private Sub NTService_Continue(Success As Boolean)
'Handle the continue service event
On Error GoTo ServiceError
Command1_Click
Success = True
NTService.LogEvent svcEventInformation, svcMessageInfo, "Service continued"
Exit Sub
ServiceError:
NTService.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description
End Sub

Private Sub NTService_Control(ByVal mEvent As Long)
'Take control of the service events
On Error GoTo ServiceError
'Label1.Caption = NTService.DisplayName & " Control signal " & CStr([mEvent])
Exit Sub
ServiceError:
NTService.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description
End Sub

Private Sub NTService_Pause(Success As Boolean)
'Pause Event Request
On Error GoTo ServiceError
Command2_Click
NTService.LogEvent svcEventError, svcMessageError, "Service paused"
Success = True
Exit Sub
ServiceError:
NTService.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description
End Sub

Private Sub NTService_Start(Success As Boolean)
'Start Event Request
On Error GoTo ServiceError
Command1_Click
Success = True
Me.Hide
Exit Sub
ServiceError:
NTService.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description
End Sub

Private Sub NTService_Stop()
'Stop and terminate the Service
On Error GoTo ServiceError
Command2_Click
StopService = True
Unload Me
ServiceError:
NTService.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description
End Sub

BrianS
Apr 1st, 2004, 04:41 PM
http://www.firedaemon.com/