使用我的代码,我可以启动和停止服务,基本上在我的应用程序中,我正在刷新 WIA 窗口服务。因此,在停止服务之前,我想知道状态..据我所知QueryServiceStatus
,这样做但在我的代码中它返回 0(失败)。
' start/stop/pause/continue a service
' SERVICENAME is the
' COMMAND can be 0=Start, 1=Stop, 2=Pause, 3=Continue
'
' returns True if successful, False otherwise
' if any error, call Err.LastDLLError for more information
Function ServiceCommand(ByVal ServiceName As String, ByVal command As Long) As _
Boolean
Dim hSCM As Long
Dim hService As Long
Dim res As Long
Dim query As Long
Dim lpServiceStatus As SERVICE_STATUS
' first, check the command
If command < 0 Or command > 3 Then Err.Raise 5
' open the connection to Service Control Manager, exit if error
hSCM = OpenSCManager(vbNullString, vbNullString, GENERIC_EXECUTE)
If hSCM = 0 Then Exit Function
' open the given service, exit if error
hService = OpenService(hSCM, ServiceName, GENERIC_EXECUTE)
If hService = 0 Then GoTo CleanUp
'fetch the status
query = QueryServiceStatus(hService, lpServiceStatus)
' start the service
Select Case command
Case 0
' to start a service you must use StartService
res = StartService(hService, 0, 0)
Case SERVICE_CONTROL_STOP, SERVICE_CONTROL_PAUSE, _
SERVICE_CONTROL_CONTINUE
' these commands use ControlService API
' (pass a NULL pointer because no result is expected)
res = ControlService(hService, command, lpServiceStatus)
End Select
If res = 0 Then GoTo CleanUp
' return success
ServiceCommand = True
CleanUp:
If hService Then CloseServiceHandle hService
' close the SCM
CloseServiceHandle hSCM
End Function
另外,如果有人也可以告诉我对窗口服务的一些疑问:
- 同一个系统可以有 2 个不同版本的窗口服务(WIA 1.0 和 WIA 2.0)吗?
- 上述服务是否具有不同的服务名称(WIA2.0 名称 =StiSvc)或相同?