NTSVC.OCX control, you can search online, here I only introduce the usage.
When using this control to register as a Service service, there is a need to pay attention. If we don't use the / i or / u parameter, the created Service service will not start because of the timeout. So when you register the service service, you must have / i or / u parameters.
1. Reference Control Select "Project" - "Reference" - "Microsoft NT Service Control", if not, please copy NTSVC.OCX to% System32% / under, then reference the dialog box to select Browse, add this control.
2. Main Code Private Sub Form_Load () On Error GoTo ServiceError 'Service Installation service If Command = "/ i" Then NTService.Interactive = True If NTService.Install Then NTService.SaveSetting "Parameters", "TimerInterval", "300" MsgBox NTService.DisplayName & ": installed successfully" Else MsgBox NTService.DisplayName & ": failed to install" End If End 'delete service service ElseIf Command = "/ u" Then If NTService.Uninstall Then MsgBox NTService.DisplayName & ": uninstalled successfully "Else MsgBox NTService.DisplayName &": failed to uninstall "End If End End If Timer.Interval = CInt (NTService.GetSetting (" Parameters "," TimerInterval "," 300 ")) NTService.ControlsAccepted = svcCtrlPauseContinue NTService.StartService Exit SubServiceError: Call Ntservice.logevent (SvcMessageError, SVCEventError, "[" & Err.Number & "& err.description) End Sub
'Unload the ServicePrivate 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 Label1.Caption =" Stopping "Cancel = True Else Cancel = True End If End IfEnd SubPrivate Sub NTService_Continue (Success As Boolean) On Error GoTo ServiceError Timer.Enabled = True Success = True NTService.LogEvent SVCEventInformation, SvcMessageInfo, "Service Continued" exit subserviceerror: ntservice.logevent svcMessageError, SVCEventEverror, "[" ["& err.Number &"] "& err.descriptionEND SUB
Private sub ntservice_control (Byval Mevent As Long) ON Error Goto ServiceError EXIT SUBSERVICEERROR: NTSERVICE.LOGEVENT SVCMESSAGEERROR, SVCEVENTERROR, "[" & Err.Number & "]" & Err.DescriptionEND SUB
Private Sub NTService_Pause (Success As Boolean) On Error GoTo ServiceError Timer.Enabled = False NTService.LogEvent svcEventError, svcMessageError, "Service paused" Success = True Exit SubServiceError: NTService.LogEvent svcMessageError, svcEventError, "[" & Err.Number & " ] "& Err.DescriptionEND SUB
Private Sub NTService_Start (Success As Boolean) On Error GoTo ServiceError Success = True Exit SubServiceError: NTService.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "]" & Err.DescriptionEnd Sub
Private Sub NTService_Stop () On Error GoTo ServiceError StopService = True Unload MeServiceError:. NTService.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "]" & Err.DescriptionEnd Sub3 If there are other services Install control fires Service And Uninstall, you can use Shell or Winexec to process. Declare function Public Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As LongPublic Const SW_HIDE = 0 to use, such as with trigger CheckBox a. Mounting Call WinExec (App.EXEName & "/ i", SW_HIDE )
b. Uninstall Call Winexec (App.Exename & "/ U", SW_HIDE)