使用Microsoft提供的NTSVC.OCX控件,VB也可以将你的应用程序做成Service服务。
2004-12-30 10:17
627 查看
NTSVC.OCX控件,可以在网上搜索得到,在这里我就只介绍一下用法。
使用这个控件注册成Service服务的时候有个需要注意的,如果我们不使用/i或者/u参数,那么建立的Service服务会因为超时而不能启动。所以在注册Service服务的时候,必须带/i或/u参数。
1. 引用控件
选择“工程”-“引用”-“Microsoft NT Service Control”,如果没有,请先将NTSVC.OCX拷贝到%System32%/下,然后再引用对话框中选择浏览,添加该控件。
2. 主要代码
Private Sub Form_Load()
On Error GoTo ServiceError
'安装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
'删除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 Sub
ServiceError:
Call NTService.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description)
End Sub
'Unload the Service
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
Label1.Caption = "Stopping"
Cancel = True
Else
Cancel = True
End If
End If
End Sub
Private Sub NTService_Continue(Success As Boolean)
On Error GoTo ServiceError
Timer.Enabled = True
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)
On Error GoTo ServiceError
Exit Sub
ServiceError:
NTService.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description
End Sub
Private Sub NTService_Pause(Success As Boolean)
On Error GoTo ServiceError
Timer.Enabled = False
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)
On Error GoTo ServiceError
Success = True
Exit Sub
ServiceError:
NTService.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description
End Sub
Private Sub NTService_Stop()
On Error GoTo ServiceError
StopService = True
Unload Me
ServiceError:
NTService.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description
End Sub
3. 如果是有其他的控件触发Service服务的Install和Uninstall,可以采用Shell或者WinExec来处理。
先声明函数
Public Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long
Public Const SW_HIDE = 0
使用,比如用CheckBox触发
a.安装
Call WinExec(App.EXEName & " /i", SW_HIDE)
b.卸载
Call WinExec(App.EXEName & " /u", SW_HIDE)
使用这个控件注册成Service服务的时候有个需要注意的,如果我们不使用/i或者/u参数,那么建立的Service服务会因为超时而不能启动。所以在注册Service服务的时候,必须带/i或/u参数。
1. 引用控件
选择“工程”-“引用”-“Microsoft NT Service Control”,如果没有,请先将NTSVC.OCX拷贝到%System32%/下,然后再引用对话框中选择浏览,添加该控件。
2. 主要代码
Private Sub Form_Load()
On Error GoTo ServiceError
'安装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
'删除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 Sub
ServiceError:
Call NTService.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description)
End Sub
'Unload the Service
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
Label1.Caption = "Stopping"
Cancel = True
Else
Cancel = True
End If
End If
End Sub
Private Sub NTService_Continue(Success As Boolean)
On Error GoTo ServiceError
Timer.Enabled = True
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)
On Error GoTo ServiceError
Exit Sub
ServiceError:
NTService.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description
End Sub
Private Sub NTService_Pause(Success As Boolean)
On Error GoTo ServiceError
Timer.Enabled = False
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)
On Error GoTo ServiceError
Success = True
Exit Sub
ServiceError:
NTService.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description
End Sub
Private Sub NTService_Stop()
On Error GoTo ServiceError
StopService = True
Unload Me
ServiceError:
NTService.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description
End Sub
3. 如果是有其他的控件触发Service服务的Install和Uninstall,可以采用Shell或者WinExec来处理。
先声明函数
Public Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long
Public Const SW_HIDE = 0
使用,比如用CheckBox触发
a.安装
Call WinExec(App.EXEName & " /i", SW_HIDE)
b.卸载
Call WinExec(App.EXEName & " /u", SW_HIDE)
相关文章推荐
- [转载]ASP.NET Web 服务还是 .NET Remoting:如何选择,使用 Microsoft .NET 建立分布式应用程序
- Linux 怎么把自己写的脚本添加到服务里面,即可以使用service命令来调用
- 把自己写的脚本添加到服务里面,即可以使用service命令来调用
- 将源码包安装的服务,加入系统服务,可以使用service开启和关闭
- Build a service by C#.net(使用C#开发服务提供程序)
- 使用Java Service Wrapper将Java应用程序做成服务
- 使用 Windows API可以实现某些控件接口没有提供的功能
- Adapter 如果客户需要使用某个类的服务,而这项服务是这个类用一个不同的接口提供的,那么,可以使用适配器模式为客户提供一个期望的接口
- 尊敬的用户:您访问的域名有误或网页不存在,您可以使用我们提供的以下服务
- 负载均衡之---应用请求路由模块的使用(ARR)(九)[在应用程序服务器上为HostNameMemory亲和提供程序配置WMI服务]
- [VB][WIN]如何利用VB编写NT服务程序-NTSVC.OCX
- 尊敬的用户:您访问的域名有误或网页不存在,您可以使用我们提供的一下服务
- 在python的应用程序中,不需要关系型数据库时,如MySQL可以使用python提供的持久字典dbm来存储名称和值(键值对)
- 如何使用VB制作OCX控件
- 是否可以利用Windows 7提供位置服务的功能结合ArcGIS JavaScript™ Extension for Microsoft Virtual Earth
- 可以使用可用的服务和参数调用在“eWorld.WCFImplement.ServiceImplement.ImageArchiveService”类型上使用“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”找到的构造函数: 无法解析参数'eWorld.WCFBLL.ImageArchive.IDocumentOperation
- 在xml布局文件中,我们既可以设置px,也可以设置dp(或者dip)。一般情况下,我们都会选择使用dp,这样可以保证不同屏幕分辨率的机器上布局一致。但是在代码中,如何处理呢?很多控件的方法中都只提供了
- 使用java service wrapper把windows flume做成服务
- 100% .NET Control_使用CurrencyManager 创建一个导航条来控制DatagGrid的XNavBar控件(VB.NET).
- 如何在VB 中直接打开并选择目录,而不使用麻烦的多个控件?