我把一个程序做成windows服务(用的是ocx控件方式),
Option ExplicitPrivate Sub Form_Load()
Dim strFile As String
With NTService1
If Command = "/i" Then
.StartMode = 2
.DisplayName = "ddd"
.ServiceName = "ddd"
.Interactive = True
If .Install Then
'Save the TimerInterval Parameter in the Registry
.SaveSetting "Parameters", "TimerInterval", "300"
Else
End If
End
'Remove the Service Registry Keys and uninstall the service
ElseIf Command = "/u" Then
If .Uninstall Then
Else
End If
End
'Invalid parameter
ElseIf Command <> "" Then
End
End If
.ControlsAccepted = 2 'svcCtrlPauseContinue
.StartService
End With
strFile = App.Path & IIf(Right(App.Path, 1) = "\", "", "\") & "ddd.exe"
If Dir(strFile) <> "" Then
Call Shell(strFile)
End If
If App.PrevInstance Then
End
End If
Me.Hide
End SubPrivate Sub Form_Paint()
Me.Hide
End SubPrivate Sub Form_Resize()
Me.Hide
End SubPrivate Sub Form_Unload(Cancel As Integer)
With NTService1
.StopService
End With
End SubPrivate Sub NTService1_Continue(Success As Boolean)
Success = True
End Sub
Private Sub NTService1_Pause(Success As Boolean)
Success = True
End SubPrivate Sub NTService1_Start(Success As Boolean)
Success = True
End SubPrivate Sub NTService1_Stop()
On Error Resume Next
Unload Me
End Sub
在我的ddd.exe中,有用到api函数现在的问题是:我指定本地系统账户运行ddd.exe的时候,有些api函数无法执行,我怀疑是权限设置的问题。可是如果指定administrator来运行服务,问题又来了,我如何设置服务允许桌面交互呢?不设置为允许交互是不能运行外部程序的 。
有高手做过这个吗?帮帮忙
Option ExplicitPrivate Sub Form_Load()
Dim strFile As String
With NTService1
If Command = "/i" Then
.StartMode = 2
.DisplayName = "ddd"
.ServiceName = "ddd"
.Interactive = True
If .Install Then
'Save the TimerInterval Parameter in the Registry
.SaveSetting "Parameters", "TimerInterval", "300"
Else
End If
End
'Remove the Service Registry Keys and uninstall the service
ElseIf Command = "/u" Then
If .Uninstall Then
Else
End If
End
'Invalid parameter
ElseIf Command <> "" Then
End
End If
.ControlsAccepted = 2 'svcCtrlPauseContinue
.StartService
End With
strFile = App.Path & IIf(Right(App.Path, 1) = "\", "", "\") & "ddd.exe"
If Dir(strFile) <> "" Then
Call Shell(strFile)
End If
If App.PrevInstance Then
End
End If
Me.Hide
End SubPrivate Sub Form_Paint()
Me.Hide
End SubPrivate Sub Form_Resize()
Me.Hide
End SubPrivate Sub Form_Unload(Cancel As Integer)
With NTService1
.StopService
End With
End SubPrivate Sub NTService1_Continue(Success As Boolean)
Success = True
End Sub
Private Sub NTService1_Pause(Success As Boolean)
Success = True
End SubPrivate Sub NTService1_Start(Success As Boolean)
Success = True
End SubPrivate Sub NTService1_Stop()
On Error Resume Next
Unload Me
End Sub
在我的ddd.exe中,有用到api函数现在的问题是:我指定本地系统账户运行ddd.exe的时候,有些api函数无法执行,我怀疑是权限设置的问题。可是如果指定administrator来运行服务,问题又来了,我如何设置服务允许桌面交互呢?不设置为允许交互是不能运行外部程序的 。
有高手做过这个吗?帮帮忙
做vxd咱不会,
和应用程序交互的通道以消息和网络信息为主。
最好不要把NT服务当应用程序来做,因为两者存在的目的不同,一个是为服务,一个
是为交互。对于应用程序,如果想隐藏界面,完全可以将界面隐藏起来工作,区别就
是一个是在为登陆系统时启动,用的是System权限,一个是登陆后启动,用的是登陆
用户的权限。如果不是以服务为目的,最好不要使用NT服务。
1、驱动程序
2、一次性运行的应用程序
3、服务程序如果你的程序不需要长期后台运作,只是单纯想在登陆前运行,
也可以使用一次性运行的应用程序方法。
你可以向注册表的:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
添加你要运行的程序路径,他会帮你在启动的时候执行,不过执行完会自动删除里面所有
的内容还有就是服务程序,如果需要交互,可能在创建服务的时候设置为交互模式,至于创建NT服务,
可以通过 WMI 的方法创建,也可以通过相关的API函数创建服务,相对来说 WMI 因为是对象,
所以使用应该算比较简单的。但API其实也不复杂,只要了解如下API函数就可以了
OpenSCManager
CreateService
CloseServiceHandle
CloseServiceHandle