导航

NtSvc.Ocx使用说明

Posted on 2007-04-14 18:41  pegger  阅读(2048)  评论(0编辑  收藏  举报
Account String , 账号属性,即本 NT 服务在哪一个 NT 域账号下运行,缺省是 LocalSystem 账号,实际上大多数的 NT 服务都可以在此账号下安全圆满的运行。
ControlsAccepted Long, 此服务接受那些 SCM 控制,为以下值:
0 允许 Start 以及 Stop .

2 允许 Pause 以及 Continue .

4 允许 shutdown 。

其他值,用户自定义的某些事件 .
利用这个属性,您可以自己决定 NT 服务进程某个(譬如某个不可中断操作)时刻是否允许 SCM 停止、暂停、启动等操作。
Dependencies String , 如果您编写的服务依赖于某个或者某些服务才能正常运行,您必须在注册服务时指定依赖的服务列表。 Dependencies 按照依赖顺序以 chr(0) 来分隔多个服务,最后必须以两个 chr(0) 结束。(大家可以看到这是一个 C/C++ 的存在痕迹)
DisplayName String, 显示名, NT 服务以何种名字显示给察看者。
Interactive Boolean , 是否允许有同桌面用户有交互的部分。
LoadOrderGroup String, 同 Dependencies 相关,决定如果本服务启动之前,那些服务必须启动,格式也类似,也以 chr(0) 分割,连续的两个 chr(0) 结尾。
Password String, 服务启动的口令,如果使用缺省得账号,就没有必要设定服务启动的帐号。
ServiceName String, 服务名,如果使用 net.exe 来控制服务, net.exe 的指定那一个服务的参数就是此属性中的字符串。
StartMode 枚举型,具体为:
vcStartAutomatic 2 服务可以自己启动

svcStartManual 3 服务手动启动

svcStartDisabled 4 服务不能自启动
另外有一个 Debug 属性,不做讨论。

我们要将一个 VB 程序当作一个 NT 服务,必须向系统作一些“申请” , 而相应的工作 VB 是无法很好的完成的。所以, NTSVC.ocx 提供了相应的方法留作我们想系统传递相关信息。
Install , 将当前 Vb 程序安装成 NT 服务,在此之前,您必须至少设置 DisplayName, ServiceName, ControlsAccepted 以及 StartMode 等属性。除此之外您可能还要设置 Account 、 Password 、 LoadOrderGroup 、 Dependencies 等。这些信息的设置正确与否,决定您的服务程序能否正常启动运行。
Uninstall, 将当前 NTSVC.ocx 指定的服务从系统注册表中删除。 NT 服务取决于系统服务注册表的设定,这是一个众所周知的秘密。
StartService ,将指定的服务启动,如果该服务注册了。
StopService ,停止服务,如果服务正在运行。
LogEvent , 记录服务事件。服务运行中,可能发生错误以及意料不到的事件,这些可以通过此方法记录下来,供管理员通过“事件察看器”察看相关的信息,以最优化服务。此方法有三个参数 event, id, message. Event 指发生的事件类型,可以设为以下值:
svcEventError 1 错误事件
svcEventWarning 2 警告事件 .
svcEventInformation 4 提供参考信息 .

svcEventAuditSuccess 8 审计成功 .

svcEventAuditFailure 10 审计失败
除了以上方法,可能用户还需要读写注册表,此控件还提供了注册表的访问方法:
DeleteSetting (section[, key])
GetAllSettings(section)
GetSetting(section, key[, default])
SaveSetting(section, key, setting).

'=====================使用实例===========================

使用这个控件注册成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)

河南恒友科贸有限公司 
电话 :0371-53733453    传真:0371-65388972
地址:郑州市郑花路8号 E-mail:hengyousoft@QQ.com